Graphics Reference
In-Depth Information
A compute shader cannot use an SRV and UAV of the same underlying
texture resource at the same time. In fact, an SRV to the same texture as
the UAV cannot be bound to any stage of the pipeline at the same time as
running the compute shader. Doing so will result in a warning if the debug
layer is active and will remove the SRV from the pipeline stage it is bound
to. For simple processing, such as desaturation/contrast/brightness,
we could however use the same UAV as the input and output.
Next we will create the HLSL compute shader, compile, and run it.
3.
The complete compute shader source code is:
Texture2D<float4> input : register(t0);
RWTexture2D<float4> output : register(u0);
// used for RGB/sRGB color models
#define LUMINANCE_RGB float3(0.2125, 0.7154, 0.0721)
#define LUMINANCE(_V) dot(_V.rgb, LUMINANCE_RGB)
// Desaturate the input, the result is returned in output
[numthreads(THREADSX, THREADSY, 1)]
void DesaturateCS(uint groupIndex: SV_GroupIndex,
uint3 groupId : SV_GroupID,
uint3 groupThreadId: SV_GroupThreadID,
uint3 dispatchThreadId : SV_DispatchThreadID)
{
float4 sample = input[dispatchThreadId.xy];
// Calculate the relative luminance
float3 target = (float3)LUMINANCE(sample.rgb);
output[dispatchThreadId.xy] = float4(target, sample.a);
}
4.
We can add the previous shader code to a string variable easily by @ -quoting (for
example, var hlslCode = @"…"; ) to interpret the string as a literal value and
ignore normal escaping rules (this also allows the string to span multiple lines).
5.
With the previous shader source code within a string variable named hlslCode ,
we can now compile and run the shader as follows:
// Define the thread group size
SharpDX.Direct3D.ShaderMacro[] defines = new[] {
new SharpDX.Direct3D.ShaderMacro( "THREADSX", 16 ),
new SharpDX.Direct3D.ShaderMacro( "THREADSY", 4 ),
};
using (var bytecode = ShaderBytecode.Compile(hlslCode,
 
Search WWH ::




Custom Search