Skip to content

Instantly share code, notes, and snippets.

@Cody-Duncan
Created July 8, 2015 03:23
Show Gist options
  • Save Cody-Duncan/81afe893d6e7d8056b64 to your computer and use it in GitHub Desktop.
Save Cody-Duncan/81afe893d6e7d8056b64 to your computer and use it in GitHub Desktop.
// ##### ComputeShader.hlsl #####
Texture2D TextureInput : register(t0); // Input texture to read from
RWTexture2D<float4> TextureOutput : register(u0); // Temp output
void main( uint3 DTid : SV_DispatchThreadID, uint3 groupID: SV_GroupID, uint3 groupThreadID: SV_GroupThreadID )
{
float4 vColor = TextureInput[int2(groupThreadID.x, groupID.y)];
// do stuff to vColor
// Store result
TextureOutput[int2(groupThreadID.x,groupID.y)]=vColor;
}
//##### Renderer.cpp #####
// ---------- Bind Shader Resource View ---------
// ---------- Bind Shader Resource View ---------
D3D11_SHADER_RESOURCE_VIEW_DESC actualShaderResDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC* shaderDesc = nullptr;
if(resDesc.Format == DXGI_FORMAT_R24G8_TYPELESS || resDesc.Format == DXGI_FORMAT_R32_TYPELESS) //typeles depth buffer, used as both shader and depthstencil
{
ZeroMemory(&actualShaderResDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
actualShaderResDesc.Format = resDesc.Format;
if(actualShaderResDesc.Format == DXGI_FORMAT_R24G8_TYPELESS)
{
actualShaderResDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
}
else if(actualShaderResDesc.Format == DXGI_FORMAT_R32_TYPELESS)
{
actualShaderResDesc.Format = DXGI_FORMAT_R32_FLOAT;
}
actualShaderResDesc.Texture2D = {0, -1}; //forgot why these args
actualShaderResDesc.ViewDimension = D3D_SRV_DIMENSION::D3D_SRV_DIMENSION_TEXTURE2D;
shaderDesc = &actualShaderResDesc;
}
HRESULT hr = m_device->CreateShaderResourceView(d3dtexture, shaderDesc, &texture->pShaderResourceView); // (ID3D11Texture2D*, D3D11_SHADER_RESOURCE_VIEW_DESC* ID3D11ShaderResourceView**)
ASSERT_DEBUG(SUCCEEDED(hr), "FAILED: Failed to create Shader Resource View.");
char debugSRVName[80];
strcpy_s(debugSRVName, (resDesc.Filename + ": ShaderResourceView").c_str());
DirectX::SetDebugObjectName(texture->pSRV, debugSRVName);
if (resDesc.GenerateMips)
{
m_deviceContext->UpdateSubresource(d3dtexture, 0, nullptr, data->pData, data->RowPitch, 1);
m_deviceContext->GenerateMips(texture->pShaderResourceView);
}
// ---------- Bind Unordered Access View ---------
D3D11_UNORDERED_ACCESS_VIEW_DESC descUAV;
ZeroMemory(&descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC));
descUAV.Format = resDesc.Format; //DXGI_FORMAT, often DXGI_FORMAT::DXGI_FORMAT_R32_FLOAT;
descUAV.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
descUAV.Texture2D.MipSlice = 0;
hr = m_device->CreateUnorderedAccessView(d3dtexture, &descUAV, &texture->pUAV); // (ID3D11Texture2D*, D3D11_UNORDERED_ACCESS_VIEW_DESC* ID3D11UnorderedAccessView**)
ASSERT_DEBUG(SUCCEEDED(hr), "Failed to create Unordered Access View.\n");
//For debugging purposes
char debugUnorderedAccessName[80];
strcpy_s(debugUnorderedAccessName, (Filename + ": UnorderedAccessView").c_str());
DirectX::SetDebugObjectName(texture->pUAV, debugUnorderedAccessName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment