Search Unity

Question How to sample a texture in a compute shader ?

Discussion in 'Scripting' started by barbelot, Aug 11, 2020.

  1. barbelot

    barbelot

    Joined:
    Jul 18, 2017
    Posts:
    38
    Hello,

    I am trying to sample a texture in a compute shader. Following the compute shader documentation page. You need to create a sampler, then use texture.Sample().

    I defined my texture and sampler as
    Code (CSharp):
    1. Texture2D<float4> myTexture;
    2. SamplerState linearClampSampler
    and call it as
    Code (CSharp):
    1. float4 color = myTexture.Sample(linearClampSampler, uv);
    However, this gives me an cannot map expression to cs_5_0 instruction set error

    What is the correct way to sample a texture in a compute shader ?
     
    dylan-hart likes this.
  2. barbelot

    barbelot

    Joined:
    Jul 18, 2017
    Posts:
    38
    It turns out SampleLevel works
    Code (CSharp):
    1. float4 color = myTexture.SampleLevel(linearClampSampler, uv, 0);
    I wish this kind of simple example was added in the compute shader documentation.
     
  3. yhyu13

    yhyu13

    Joined:
    Jul 21, 2021
    Posts:
    1
    @barbelot Coz Mipmapping sampling is not defined in compute shader pipeline, you need to specifiy the texture lod manully
     
    AshwinMods and ReMix3D like this.