DirectX : Write to texture with Compute Shader - c++

I'm trying to write in a texture with compute shader in HLSL.
Creation of the texture :
D3D11_TEXTURE2D_DESC textureDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = 512;
textureDesc.Height = 512;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
m_tex = 0;
hr = device->CreateTexture2D(&textureDesc, 0, &m_tex);
Creation of the UAV :
D3D11_UNORDERED_ACCESS_VIEW_DESC descUAV;
ZeroMemory(&descUAV, sizeof(descUAV));
descUAV.Format = DXGI_FORMAT_UNKNOWN;
descUAV.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
descUAV.Texture2D.MipSlice = 0;
hr = device->CreateUnorderedAccessView(m_tex, &descUAV, &m_uavAccess);
Creation of the SRV (to see the texture) :`
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
hr = device->CreateShaderResourceView(m_tex, &srvDesc, &m_srvTexOutput);
The shader :
RWTexture2D<float4> gOutput : register(u0);
[numthreads(16, 16, 1)]
void main(int3 dispatchThreadID : SV_DispatchThreadID) // Thread ID
{
gOutput[dispatchThreadID.xy] = float4(0.0f, 1.0f, 0.0f, 1.0f);
}
The problem is that the texture is always black (the compute shader doesn't write to the texture).
Thanks for your help ! :D

The problem is that I bound an UAV and a SRV on the same texture.
I resolved the problem creating two textures : one bound to an UAV and another bound to a SRV.
The compute shader write on the UAV and, each frame, I copy the data on the SRV with deviceContext->CopyResource(m_texSRV, m_texUAV);
The problem now is the performance, CopyResource is a costly operation. Does it have an other solution less expensive ?
Thanks for your help ! :D

The issue is that you need to cause the GPU to flush the surface before using it as an input in a different shader invocation (draw/PS). You do this by binding a null resource to the previous shader stage.
ID3D11UnorderedAccessView *NullUav = nullptr;
m_DeviceContext->CSSetUnorderedAccessViews(0, 1, &NullUav, nullptr);
This should be enough to tell the graphics driver to flush or transition the resource from a write to a read resource.
If you use D3D12 or vulkan you will need a resource barrier when going from write to read.
Example render loop:
// Run compute kernel output to UAV.
m_DeviceContext->CSSetShader(m_ComputeShader, NULL, 0);
m_DeviceContext->CSSetUnorderedAccessViews(0, 1, &m_UAV, nullptr);
m_DeviceContext->Dispatch(DispatchX, DispatchY, DispatchZ);
// Run the raster pipeline to present the UAV values to screen.
ID3D11UnorderedAccessView *NullUav = nullptr;
ID3D11ShaderResourceView* NullSrv = nullptr;
ID3D11DepthStencilView *NullDsv = nullptr;
m_DeviceContext->PSSetShader(m_PixelShader, NULL, 0);
m_DeviceContext->VSSetShader(m_VertexShader, NULL, 0);
m_DeviceContext->CSSetUnorderedAccessViews(0, 1, &NullUav, nullptr);
m_DeviceContext->PSSetShaderResources(0, 1, &m_SRV);
m_DeviceContext->IASetIndexBuffer(m_IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerState);
m_DeviceContext->OMSetRenderTargets(1, &m_RenderTarget, NullDsv);
m_ViewPort.Width = (float)RTWidth;
m_ViewPort.Height = (float)RTHeight;
m_ViewPort.MaxDepth = 1.0f;
m_DeviceContext->RSSetViewports(1, &m_ViewPort);
m_DeviceContext->DrawIndexed(4, 0, 0);
// Present the final result.
int Result = m_SwapChain->Present(1, 0);
if (Result == DXGI_ERROR_DEVICE_REMOVED || Result == DXGI_ERROR_DEVICE_RESET) {
// Reinitialize the renderer..
}
// Unset the SRV
m_DeviceContext->PSSetShaderResources(0, 1, &NullSrv);

Related

dx11 Rendering to texture shows only clearcolor

Im trying to create a game view for my game engine using ImGui framework,however when i try to render texture,there is only clear color displayed on the screen.
My plan
Creating Render Target Texture(I think i done this part right(i think))
Writing my scene into target texture(I presume this is where i failed,only clear color displayed on the screen,my test triangle not displayed)
Crearting a shader resource view(I tested this and the next step via loading image from the disk,it shows no problem here)
Display it on İmgui::Image()
Using render texture only clear color and Imgui Editor elements displayed on the screen when set back to normal rendering,my test triangle appears on the screen.
My Render Texture Creation
D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = 1920;
textureDesc.Height = 1080;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
DLE_Graphics::CurrentGraphics->pDevice->CreateTexture2D(&textureDesc, NULL, pTargetTexture.GetAddressOf());
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
DLE_Graphics::CurrentGraphics->pDevice->CreateShaderResourceView(this->pTargetTexture.Get(), &shaderResourceViewDesc, this->pResourceView.GetAddressOf());
// (re)-create the render target view
DLE_Graphics::CurrentGraphics->pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(pTargetTexture.GetAddressOf()));
DLE_Graphics::CurrentGraphics->pDevice->CreateRenderTargetView(pTargetTexture.Get(), &renderTargetViewDesc, pRenderTargetView.GetAddressOf());
// create the depth and stencil buffer
D3D11_TEXTURE2D_DESC dsd;
pTargetTexture->GetDesc(&dsd);
dsd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsd.Usage = D3D11_USAGE_DEFAULT;
dsd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
DLE_Graphics::CurrentGraphics->pDevice->CreateTexture2D(&dsd, NULL, pDepthTexture.GetAddressOf());
DLE_Graphics::CurrentGraphics->pDevice->CreateDepthStencilView(pDepthTexture.Get(), NULL, pDepthView.GetAddressOf());
// activate the depth and stencil buffer
DLE_Graphics::CurrentGraphics->pContext->OMSetRenderTargets(1, pRenderTargetView.GetAddressOf(), pDepthView.Get());
My Rendering Code
const FLOAT clr[] = { 0.0f, 0.5f, 0.5f, 0.0f };
pContext->ClearRenderTargetView(pRenderTexture->GetRenderView().Get(), clr);
pContext->ClearDepthStencilView(pRenderTexture->GetDepthView().Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 0u, 0u);
pContext->OMSetRenderTargets(1u, pRenderTexture->GetRenderView().GetAddressOf(), pRenderTexture->GetDepthView().Get());
pContext->DrawIndexed(size, 0u, 0u);
pSwapChain->Present(0u, 0u);
If anyone curious this is my swap chain description
DXGI_SWAP_CHAIN_DESC description = {};
description.BufferDesc.Width = 1920;
description.BufferDesc.Height = 1080;
description.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
description.BufferDesc.RefreshRate.Numerator = 60;
description.BufferDesc.RefreshRate.Denominator = 1;
description.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
description.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
description.SampleDesc.Count = 1;
description.SampleDesc.Quality = 0;
description.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT ;
description.BufferCount = 1;
description.OutputWindow = hWnd;
description.Windowed = TRUE;
description.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
description.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
When i use my render texture and create a resource view and use ImGui::Image to show there is no image,not even clear color.If i use my image on the disk for the ImGui::Image it displays the image,i dont what im doing wrong here
Here's my imgui code
ImGui::Begin("Spectrum Observer");
ImVec2 pos = ImGui::GetCursorScreenPos();
//ImGui::Image(texture->GetResource(), ImVec2(512, 512)); // my image from the disk
ImGui::Image(pGraphics->pRenderTexture->GetResourceView().Get(), ImVec2(512, 512)); // render texture
ImGui::End();
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
I appreciate every input possible,thank you
I managed to solve the triangle rendering problem via changing ClearDepthStencilView() 0u to 1u
const FLOAT clr[] = { 0.0f, 0.5f, 0.5f, 0.0f };
pContext->ClearRenderTargetView(pRenderTexture->GetRenderView().Get(), clr);
pContext->ClearDepthStencilView(pRenderTexture->GetDepthView().Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1u, 0u);
pContext->OMSetRenderTargets(1u, pRenderTexture->GetRenderView().GetAddressOf(), pRenderTexture->GetDepthView().Get());
pContext->DrawIndexed(size, 0u, 0u);
pSwapChain->Present(0u, 0u);
Appearently i was clearing the depth buffer to 0 which indicates all depth values nearest possiible which leads to failing ztest.

Rendering to texture - ClearRenderTargetView() works, but none objects are rendered to texture (rendering to screen works fine)

I try to render the scene to texture which should be then displayed in corner of the screen.
I though that I can do that this way:
Render the scene (my Engine::render() method that will set shaders and make draw calls) - works ok.
Change render target to the texture.
Render the scene again - does not work. The context->ClearRenderTargetView(texture->getRenderTargetView(), { 1.0f, 0.0f, 0.0f, 1.0f } ) does set my texture to red color (for scene in step 1. I use different color), but none objects are being rendered on it.
Change render target back to original.
Render the scene for the last time, with rectangle at corner that has the texture I've rendered in step 3. - works ok. I see the scene, the little rectangle in the corner too. The problem is, it's just red (something went wrong with rendering in step 3., I guess).
The result (there should be "image in image" instead of red rectangle):
The code for steps 2. - 4.:
context->OMSetRenderTargets(1, &textureRenderTargetView, depthStencilView);
float bg[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
context->ClearRenderTargetView(textureRenderTargetView, bg); //backgroundColor - red, green, blue, alpha
render();
context->OMSetRenderTargets(1, &myRenderTargetView, depthStencilView); //bind render target back to previous value (not to texture)
The render() method does not change (it works in step 1., why it doesn't work when I render to texture?) and ends with swapChain->Present(0, 0).
I know that ClearRenderTargetView affects my texture (without it, it's doesn't change color to red). But the rest of rendering either do not output to it or there's another problem.
Did I miss something?
I create the texture, shader resource view and render target for it based on this tutorial (maybe there is an error in my D3D11_TEXTURE2D_DESC?):
D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
//1. create render target
ZeroMemory(&textureDesc, sizeof(textureDesc));
//setup the texture description
//we will need to have this texture bound as a render target AND a shader resource
textureDesc.Width = size.getX();
textureDesc.Height = size.getY();
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;
//create the texture
device->CreateTexture2D(&textureDesc, NULL, &textureRenderTarget);
//2. create render target view
//setup the description of the render target view.
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
//create the render target view
device->CreateRenderTargetView(textureRenderTarget, &renderTargetViewDesc, &textureRenderTargetView);
//3. create shader resource view
//setup the description of the shader resource view.
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
//create the shader resource view.
device->CreateShaderResourceView(textureRenderTarget, &shaderResourceViewDesc, &texture);
The depth buffer:
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = sampleCount;
descDepth.SampleDesc.Quality = maxQualityLevel;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
And here goes the swap chain:
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = numerator; //60
sd.BufferDesc.RefreshRate.Denominator = denominator; //1
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = *hwnd;
sd.SampleDesc.Count = sampleCount; //1 (and 0 for quality) to turn off multisampling
sd.SampleDesc.Quality = maxQualityLevel;
sd.Windowed = fullScreen ? FALSE : TRUE;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; //allow full-screen switchin
// Set the scan line ordering and scaling to unspecified.
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
// Discard the back buffer contents after presenting.
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
I create the default render target view that way:
//create a render target view
ID3D11Texture2D* pBackBuffer = NULL;
result = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
ERROR_HANDLE(SUCCEEDED(result), L"The swapChain->GetBuffer() failed.", MOD_GRAPHIC);
//Create the render target view with the back buffer pointer.
result = device->CreateRenderTargetView(pBackBuffer, NULL, &myRenderTargetView);
After some debugging, as #Gnietschow suggested, I have found an error:
D3D11 ERROR: ID3D11DeviceContext::OMSetRenderTargets:
The RenderTargetView at slot 0 is not compatable with the
DepthStencilView. DepthStencilViews may only be used with
RenderTargetViews if the effective dimensions of the Views are equal,
as well as the Resource types, multisample count, and multisample
quality.
The RenderTargetView at slot 0 has (w:1680,h:1050,as:1), while the
Resource is a Texture2D with (mc:1,mq:0).
The DepthStencilView has
(w:1680,h:1050,as:1), while the Resource is a Texture2D with
(mc:8,mq:16).
So basically, my render target (texture) was not using anti-aliasing while my back buffer/depth buffer do.
I had to change SampleDesc.Count to 1 and SampleDesc.Quality to 0 in both DXGI_SWAP_CHAIN_DESC and D3D11_TEXTURE2D_DESC to match the values from texture to which I render. In other words I had to turn off anti-aliasing when rendering to texture.
I wonder, why render to texture does not support anti-aliasing? When I set SampleDesc.Count and SampleDesc.Quality to my standard values (8 and 16, those works fine on my GPU when rendering the scene) for my texture render target, the device->CreateTexture2D(...) fails with "invalid parameter" (even when I use those same values everywhere).

DirectX/C++: Texture Coordinates not "correct" in-engine correctly after Obj export

If I draw a single plane, the texture coordinates are mapped correctly. (4 Verts, 4 TC, 6 Indices(2 polys))
Even if it's subdivided, (9 Verts, 9 TC, 27 Indices(8 polys)) the texture will display:
(Maya complex plane)
I can take my [written] Obj converter and load the Texture coordinates into the buffer. However, if I extrude a face in Maya, and even applying a planar UV map to "repair" the broken uv's, (above) the texture coordinates get really wild in-engine. (below)
Is there an issue with Obj texture coordinate format?
Update: I've drawn with D3D11_PRIMITIVE_TOPOLOGY_LINELIST, and noticed a change in indexing as well.. Would this be the issue? or should I reconstruct texture coords as brushed on at http://www.gamedev.net/topic/600234-texcoord-count-is-different-than-vertex-count/
This issue was caused by the texture coordinates index not being equal to the vertex index. To resolve the problem, the texture coordinate index had to be ordered such as to line up with the vertex index.
For this issue, I was able to resolve using a brute force method that fit my understanding, which is slow, that catalogs the entire Vertex/Texture Coordinates as a key and reconstructs the full explicit array of vertices and Texture coordinates from their indices, filling a custom struct that fits the needs of my application.
There are other faster solutions that involve using a hash
http://programminglinuxgames.blogspot.com/2010/03/wavefront-obj-file-format-opengl-vertex.html
A related issue with normals:
OpenGL - Index buffers difficulties
And 3 index buffers
And a good explaination:
Why is my OBJ parser rendering meshes like this?
And further resources on obj format:
http://en.wikipedia.org/wiki/Wavefront_.obj_file
OBJ resources:
http://www.martinreddy.net/gfx/3d/OBJ.spec
http://www.fileformat.info/format/wavefrontobj/egff.htm
Also, the MeshFromObj10 Tutorial from the DirectX library helps some, getting it done in an efficient way. There's no easy way around coding it, outside of find a third party source.
Check out these things, it might help you:
DirectX uses left hadned coordinate systems. I think you will get a right handed coordinate system from Maya/3ds Max when you export to .obj files
://msdn.microsoft.com/en-us/library/windows/desktop/bb204853(v=vs.85).aspx
Compare your vertecies count, in Maya and then in your program.
Figure out the reason why you get 1.000671 in your TU/TV coordinates, this looks like it is a bit high.
Make sure you export as Triangles and not Rectangles.
Show your normals in Maya, it looks like some part of the ground/rectangle this object is standing on is missing. Or you could try to disable culling in your D3D11_RASTERIZER_DESC.
I don't have Maya installed on my computer right now, but i think you can get a graphical view in there that shows you the excact TU/TV coordinates on your texture.
http ://www.youtube.com/watch?v=T-fFpmBYP_Q That view is displayed in this video at 4,21.
EDIT:
Providing some code examples:
struct D3D11TextureVertexType
{
XMFLOAT3 Position;
XMFLOAT2 TX;
};
This is how you could put the vertices on the pipe:
hr = m_pd3dImmediateContext->Map(MyID3D11Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) return hr;
pData = (D3D11TextureVertexType*)mappedResource.pData;
memcpy(pData, MYOBJECTVERTICES/*this is D3D11TextureVertexType*/, sizeof(D3D11TextureVertexType) * VertexCount);
m_pd3dImmediateContext->Unmap(MyID3D11Buffer, 0);
stride = sizeof(D3D11TextureVertexType);
offset = 0;
m_pd3dImmediateContext->IASetVertexBuffers(0, 1, &MyID3D11Buffer, &stride, &offset);
m_pd3dImmediateContext->IASetIndexBuffer(m_AdjRectangleIBuffer, DXGI_FORMAT_R32_UINT, 0);
m_pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
result = m_TextureShader->Render(m_pd3dImmediateContext, 6, worldMatrix, viewMatrix, orthoMatrix, m_Textures[Q.textureID]->pSRV); if (!result)
{
return S_FALSE;
}
This is some intresting functions in the TextureShaderClass
bool Render(ID3D11DeviceContext* deviceContext, int indexCount, DirectX::CXMMATRIX worldMatrix, DirectX::CXMMATRIX viewMatrix, DirectX::CXMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
{
bool result;
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture);
if (!result)
{
return false;
}
RenderShader(deviceContext, indexCount);
return true;
}
bool InitializeShader(ID3D11Device* device, const WCHAR* filename)
{
HRESULT result;
ID3D10Blob* errorMessage;
ID3D10Blob* vertexShaderBuffer;
ID3D10Blob* pixelShaderBuffer;
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
unsigned int numElements;
D3D11_BUFFER_DESC matrixBufferDesc;
D3D11_SAMPLER_DESC samplerDesc;
errorMessage = 0;
vertexShaderBuffer = 0;
pixelShaderBuffer = 0;
result = D3DCompileFromFile(filename, NULL, NULL, "TextureVertexShader", "vs_5_0", 0, 0, &vertexShaderBuffer, &errorMessage);
if (FAILED(result))
{
if (errorMessage)
{
//OutputShaderErrorMessage(errorMessage, hwnd, filename);
}
else
{
MessageBox(0, filename, L"Missing Shader File", MB_OK);
}
return false;
}
result = D3DCompileFromFile(filename, NULL, NULL, "TexturePixelShader", "ps_5_0", 0, 0, &pixelShaderBuffer, &errorMessage);
if (FAILED(result))
{
if (errorMessage)
{
//OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
}
else
{
MessageBox(0, filename, L"Missing Shader File", MB_OK);
}
return false;
}
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
if (FAILED(result))
{
return false;
}
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
if (FAILED(result))
{
return false;
}
polygonLayout[0].SemanticName = "POSITION";
polygonLayout[0].SemanticIndex = 0;
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[0].InputSlot = 0;
polygonLayout[0].AlignedByteOffset = 0;
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[0].InstanceDataStepRate = 0;
polygonLayout[1].SemanticName = "TEXCOORD";
polygonLayout[1].SemanticIndex = 0;
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[1].InputSlot = 0;
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0;
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
if (FAILED(result))
{
return false;
}
vertexShaderBuffer->Release();
vertexShaderBuffer = 0;
pixelShaderBuffer->Release();
pixelShaderBuffer = 0;
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
matrixBufferDesc.MiscFlags = 0;
matrixBufferDesc.StructureByteStride = 0;
result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
if (FAILED(result))
{
return false;
}
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 0;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
if (FAILED(result))
{
return false;
}
return true;
}
bool SetShaderParameters(ID3D11DeviceContext* deviceContext, DirectX::CXMMATRIX worldMatrix, DirectX::CXMMATRIX viewMatrix, DirectX::CXMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
{
HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
MatrixBufferType* dataPtr;
unsigned int bufferNumber;
result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(result))
{
return false;
}
dataPtr = (MatrixBufferType*)mappedResource.pData;
DirectX::XMMATRIX world = worldMatrix;
world = XMMatrixTranspose(world);
DirectX::XMMATRIX view = viewMatrix;
view = XMMatrixTranspose(view);
DirectX::XMMATRIX projection = projectionMatrix;
projection = XMMatrixTranspose(projection);
dataPtr->world = world;
dataPtr->view = view;
dataPtr->projection = projection;
deviceContext->Unmap(m_matrixBuffer, 0);
bufferNumber = 0;
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
deviceContext->PSSetShaderResources(0, 1, &texture);
return true;
}
void RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
{
deviceContext->IASetInputLayout(m_layout);
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
deviceContext->DrawIndexed(indexCount, 0, 0);
return;
}
From Maya try re-exporting the texture after performing extrude for correct uv mapping.
Extruding creates new faces and existing uv mapping doesn't apply to those. Try using Geometry clean tool.
As far as I get you are editing the model after the UV map had been made and not updating the UV map to extruded object (which is still planner).
You can also try expert all option which includes the texture, not just the model in .obj
Haven't used Maya for a while though :(

Depth stencil not working - DirectX 10 C++

I have a DirectX10 + C++ problem.
Basically we're at the early stages of rendering, and for some reason our depth stencil seems to be failing to understand our model. Basically, here is everything we are doing:
Load shader, model and texture
Initialize DirectX
Draw
The model, shader and texture all load and work correctly, however (as shown in the screenshot below), the depth stencil is clearly not doing its job and the shader is being used in the wrong places. I have also included our initialization method in case you need it to figure it out. We believe we have tried almost everything but knowing our luck we have probably missed out 1 line of important code ^.^
We also saw that someone else had the same problem, however their fix didn't work (their problem was that they had set the near clipping plane to 0.0, however ours is not 0.0 so that is not the problem)
Thanks in advance!
Problem screenshot
void GraphicsDeviceDirectX::InitGraphicsDevice(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd; // create a struct to hold various swap chain information
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); // clear out the struct for use
scd.BufferCount = 2; // create two buffers, one for the front, one for the back
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Height = 600;
scd.BufferDesc.Width = 600;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // tell how the chain is to be used
scd.OutputWindow = hWnd; // set the window to be used by Direct3D
scd.SampleDesc.Count = 1; // set the level of multi-sampling
scd.SampleDesc.Quality = 0; // set the quality of multi-sampling
scd.Windowed = true; // set to windowed or full-screen mode
//set scan line ordering and scaling
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
//discard back buffer dontents
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
//dont set advanced flags
scd.Flags = 0;
// create a device class and swap chain class using the information in the scd struct
if(FAILED(D3D10CreateDeviceAndSwapChain(NULL,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
D3D10_CREATE_DEVICE_DEBUG,
D3D10_SDK_VERSION,
&scd,
&swapchain,
&device)))
{
throw EngineException("Error creating graphics device");
}
//Push graphics device to Persistant Object Manager
//PerObjMan::Push(device);
//Push swapchain to Peristant Object Manager
PerObjMan::Push(swapchain);
// get the address of the back buffer and use it to create the render target
ID3D10Texture2D* pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);
/*D3D10_TEXTURE2D_DESC descBack;
pBackBuffer->GetDesc(&descBack);*/
pBackBuffer->Release();
pBackBuffer = NULL;
//Push graphics device to Persistant Object Manager
PerObjMan::Push(rtv);
ID3D10Texture2D* pDepthStencil = NULL;
D3D10_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = 600;
descDepth.Height = 600;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
HRESULT hr;
hr = GetGraphicsDevice()->CreateTexture2D( &descDepth, NULL, &pDepthStencil );
if(FAILED(hr))
throw EngineException("FAIL");
PerObjMan::Push(pDepthStencil);
D3D10_DEPTH_STENCIL_DESC dsDesc;
ZeroMemory(&dsDesc, sizeof(dsDesc));
// Depth test parameters
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK::D3D10_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D10_COMPARISON_FUNC::D3D10_COMPARISON_LESS;
// Stencil test parameters
dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing.
dsDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing.
dsDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
// Create depth stencil state
hr = device->CreateDepthStencilState(&dsDesc, &dss);
if(FAILED(hr))
throw EngineException("FAIL");
// Bind depth stencil state
device->OMSetDepthStencilState(dss, 1);
PerObjMan::Push(dss);
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION::D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
// Create the depth stencil view
hr = device->CreateDepthStencilView( pDepthStencil, // Depth stencil texture
&descDSV, // Depth stencil desc
&dsv ); // [out] Depth stencil view
if(FAILED(hr))
throw EngineException("FAIL");
PerObjMan::Push(dsv);
// Bind the depth stencil view
device->OMSetRenderTargets( 1, // One rendertarget view
&rtv, // Render target view, created earlier
dsv); // Depth stencil view for the render target
D3D10_VIEWPORT viewport; // create a struct to hold the viewport data
ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT)); // clear out the struct for use
GameToImplement::GameInfo::Info info = GameToImplement::GameInfo::GetGameInfo();
RECT rect;
int width = 0;
int height = 0;
if(GetClientRect(hWnd, &rect))
{
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
else
{
throw EngineException("");
}
viewport.TopLeftX = 0; // set the left to 0
viewport.TopLeftY = 0; // set the top to 0
viewport.Width = 600; // set the width to the window's width
viewport.Height = 600; // set the height to the window's height
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
device->RSSetViewports(1, &viewport); // set the viewport
}
I fixed it, thanks to catflier's nod in the right direction. Turns out I was actually releasing the rasterizer state too early for the depth stencil to be used.
I'll leave this answer here for anyone who has the same problem.

DirectX Render to Texture

i am trying to render my Scene to a texture to use it in shaders for some post processing effect. I am not very experienced with this kind of stuff so i have some questions here..
First off all:
I am having this code in my OnCreateDevice() method:
D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = 800/2;
textureDesc.Height = 600/2;
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;
pd3dDevice->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
pd3dDevice->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetViewMap);
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
pd3dDevice->CreateShaderResourceView(renderTargetTextureMap, &shaderResourceViewDesc, &shaderResourceViewMap);
I am creating a texture, a renderView and a resourceView. But where do i go from here? Do i have to define another buffer or does this work after all?
Specifically,
So i think i know i have to set the OMRenderTargets with: renderTargetViewMap and depthStencilView. But where do i have to set this? In every frame render call? Before or after i rendered the other objects in my scene? and my guess would be that i have to make a new shader to which i give this texture created to work with it.
Thanks for any insight :/
You will have to set it each time you want to render to the texture, yes (and as long as you haven't switched to another render target or the back buffer). Once you've rendered the data to the texture, set up the shader that processes this data and draw a screen aligned quad with this texture.