Home > other >  Written by HLSL Compute Shader of problems dealing with simple map
Written by HLSL Compute Shader of problems dealing with simple map

Time:09-18

Trying to write "the Introduction to 3 d Game Programming with DirectX 11" in chapter 12 exercises at 5 problems,
First initialized again two format for DXGI_FORMAT_R32_FLOAT map as the now moment and wave height map, the past moment with full 0 initialization:
 
D3D11_TEXTURE2D_DESC tex2DDesc;
Tex2DDesc. Height=hCount;
Tex2DDesc. Width=wCount;
Tex2DDesc. MipLevels=1;
Tex2DDesc. ArraySize=1;
Tex2DDesc. Format=DXGI_FORMAT_R32_FLOAT;
Tex2DDesc. SampleDesc. Count=1;
Tex2DDesc. SampleDesc. Quality=0;
Tex2DDesc. Usage=D3D11_USAGE_DEFAULT;
Tex2DDesc. BindFlags=D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
Tex2DDesc. CPUAccessFlags=0;
Tex2DDesc. MiscFlags=0;
//Set the init data for both height maps to 0.0 f
STD: : vector VecInitHeight;
For (int I=0; I & lt; HCount * wCount; + + I)
{
VecInitHeight. Push_back (0.0 f);
}
D3D11_SUBRESOURCE_DATA subHeightData;
SubHeightData. PSysMem=& amp; VecInitHeight [0].
SubHeightData. SysMemPitch=wCount * sizeof (float).
SubHeightData. SysMemSlicePitch=wCount hCount * * sizeof (float).
//the javax.media.sound.sampled VS "todo" in the transform
HR (device - & gt; CreateTexture2D (& amp; Tex2DDesc, & amp; SubHeightData, & amp; MCurHeightMap));
HR (device - & gt; CreateTexture2D (& amp; Tex2DDesc, & amp; SubHeightData, & amp; MLasHeightMap));

And then create the corresponding the SRV UAV, after using Compute Shader update:

 # define THREAD_NUM 32 
# define MEM_SIZE (THREAD_NUM + 2)

//Only need 3 params for CSUpdateHeight
Cbuffer cbUpdateHeightPerObject
{
Float gK1;
Float gK2;
Float gK3;
};

Cbuffer cbUpdateNormAndTangPerObject
{
Float gSpatialStep;
};

Texture2D & lt; Float> GCurTex;
RWTexture2D & lt; Float> GLasTex;
RWTexture2D & lt; Float2 & gt; GNormTex;
RWTexture2D & lt; Float2 & gt; GTangTex;

Groupshared float gCache1 [MEM_SIZE] [MEM_SIZE];//curtex
Groupshared float gCache2 [MEM_SIZE] [MEM_SIZE];//prevtex

Int2 MaxInt2 (int2 SRC, int2 border)
{
Return int2 (Max (SRC) x, border, x), Max (SRC) y, border, y));
}

Int2 MinInt2 (int2 SRC, int2 border)
{
Return int2 (min (SRC) x, border, x), min (SRC) y, border, y));
}

[numthreads (THREAD_NUM THREAD_NUM, 1)]
Void CSUpdateHeight (int3 groupThreadID: SV_GroupThreadID,
Int3 dispatchThreadID: SV_DispatchThreadID)
{
//Update gCurTex data to gChache1
//Update gLasTex data to gCache2
//Clamp left up border
If (groupThreadID. X==0 | | groupThreadID. Y==0)
{
Int2 xy0=int2 (dispatchThreadID. X - 1, dispatchThreadID. Y - 1);//- 1 men?
Int2 xy1=int2 (0, 0);
Int2 y=MaxInt2 (xy0, int2 (0, 0));
GCache1 [groupThreadID x] [groupThreadID. Y]=gCurTex [y];
GCache2 [groupThreadID x] [groupThreadID. Y]=gLasTex [y];
}
//Clamp right down border
If (groupThreadID.==x (THREAD_NUM - 1) | | groupThreadID. Y==(THREAD_NUM - 1))//- 1 men?
{
Int2 xy0=int2 (dispatchThreadID. X + 1, dispatchThreadID. Y + 1);
Int2 xy1=int2 (gCurTex. Length. X - 1, gCurTex. Length. The y - 1);
Int2 y=MinInt2 (xy0 xy1);
GCache1 [groupThreadID. X + 2] [groupThreadID. Y + 2]=gCurTex [y];
GCache2 [groupThreadID. X + 2] [groupThreadID. Y + 2]=gLasTex [y];
}
//Other
Int2 xy0=int2 (gCurTex. Length. X - 1, gCurTex. Length. The y - 1);
Int2 y=MinInt2 (dispatchThreadID. Xy, xy0);
GCache1 [groupThreadID. X + 1] [groupThreadID. Y + 1]=gCurTex [y];
GCache2 [groupThreadID. X + 1] [groupThreadID. Y + 1]=gLasTex [y];

//Wait for the cache the synchronize
GroupMemoryBarrierWithGroupSync ();

Cx=groupThreadID int. X + 1;
Int cy=groupThreadID. Y + 1;
//Restrict border from changing
If (dispatchThreadID x!=0 & amp; &
DispatchThreadID. Y!=0 & amp; &
DispatchThreadID. XDispatchThreadID. YGLasTex [dispatchThreadID xy]=
GK1 * gCache2 (cx) (cy) +
GK2 * gCache1 (cx) (cy) +
GK3 * (gCache1 cx + [1] (cy) +
Cx - gCache1 [1] (cy) +
GCache1 cx] [[cy + 1] +
GCache1 cx] [] [cy - 1);
}

Technique11 UpdateHeight
{
Pass P0
{
SetVertexShader (NULL);
SetPixelShader (NULL);
SetComputeShader (CompileShader (cs_5_0, CSUpdateHeight ()));
}
}

And call in the code

 
T +=dt;
If (t & gt;=mTimeStep)
{
+ + testcount;
//Update the current and the last height map
D3DX11_TECHNIQUE_DESC techDesc;
Effects: : WavesUpdateFX - & gt; The Test - & gt; GetDesc (& amp; TechDesc);
For (int p=0; p {
Effects: : WavesUpdateFX - & gt; SetTestMap (mLasHeightSRV);
Effects: : WavesUpdateFX - & gt; SetTestCount (testcount);

Effects: : WavesUpdateFX - & gt; The Test - & gt; GetPassByIndex (p) - & gt; The Apply (0, dc);
Int cxGroups=ceilf (1.0 f * mNumRows/THREAD_NUM);
Int cyGroups=ceilf (1.0 f * mNumCols/THREAD_NUM);
HR (dc - & gt; Dispatch (cxGroups cyGroups, 1));

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related