C++ HLSL implements simple image processing functions

  • 2020-05-17 05:57:05
  • OfStack

Since the data decoded by dxva2 is not suitable for copy back to CPU for processing, the best way is to process it directly on GPU. The pixel shader of D3D can operate the pixels directly, so it is very simple and convenient to realize the point operation, and the simple convolution operation effect is also very good. However, D3D9 also has a lot of limitations, which makes it a little incompetent for overly complex image processing.

1. The operation

Point arithmetic is very easy to implement with HLSL, almost as the formula is written, the code is written. Take RGB to grayscale image display as an example:


texture Tex0 ;
int iFlag = 0 ;
float aValue= 0.0 ;
float bValue= 0.0 ;
sampler2D YTex = 
sampler_state
{
  Texture = <Tex0> ;
  MipFilter = LINEAR ;
  MinFilter = LINEAR ;
  MagFilter = LINEAR ;

  AddressU = CLAMP ;
  AddressV = CLAMP ;
};
struct PS_INPUT
{
  float2 uvCoords0 : TEXCOORD0 ;
};
float4 Main( PS_INPUT input ) : COLOR0
{
  float4 yuvColor ;
  //rgb to gray  I don't know if that's what it looks like, but let's say that's what it looks like 
  float gray = tex2D( YTex, input.uvCoords0 ).r * 0.299 + tex2D( YTex, input.uvCoords0 ).g * 0.587 + tex2D( YTex, input.uvCoords0 ).b * 0.114 ;
  float s = 0 ;
  if(iFlag == 0)
  {
    s = aValue * gray + bValue/255 ;
  }
  else if(iFlag == 1)
  {
    s = aValue * log(1+gray) ;
  }
  else if(iFlag == 2)
  {
    s = aValue * pow(abs(gray),bValue) ;
  }
  yuvColor.r = s ;
  yuvColor.g = s ;
  yuvColor.b = s ;
  yuvColor.a = 1.0 ;
  return yuvColor ;
}

Point operation is so easy because GPU is parallel operation. I think it can be regarded as one thread for every pixel (BGRA), which is about the so-called data parallelism in OpenCL. This is a very simple program, the number of instructions is small, the program structure is also very simple, shader version of 2.0 can be easily compiled.

2. Examples of convolution operation

In the case of more instructions, version 2.0 of shader can not be solved. The previous version 3.0 can do some simple convolution operation. Take median filtering as an example:


texture Tex0 ;
matrix WorldMatrix;
matrix ViewMatrix;
matrix ProjMatrix;
sampler2D YTex = 
sampler_state
{
  Texture = <Tex0> ;
  MipFilter = LINEAR ;
  MinFilter = LINEAR ;
  MagFilter = LINEAR ;
  AddressU = CLAMP ;
  AddressV = CLAMP ;
};
struct VS_INPUT
 {
  float4 pos  : POSITION;
  float4 color : COLOR0;
  float2 tex  : TEXCOORD0;
 };
// 
struct VS_OUTPUT
 {
  float4 pos   : POSITION;
  float4 color  : COLOR0;
  float2 tex   : TEXCOORD0;
 };
float2 g_v4ScreenSize;
int ksize = 1 ;
float fLeft = -1.0f ;
float fTop = -1.0f ;
float fRight = -1.0f ;
float fBottom = -1.0f ;
//--------------------------------- BurTechnique --------------------------------------
VS_OUTPUT MainVS_Screen( VS_INPUT In )
{
  VS_OUTPUT Out = ( VS_OUTPUT )0;
  float4x4 matWorldView = mul(WorldMatrix,ViewMatrix);
  float4x4 matProject = mul(matWorldView,ProjMatrix);
  Out.pos = mul(In.pos,matProject);
  Out.tex = In.tex;
  Out.color = In.color;
  return Out;
}
float4 MainPS_Screen( VS_INPUT In ) : COLOR0
{
  float4 outColor = tex2D( YTex, In.tex ).rgba ;
  if(ksize <= 1 || ksize%2 == 0)
  {
    return outColor ;
  }
  if( ksize > 11 || ksize < 3)
  {
    return outColor ;
  }
  if(!(In.tex.x < fRight && In.tex.y < fBottom && In.tex.x > fLeft && In.tex.y > fTop))
  {
    return outColor ;
  }
  //  Grain size 
  float2 TexSize = float2( g_v4ScreenSize.x , g_v4ScreenSize.y );
  float x_off = 1.0f / TexSize.x;
  float y_off = 1.0f / TexSize.y;
  float2 fX0Y0 = In.tex - float2(x_off * ksize/2, y_off*ksize/2) ;
  float3 sum = {0.0f, 0.0f, 0.0f} ;
  if(ksize >= 3)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*2)).rgb;
  }
  if(ksize >= 5)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*4)).rgb;
  }
  if(ksize >= 7)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*6)).rgb;
  }
  if(ksize >= 9)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*8)).rgb;
  }
  if(ksize >= 11)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*10)).rgb;
  }
  outColor = float4(sum/(ksize*ksize),1.0f);
  return outColor ;
}
//---------------------------  technology ---------------------------
technique BurTechnique
{
  pass P0
  {  
    LightEnable[0] = false;
    VertexShader = compile vs_3_0 MainVS_Screen();
    PixelShader = compile ps_3_0 MainPS_Screen();
  }
}

Since version 3.0 of shader doesn't seem to allow pixel shader to stand alone, I switched from a pixel shader implementation for point operations to a special effects implementation. There are if statements and for statements in the HLSL syntax, but instead of using the for loop, the program goes out of its way to list them all. This is because some limitations are found in actual use, such as if (A) for if statements > One of B),A and B must be constant, as seen above; The same is true in the middle of the for loop, except that in the second layer of the j loop, it can be the i of the first layer of the loop, that is, it cannot be


 for(int i=0;i<ksize;i++)
{
  for(int j=0;j<ksize1;j++)
    {
  ..........
    }
}

Both ksize and ksize1 of the above code must be constants, except that ksize1 can be i of the layer 1 loop. I don't know if the subsequent version of shader has this problem, but the version I am currently using does.

One other thing to note is the number of instructions. Version 2.0 of shader supports a relatively small number of instructions, and version 3.0 supports a much larger number. One more note is that version 3.0 of shader only supports D3D 9.0C. If more complex image processing is required, D3D11 and compute shader are recommended. Although I haven't used them before, it should be able to handle some more complex image processing from the introduction.


Related articles: