Problem with rendering a simple triangle

Started by
12 comments, last by galop1n 6 years, 8 months ago

Here is the code that is relevant for my problem. Everything else is omitted and is giving no problems.

 

The hlsl that compiles and later it successfully adds to the PSO:
 


struct VSInput
{
	float4 position : mPOSITION;
	float2 uv : mTEXCOORD;
};

struct PSInput
{
	float4 position : SV_POSITION;
	//float2 uv : TEXCOORD;
};

Texture2D g_texture : register(t0);
SamplerState g_sampler : register(s0);

PSInput VSMain(VSInput input)
{
	PSInput output;

	output.position = input.position;
	//output.uv = input.uv;

	return output;
}

float4 PSMain(PSInput input) : SV_TARGET
{
	//return g_texture.Sample(g_sampler, input.uv);
	return float4(1.0, 0.0, 0.0, 1.0);
}

 

The part of the C++ I consider relevant to the problem:
 


Vertex triangleVertices[] =
{
	{ { 0.0f, 0.25f, 0.0f }, { 0.5f, 0.0f } },
	{ { 0.25f, -0.25f, 0.0f }, { 1.0f, 1.0f } },
	{ { -0.25f, -0.25f, 0.0f }, { 0.0f, 1.0f } }
};

// FAILED macro is omited
D3DCompileFromFile(shadersPath.c_str(), nullptr, nullptr, "VSMain", "vs_5_0", 0, 0, &mvsByteCode, &errors);
D3DCompileFromFile(shadersPath.c_str(), nullptr, nullptr, "PSMain", "ps_5_0", 0, 0, &mpsByteCode, &errors);

D3D12_INPUT_ELEMENT_DESC mInputLayout[] =
{		
	{ "mPOSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
	{ "mTEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
};

renderQuadVertexBufferView.BufferLocation = mRenderQuadBufferDefault->GetGPUVirtualAddress();
renderQuadVertexBufferView.StrideInBytes = sizeof(Vertex);
renderQuadVertexBufferView.SizeInBytes = sizeof(triangleVertices);

mCommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
mCommandList->IASetVertexBuffers(0, 1, &renderQuadVertexBufferView);

// this command executes painting the screen well
mCommandList->ClearRenderTargetView(RTVHandleCPU, clearColor, 0, nullptr);

// this command does not show the triangle
mCommandList->DrawInstanced(3, 1, 0, 0);


Before to attempt to render the triangle, I set the state of the vertex buffer to be D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER. Its heap is of the type DEFAULT.
Do you see any problem in the shown code? If I can discard this as the source of the problem, I could search in other places.

Advertisement

Have you set your viewport and scissor? Have you tried PIX for windows or the D3D12 debug layer?

Viewport and scissors doesn't help. No I haven't tried PIX/debug. I'm testing each HRESULT and everything for NULL. And If something is wrong it doesn't compile here, it doesn't serialize there, doesn't initialize descriptors, it doesn't close the command list if something is wrong. So I decided to not make it more complex with more APIs for debugging.

Now i'm so sleepy... Tomorrow I will try to read the vertex buffer and see if it contains expected vertex data.

Your Z components are 0.0, maybe that causes the triangle to be clipped? Have you tried 0.5 or something?

.:vinterberg:.

Absolutely try enabling the debug layer and check the Visual Studio output (or whatever you are using) for warnings or errors. I have recently added DX12 support to my engine and the debug output helped me out a lot. If that does not help, run the app with RenderDoc and see that you transform the vertices correctly.

https://renderdoc.org/

I don't know what else to try...

is this correct?


D3D12_VIEWPORT viewPort = {};
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
viewPort.Width = TextureWidth;
viewPort.Height = TextureHeight;
viewPort.MinDepth = 0;
viewPort.MaxDepth = 1;

D3D12_RECT m_scissorRect = {};
m_scissorRect.left = 0;
m_scissorRect.top = 0;
m_scissorRect.right = TextureWidth;
m_scissorRect.bottom = TextureHeight;

tried with Z for the vertices 0.5 and nothing

Graphics debugging is not a guessing game. You need to run with all the debug tools you can, especially with d3d12 and inspect every elements for the mistake.

Enabling the debug layer prior to device creation is also pretty effective, many graphic issues won't trigger any HRESULT for example ( like improper descriptors in a heap or missing root parameters ).

PIX and Renderdoc are also very valuable once you have no validation errors and still not seeing what you should see.

 

In your case, i would look at the PSO creation parameters, like the output write mask and backface culling.

I would use debug for complex applications. I didn't expected when I started with such simple task to fall in such situation. I mean, it is a simple triangle! What I would need next for one single triangle?! Profiling performance tool?

I will rewrite it all from scratch and if the problem persists, I have no choice but try debugging tools too. :(

D3D12 is not for everyone. This is an API for the 1% of applications where D3D11 is not enough ! AAA games, large data set processing and heavy renderer tools.

If you are not an expert at D3D11 nor know why you need D3D12, you don't need it and will just shoot a bullet in your foot using it. D3D12 is not a replacement of D3D11, both are made for cohabitation, and it won't change.

Rendering a triangle with D3D12 is a complex application already, you have to deal with gpu/cpu sync and life time management, manual memory management, complex idioms like queues, allocator and barriers, etc. Add a texture to your triangle and you reach a whole world of non trivial decisions to your design.

You might also might trying to compile and run Microsoft DX12 samples on github.  See if they work... if they do just enable the debug layer. (which I think you should do anyway)

-potential energy is easily made kinetic-

This topic is closed to new replies.

Advertisement