Advertisement

Can RWTexture2D variables as a parameter for a user-defined function

Started by September 05, 2017 08:29 AM
6 comments, last by galop1n 7 years, 5 months ago

Hi, I am newer of Direct3D 11, and I want to get help from you.

In Direct3D samples, we usually define RWTexture2D variables globally, and use it in functions directly. Is it legal to transfer this variable as a parameter of a user-defined function?

RWTexture2D<float4> texture : regiters(u0);

void setData(RWTexture2D<float4> tex, float2 data)

{    tex[uint2(0, 0)] = data; }

PS_OUTPUT main(PS_INPUT input)

{

    setData(texture, float2(0.0, 0.0));

    PS_OUTPUT output;

    return output;

}

There's no reason why not. If the compiler accepts it then it should be fine. Everything gets flattened out anyway, so function calls don't exist in the final compiled version anyway.

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

Advertisement
17 hours ago, ajmiles said:

There's no reason why not. If the compiler accepts it then it should be fine. Everything gets flattened out anyway, so function calls don't exist in the final compiled version anyway.

It is legal and encouraged, global are bad, just in term of code best practice.

Even without dynamic texture indexing, but you may want to call the same function twice with two different textures, imagine for example "float3 SampleNormal( float2 uv, Texture2D img, SamplerState samp )", you can now factorize the typical normal unpacking like "2*xyz-1" in any shader using more than one normal map ( common situation to add extra tiled details ).

 

With d3d12 and vulkan, we have dynamic indexing of textures, and your code don't even have to be flatten anymore, again, it is more a question of good programming practice to start with !

 

 

1 hour ago, galop1n said:

It is legal and encouraged, global are bad, just in term of code best practice.

Even without dynamic texture indexing, but you may want to call the same function twice with two different textures, imagine for example "float3 SampleNormal( float2 uv, Texture2D img, SamplerState samp )", you can now factorize the typical normal unpacking like "2*xyz-1" in any shader using more than one normal map ( common situation to add extra tiled details ).

 

With d3d12 and vulkan, we have dynamic indexing of textures, and your code don't even have to be flatten anymore, again, it is more a question of good programming practice to start with !

 

 

Thank you for your reply.

I have another question, for D3D11, if I declare a RWTexture2D variable without specifying the register in compute shader, for example

RWTexture2D<float4> texture;

when I call 


void CSSetUnorderedAccessViews(
  [in]                 UINT                             StartSlot,
  [in]                 UINT                             NumUAVs,
  [in, optional]       ID3D11UnorderedAccessView *const *ppUnorderedAccessViews,
  [in, optional] const UINT                             *pUAVInitialCounts
);

How I know the “StartSlot” for HLSL texture variable.  Does it has a default slot for non-register variable, like zero?

 

If you do not force it in the shader source, the compiler will decide for you, and you need to use "ID3D11ShaderReflection" to find what the slot is.

37 minutes ago, galop1n said:

If you do not force it in the shader source, the compiler will decide for you, and you need to use "ID3D11ShaderReflection" to find what the slot is.

Thank you. As we all known, unordered access views(UAVs) are available on all shader stages, and the UAVs that are shared across all pipeline stages. For example, a UAV that is bound at slot 0 at the output-merger stage is available at slot 0 to VS/HS/DS/GS/PS. If a RWTexture2D variable are declared the same in vertex shader and pixel shader. Does the compiler ensure to assigned the same slot for the two variables?

The vertex shader and pixel shader all declare a variable as below,

RWTexture2D<float4> texture;

In OpenGL, if two uniform exist in vertex shader and pixel shader respectively, with the same name, they are bound the the same binding value. Does d3d11 is like it?

 

Advertisement

There is no guarantee, you compile your shader separately after all.

 

Imagine you have two texture, A and B, in the VS you read both, they get respectively t0 and t1. The PS read B only and it will get t0.

This topic is closed to new replies.

Advertisement