Jump to content

  • Log In with Google      Sign In   
  • Create Account


DgekGD

Member Since 01 Feb 2012
Offline Last Active Mar 08 2014 01:50 PM
-----

Topics I've Started

Texture UnorderedAccessView problem (compute shader)

03 December 2012 - 12:01 PM

Hi, guys!

I'm trying to implement Gaussian Filter using compute shader.
I have two texture resources. Firstly, I render entire scene to the first texture. Then I apply gaussian filter for this texture using compute shader( which writes results to the second texture). And finally, i use shader resource view of the second texture in final render pass (pixel shader just executes texture.Sample). But as result i just get black screen:(

Initialize textures and their resource views:
//Texture used for rendering 
ID3D11Texture2D* renderTargetTexture;
ID3D11RenderTargetView* renderTargetTextureView;
ID3D11ShaderResourceView* shaderRenderTextureView;

//texture to which result of gaussian filter will be applied

ID3D11Texture2D* gaussTexture;
ID3D11UnorderedAccessView* gaussTextureUAView;
ID3D11ShaderResourceView*  gaussTextureSRView;
ID3D11RenderTargetView*    gaussTextureRTView;

/***** Create Texture Render Target View *****/
D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;


//create render texture
ZeroMemory(&textureDesc, sizeof(D3D11_TEXTURE2D_DESC));
textureDesc.Width = textureWidth;
textureDesc.Height = textureHeight;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
hr = d3d11Device->CreateTexture2D(&textureDesc, NULL, &renderTargetTexture);
if (FAILED(hr))
  return false;


//create render target view
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
hr = d3d11Device->CreateRenderTargetView(renderTargetTexture, &renderTargetViewDesc, &renderTargetTextureView);
if (FAILED(hr))
  return false;


//create shader resource view
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
hr = d3d11Device->CreateShaderResourceView(renderTargetTexture, &shaderResourceViewDesc, &shaderRenderTextureView);
if (FAILED(hr))
  return false;

/*** GAUSSIAN TEXTURE ***/

D3D11_TEXTURE2D_DESC gtDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC  srView;
D3D11_UNORDERED_ACCESS_VIEW_DESC uaDesc;
D3D11_RENDER_TARGET_VIEW_DESC rtDesc;
//create gauss texture
ZeroMemory(&gtDesc, sizeof(gtDesc));
gtDesc.Width = textureWidth;
gtDesc.Height = textureHeight;
gtDesc.MipLevels = 1;
gtDesc.ArraySize = 1;
gtDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
gtDesc.SampleDesc.Count = 1;
gtDesc.Usage = D3D11_USAGE_DEFAULT;
gtDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
gtDesc.CPUAccessFlags = 0;
gtDesc.MiscFlags = 0;
hr = d3d11Device->CreateTexture2D(&gtDesc, NULL, &gaussTexture);
if (FAILED(hr))
  return false;

//create render target view
rtDesc.Format = gtDesc.Format;
rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtDesc.Texture2D.MipSlice = 0;
hr = d3d11Device->CreateRenderTargetView(gaussTexture, &rtDesc, &gaussTextureRTView);
if (FAILED(hr))
  return false;

//create shader resource view
srView.Format = gtDesc.Format;
srView.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srView.Texture2D.MostDetailedMip = 0;
srView.Texture2D.MipLevels = 1;
hr = d3d11Device->CreateShaderResourceView(gaussTexture, &srView, &gaussTextureSRView);
if (FAILED(hr))
  return false;

//create unordered access view
uaDesc.Format = gtDesc.Format;
uaDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
uaDesc.Texture2D.MipSlice = 0;
hr = d3d11Device->CreateUnorderedAccessView(gaussTexture, &uaDesc, &gaussTextureUAView);
if (FAILED(hr))
  return false;

Now render entire scene to the first texture:
float bgTexColor[4] = {(1.0f, 0.0f, 0.0f, 1.0f)}; //default color is red!


d3d11DevCon->VSSetShader(VS, 0, 0);
d3d11DevCon->PSSetShader(PS, 0, 0);

d3d11DevCon->RSSetViewports(1, &texViewport);
d3d11DevCon->OMSetRenderTargets(1, &renderTargetTextureView, NULL);

d3d11DevCon->ClearRenderTargetView(renderTargetTextureView, bgTexColor);
d3d11DevCon->Draw(36, 0); //draw the cube

Apply gaussian filter:
d3d11DevCon->CSSetShader(CS, 0, 0);
d3d11DevCon->CSSetShaderResources(0, 1, &shaderRenderTextureView); //bind out rendered scene as shader resource view
d3d11DevCon->CSSetUnorderedAccessViews(0, 1, &gaussTextureUAView, NULL); //unordered access for resulting texture!
UINT x = (UINT)( ceil( textureWidth / 32.0f ) );
UINT y = (UINT)( ceil( textureHeight / 32.0f ) );

d3d11DevCon->Dispatch(x, y, 1);

Finally, render our final texture to the screen (in pixel shader i just use texture.Sample) :
d3d11DevCon->VSSetShader(textureVS, 0, 0);
d3d11DevCon->PSSetShader(texturePS, 0, 0);

d3d11DevCon->RSSetViewports(1, &viewport);
d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, NULL); //back buffer now is render target

d3d11DevCon->PSSetShaderResources(0, 1, &gaussTextureSRView);
d3d11DevCon->Draw(6, 0);

Oh, by the way, here is my compute shader:
//Resources
Texture2D<float4> InputTex : register( t0 );
RWTexture2D<float4> OutputTex : register( u0 );



//filter kernel coefficients
static const float kernel[7][7] = {
0.000904706, 0.003157733, 0.00668492,  0.008583607, 0.00668492,  0.003157733, 0.000904706,
0.003157733, 0.01102157,  0.023332663, 0.029959733, 0.023332663, 0.01102157,  0.003157733,
0.00668492,  0.023332663, 0.049395249, 0.063424755, 0.049395249, 0.023332663, 0.00668492,
0.008583607, 0.029959733, 0.063424755, 0.081438997, 0.063424755, 0.029959733, 0.008583607,
0.00668492,  0.023332663, 0.049395249, 0.063424755, 0.049395249, 0.023332663, 0.00668492,
0.003157733, 0.01102157,  0.023332663, 0.029959733, 0.023332663, 0.01102157,  0.003157733,
0.000904706, 0.003157733, 0.00668492,  0.008583607, 0.00668492,  0.003157733, 0.000904706
};


[numthreads(32, 32, 1)]
void CS(uint3 DispatchThreadId : SV_DispatchThreadID)
{
	 //get coordinates of top left corner
	 int3 textureCoords = DispatchThreadId - int3(3, 3, 0);

	 //initialize final color
     float4 finalColor = (float4)0.0f;


	 //loop through filter kernel getting final color
     for (int  i = 0; i < 7; i++)
          for (int  j = 0; j < 7; j++)
				 finalColor += InputTex.Load(textureCoords + int3(i, j, 0) ) * kernel[i][j];

	 //assign new color value to the output image
     OutputTex[DispatchThreadId.xy] = finalColor;
     //OutputTex[DispatchThreadId.xy] = float4(1.0f, 1.0f, 1.0f, 1.0f);
}

Why I think that the problem is with Unordered Access View? I tried to clear gaussTexture with red color after the compute shader has executed:
float bgTexColor[4] = {(1.0f, 0.0f, 0.0f, 1.0f)};
d3d11DevCon->ClearRenderTargetView(gaussTextureRTView, bgTexColor);

And i still get the black screen. But if I comment binding the unordered access to the compute shader I get what I should get - the red screen:
//d3d11DevCon->CSSetUnorderedAccessViews(0, 1, &amp;gaussTextureUAView, NULL); //unordered access for resulting texture!

I'm pretty sure that compute shader itself works correctly as commenting Dispatch call without commenting CSSetUnorderedAccessViews still suffers the problem and I get only black screen.

Where the problem can be?

I would appreciate any help! Thanks in advance!

GPU Skinning problem (MD5 Doom)

13 November 2012 - 05:05 PM

Hello!

I'm trying to implement GPU skinning using md5 format. But after many hours of trying to figure out what's wrong I still get incorrect results for animation.

Firstly, I calculate Bind Pose and Inverse Bind Pose matrices for each bone:
[source lang="cpp"]Mat4x4 trans; trans.BuildTranslation(joints[i].posObjectSpace); //build translation from joint position in object spaceMat4x4 rot = joints[i].rot.BuildMatrix(); //joints[i].rot is unit quaternionMat4x4 bindPoseTransform = rot *trans;(**ppBones)[i].bindPoseTransform = rot * trans;(**ppBones)[i].inverseBindPoseTransform = bindPoseTransforms.Inverse();[/source]

After that I calculate vertex positions in object space and fill vertex buffer with positions, indices and weights
[source lang="cpp"]for (int j = 0; j < mesh.numVertices; j++) { //calculate position Vec3 pos(0.0f, 0.0f, 0.0f); md5_vertex vertex = mesh.vertices[j]; for (int k = 0; k < vertex.numWeights; k++) { md5_weight weight = mesh.weights[vertex.startWeight+k]; md5_joint joint = joints[weight.jointId]; //calculate vertex position in object space temp = Vec4(weight.posJointSpace); temp.w = 0.0f; Quaternion q = joint.rot; Quaternion revq = joint.rot.Inverse(); Vec3 temp3 = q * temp * revq; temp3 = temp3 + joint.posObjectSpace; temp3 *= weight.weight; pos += temp3;// * weight.weight; }//finally fill vertex structure with data v[j].pos = pos; v[j].texCoord = vertex.texCoords; for (int k = 0; k < vertex.numWeights; k++) { v[j].bonesIndices[k] = mesh.weights[vertex.startWeight+k].jointId; } v[j].bonesWeights = Vec4( vertex.numWeights > 0 ? mesh.weights[vertex.startWeight].weight : 0.0f, vertex.numWeights > 1 ? mesh.weights[vertex.startWeight+1].weight : 0.0f, vertex.numWeights > 2 ? mesh.weights[vertex.startWeight+2].weight : 0.0f, vertex.numWeights > 3 ? mesh.weights[vertex.startWeight+3].weight : 0.0f); assert( fabs(v[j].bonesWeights.x + v[j].bonesWeights.y + v[j].bonesWeights.z + v[j].bonesWeights.w - 1.0f) < 0.001f);[/source]
Next step is to calculate joints transforms in object space for each frame in animation:
[source lang="cpp"]//pos - current joint position relative to the parent (if have any)//rot - quaternion representng rotation of current joint//if we have parent joint... if (joint.id >= 0) { md5_joint parent = joints[joint.id]; //calculate joint pos Quaternion parentOrient = parent.rot; Vec4 temp( pos.x, pos.y, pos.z, 0.0f); pos = parentOrient * temp * parentOrient.Inverse(); pos = pos + parent.posObjectSpace; //concatenate rotation rot = parentOrient * rot'; rot.Normalize(); } //update joint position and orientation joints[j].posObjectSpace = pos; joints[j].rot = rot; Mat4x4 trans; trans.BuildTranslation(pos); Mat4x4 rotation = rot.BuildMatrix(); Mat4x4 transform = rotation * trans; newFrame.m_positions.push_back(pos); newFrame.m_rotations.push_back(rot); newFrame.m_transforms.push_back(transform); //use just fixed frames for debugging, no interpolation for now...[/source]
//Finally I render animation!
[source lang="cpp"]SkinningBuffer skinningBuffer;for (int j = 0; j < m_numBones; j++) { Mat4x4 world = pScene->GetWorld(); //world transform. identity for now Mat4x4 inverseBindPose = m_pBones[j].m_inverseBindPose; //inverse bind pose matrixMat4x4 curObjectSpace = m_pAnimations[0].GetTransform(j); //current object space transform for given bone skinningBuffer.bonesTransforms[j] = Transpose(inverseBindPose * curObjectSpace * world); } //next frame... m_pAnimations[0].NextFrame(); //update constant buffer for shader m_pSkinningBuffer->UpdateAndSet(&amp;amp;skinningBuffer, ST_Vertex, 3);[/source]

Here is my vertex shader code:
[source lang="cpp"]//////////////////////////////////////////////////Constant Buffers////////////////////////////////////////////////cbuffer MatrixBuffer : register(b0){ float4x4 m_ViewProjection;}cbuffer LightBuffer : register(b1){ float4 ambientColor : packoffset(c0); float4 diffuseColor : packoffset(c1); float3 lightDir : packoffset(c2.x); float padb1 : packoffset(c2.w);};cbuffer CameraBuffer : register(b2){ float3 eyePos : packoffset(c0.x); float padb2 : packoffset(c0.w);};cbuffer SkinningBuffer : register(b3){ float4x4 m_BoneWorld[50];};/////////////////////////////////////////////////Texture Resources and Sampler States///////////////////////////////////////////////Texture2D objTexture : register(t0);Texture2D bumpMap : register(t1);Texture2D heightMap : register(t2);SamplerState LinearSampler : register(s0);/////////////////////////////////////////////////Shaders' input/output layouts///////////////////////////////////////////////struct vs_input{ float3 pos : POSITION; uint4 bones : BONEIDS; float4 weights : BONEWEIGHTS; float2 texCoord : TEXCOORD; float3 normal : NORMAL; float3 tangent : TANGENT; float3 binormal : BINORMAL;};struct ps_input{ float4 pos : SV_POSITION; float2 texCoord : TEXCOORD; float4 color : COLOR; float3 normal : NORMAL; float3 tangent : TANGENT; float3 binormal : BINORMAL;};////////////////////////////////////////////////Vertex Shader//////////////////////////////////////////////ps_input VS(vs_input input){ ps_input output; //apply bone transforms to position output.pos = mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.x] * input.weights.x); output.pos += mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.y] * input.weights.y); output.pos += mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.z] * input.weights.z); output.pos += mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.w] * input.weights.w); output.pos = mul(output.pos, m_ViewProjection); //apply bone transforms to normal output.normal = mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.x]) * input.weights.x; output.normal += mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.y]) * input.weights.y; output.normal += mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.z]) * input.weights.z; output.normal += mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.w]) * input.weights.w; //apply bone transforms to tangent output.tangent = mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.x]) * input.weights.x; output.tangent += mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.y]) * input.weights.y; output.tangent += mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.z]) * input.weights.z; output.tangent += mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.w]) * input.weights.w; //apply bone transforms to normal output.binormal = mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.x]) * input.weights.x; output.binormal += mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.y]) * input.weights.y; output.binormal += mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.z]) * input.weights.z; output.binormal += mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.w]) * input.weights.w; //transfer texture coordinates output.texCoord = input.texCoord; //output result return output;}[/source]
I'm pretty sure, that vertex object space positions are calculated correctly and shaders code works ok as I get correct results for Bind Pose. But when animation starts it doesn't work:
Posted Image

Posted Image

The model is taken from http://www.katsbits.com/download/models/ .

To simplify debugging, I created simple model from 2 joints and 4 vertices with two frames of animation and still it doesn't work, but it seems that i checked everything...
Posted Image

This is capture of the second frame(of two). The first frame is rendering correctly showing this black plane lies straight on the floor. In the second frame I just have left part lifted up a bit. Obvisously, I renders incorrectly for me. I attached file with this plane to my post.

Could you give me some ideas why I get wrong result? If you already have implemented GPU skinning could you type your matrices for inverseBindPose of each bone and curObjectTransform of bones for each frame, so that i can compare them with mine(for that black plane model)? I would really appreciate any help!

Thanks!

PARTNERS