Where is my triangle? Issue moving to Directx 11 and Metro

Started by
27 comments, last by AlzPatz 11 years, 5 months ago
Thanks Spiro. have tried reverse winding. Will try out PIX when I get home. DX DebugMode is turned on, no funny messages.
Advertisement

I'm not sure if this will help but it seems to me your input layout defines vertex position as a 3 element vector (DXGI_FORMAT_R32G32B32_FLOAT), while your vertex shader parameter is expecting a 4 element one (float4 pos). I'd suggest you try and change the vertex shader parameter to float3 and see if that helps!


Using DXGI_FORMAT_R32G32B32_FLOAT is perfectly valid. The vertex shader will receive the value as [x,y,z,1.0f] automatically.

Cheers!
so..turns out PIX has been replaced by Graphics Diagnostics integrated into Visual Studio 2012. Hurray I thought, until it turns out that it's only available on versions higher than 'express', so that's a £500+ purchase to use graphics debugging on Visual Studio 2012. soul destroying. Not sure anyway around this. I cant download any trial versions etc as I am running RC and some version of windows 8, it's all gone to sh1t until the real version of windows 8 is released.
i would also add that I have no problems doing this simple drawing in DirectX 11 and C++ in windows 7 / win 32. Im at a loss, completely.
The problem you are describing sound like the exact same problem when I try to render a cube.

Inspecting the problem with the Graphics Diagnosis Tool tells me that the Pixel Shader has not participate in the graphics pipeline process, in my case, without any obvious reason.

I have double checked it, but it was loaded properly and set correctly in the device context.
This leaves me clueless.

Perhaps the additional diag info helps in your case as well, AlzPatz. (see attachment)

And this is the corresponding source code

Cube.cpp:

#include "Cube.h"
#include "GlobalDefines.h"
#include "Direct3dUtils.h"
struct SChangesEveryFrame{
XMMATRIX mWorld;
};
Cube::Cube(void)
:isInitialized(false),
vertexBuffer(NULL),
indexBuffer(NULL),
cbChangesEveryFrame(NULL),
vertexShaderFile(L"SimpleVertexShader"),
pixelShaderFile(L"SimplePixelShader"),
vertexShader(NULL),
pixelShader(NULL),
inputLayout(NULL)
{
}

Cube::~Cube(void)
{
SAFE_RELEASE(inputLayout);
SAFE_RELEASE(pixelShader);
SAFE_RELEASE(vertexShader);
SAFE_RELEASE(cbChangesEveryFrame);
SAFE_RELEASE(vertexBuffer);
SAFE_RELEASE(indexBuffer);
}
void Cube::Render()
{
if(isInitialized){
SChangesEveryFrame sChangesEveryFrame;
//using XMMATRIX causes runtime error - better use XMFloat4x4
//http://www.gamedev.net/topic/627033-xmmatrixtranspose-crashes/
//http://xboxforums.create.msdn.com/forums/t/84299.aspx
sChangesEveryFrame.mWorld = XMMatrixTranspose(XMLoadFloat4x4(&worldMatrix));

UINT stride =sizeof(SimpleColorVertex) ;
UINT offset = 0;

immediateContext->IASetInputLayout(inputLayout);
immediateContext->IASetVertexBuffers(0,1,&vertexBuffer,&stride,&offset);
immediateContext->IASetIndexBuffer(indexBuffer,DXGI_FORMAT_R16_UINT,0);
immediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

immediateContext->UpdateSubresource(cbChangesEveryFrame,0,NULL,&sChangesEveryFrame,0,0);
immediateContext->VSSetConstantBuffers(2,1,&cbChangesEveryFrame);
//immediateContext->PSSetConstantBuffers(2,1,&cbChangesEveryFrame);
immediateContext->VSSetShader(vertexShader, NULL,0);
immediateContext->PSSetShader(pixelShader, NULL,0);
immediateContext->DrawIndexed( 36, 0, 0 );
}
}
void Cube::Initialize(ID3D11Device* device, ID3D11DeviceContext* immediateContext)
{
this->device = device;
this->immediateContext = immediateContext;

SimpleColorVertex vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT4( 0.0f, 0.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT4( 0.0f, 1.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT4( 0.0f, 1.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT4( 1.0f, 0.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT4( 1.0f, 1.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f ) }
};
WORD indices[] =
{
3,1,0,
2,1,3,

0,5,4,
1,5,0,

3,4,7,
0,4,3,

1,6,5,
2,6,1,

2,7,6,
3,7,2,

6,4,5,
7,4,6,
};
HRESULT hr;
vertexBuffer = Direct3dUtils::CreateBuffer(device,D3D11_BIND_VERTEX_BUFFER,sizeof(SimpleColorVertex) * ARRAYSIZE(vertices),vertices,hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"create vertexbuffer");
return;
}
indexBuffer = Direct3dUtils::CreateBuffer(device,D3D11_BIND_INDEX_BUFFER,sizeof(WORD) * ARRAYSIZE(indices),indices,hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"create indexbuffer");
return;
}
cbChangesEveryFrame = Direct3dUtils::CreateBuffer(device,D3D11_BIND_CONSTANT_BUFFER,sizeof(SChangesEveryFrame),NULL,hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"create cbChangesEveryFrame");
return;
}
D3D11_INPUT_ELEMENT_DESC layoutDesciption[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,0 ,0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR" , 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
Direct3dUtils::LoadVertexShader(device, vertexShaderFile, layoutDesciption,ARRAYSIZE(layoutDesciption), &vertexShader, &inputLayout, hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"LoadVertexShader");
return;
}
Direct3dUtils::LoadPixelShader(device, pixelShaderFile,&pixelShader, hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"LoadPixelShader");
return;
}
XMStoreFloat4x4(&worldMatrix,XMMatrixIdentity());
isInitialized = true;
}


Direct3d.cpp

#include "Direct3d.h"
struct SNeverChanges{
XMMATRIX mView;
};
struct SChangesOnResize{
XMMATRIX mProjection;
};
Direct3d::Direct3d(HWND hwnd)
{
// TODO set up configuration class to determine proper system configuration, for now we go with the dev system requirments

IDXGIAdapter* adapter = NULL;
D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_HARDWARE;
// TODO allow multiple display modes, including full-screen resolutions - for now we go with windowed 1024x768x60hz

int resolutionWidth = WINDOWWITDH;
int resolutionHeight = WINDOWHEIGHT;
int refreshrate = 60;
bool windowed = true;
int multisamplingCount = 1;
int mulitsamplingQuality = 0;
UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof(sd) );
sd.BufferCount = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferDesc.RefreshRate.Numerator = refreshrate;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.Width = resolutionWidth;
sd.BufferDesc.Height = resolutionHeight;
sd.Windowed = windowed;
sd.SampleDesc.Count = multisamplingCount;
sd.SampleDesc.Quality = mulitsamplingQuality;
sd.OutputWindow = hwnd;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
int numberOfFeatureLevels = ARRAYSIZE(featureLevels);
HRESULT hr;
hr = D3D11CreateDeviceAndSwapChain(adapter, driverType, NULL, createDeviceFlags, featureLevels, numberOfFeatureLevels, D3D11_SDK_VERSION, &sd, &swapChain, &device, NULL, &immediateContext );
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult( hr, L"CreateDevice&SwapChain" );
return;
}
hr = swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (void**) &backBuffer );
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult( hr, L"SwapChain-GetBuffer" );
return;
}

hr = device->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
backBuffer->Release();
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult( hr, L"CreateRenderTargetView" );
return;
}

D3D11_TEXTURE2D_DESC stencilDescription;
ZeroMemory(&stencilDescription,sizeof(stencilDescription));
stencilDescription.Usage = D3D11_USAGE_DEFAULT;
stencilDescription.Width = resolutionWidth;
stencilDescription.Height = resolutionHeight;
stencilDescription.MipLevels = 1;
stencilDescription.ArraySize = 1;
stencilDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
stencilDescription.SampleDesc.Count = 1;
stencilDescription.SampleDesc.Quality = 0;
stencilDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL;
stencilDescription.CPUAccessFlags = 0;
stencilDescription.MiscFlags = 0;
hr = device->CreateTexture2D(&stencilDescription,NULL, &depthStencilBuffer);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult( hr, L"CreateDepthStencilBuffer" );
return;
}

D3D11_DEPTH_STENCIL_VIEW_DESC viewDescription;
ZeroMemory(&viewDescription,sizeof(viewDescription));
viewDescription.Format = stencilDescription.Format;
viewDescription.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
hr = device->CreateDepthStencilView(depthStencilBuffer,&viewDescription,&depthStencilView);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult( hr, L"CreateDepthStencilView" );
return;
}

immediateContext->OMSetRenderTargets( 1, &renderTargetView, depthStencilView);
D3D11_VIEWPORT vp;
vp.Width = float(resolutionWidth);
vp.Height = float(resolutionHeight);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
immediateContext->RSSetViewports( 1, &vp );
XMVECTOR eye = XMVectorSet(0.0f, 3.0f, -2.0f, 0.0f);
XMVECTOR at = XMVectorSet(0.0f,1.0f,1.0f,0.0f);
XMVECTOR up = XMVectorSet(0.0f,1.0f,1.0f,0.0f);
XMStoreFloat4x4(&ViewMatrix,XMMatrixLookAtLH(eye,at,up));
SNeverChanges sNeverChanges;
sNeverChanges.mView = XMLoadFloat4x4(&ViewMatrix);
cbNeverChanges = Direct3dUtils::CreateBuffer(device,D3D11_BIND_CONSTANT_BUFFER,sizeof(SNeverChanges),NULL,hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"create cbNeverChanges");
return;
}
immediateContext->UpdateSubresource( cbNeverChanges, 0, NULL, &sNeverChanges, 0, 0 );
XMStoreFloat4x4(&ProjectionMatrix,XMMatrixPerspectiveFovLH(XM_PIDIV2,resolutionWidth/(float)resolutionHeight,0.01f,1.0f));
SChangesOnResize sChangesOnResize;
sChangesOnResize.mProjection = XMLoadFloat4x4(&ProjectionMatrix);
cbChangesOnResize = Direct3dUtils::CreateBuffer(device,D3D11_BIND_CONSTANT_BUFFER,sizeof(SChangesOnResize),NULL,hr);
if (FAILED(hr)){
Direct3dUtils::ShowMessageBoxOnBadResult(hr,L"create cbNeverChanges");
return;
}
immediateContext->UpdateSubresource( cbChangesOnResize, 0, NULL, &sChangesOnResize, 0, 0 );
}
Direct3d::~Direct3d(void)
{
if (immediateContext) immediateContext->ClearState();
SAFE_RELEASE(cbNeverChanges);
SAFE_RELEASE(cbChangesOnResize);
SAFE_RELEASE(depthStencilView);
SAFE_RELEASE(renderTargetView);
SAFE_RELEASE(backBuffer);
SAFE_RELEASE(immediateContext);
SAFE_RELEASE(swapChain);
SAFE_RELEASE(device);
}
void Direct3d::Render()
{
float color[] = { 0.0f, 0.2f, 0.0f, 1.0f };
immediateContext->ClearRenderTargetView(renderTargetView, color);
immediateContext->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
immediateContext->VSSetConstantBuffers(1,1,&cbChangesOnResize);
immediateContext->VSSetConstantBuffers(0,1,&cbNeverChanges);
renderObjects();
swapChain->Present( 0, 0 );
}
void Direct3d::Add(Renderable* object)
{
object->Initialize(device,immediateContext);
objects.insert(objects.end(),object);
}

void Direct3d::renderObjects()
{
for (std::vector<Renderable*>::iterator iter = objects.begin(); iter != objects.end(); iter++)
(*iter)->Render();
}


My shaders are pretty basic as well:

definitions.fx:

cbuffer cbNeverChanges : register(b0) {
matrix View;
};
cbuffer cbChangesOnResize: register(b1) {
matrix Projection;
};
cbuffer cbChangesEveryFrame : register (b2) {
matrix World;
};
struct VS_INPUT{
float4 Pos : POSITION;
float4 Color : COLOR0;
};
struct PS_INPUT{
float4 Pos : SV_POSITION;
float4 Color : COLOR0;
};


SimplePixelShader.hlsl:

#include "definitions.fx"
float4 main(PS_INPUT input) : SV_Target
{
return input.Color;
}

SimpleVertexShader.hlsl:

#include "definitions.fx"

PS_INPUT main( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;
output.Pos = mul(input.Pos,World);
output.Pos = mul(output.Pos, View);
output.Pos = mul(output.Pos, Projection);
output.Color = input.Color;
return output;
}
Hi col_andersen.
Same as me, I do not see anything obvious in the code. You are using the Jun2010 SDK? So no targeting metro?
I could be getting the same effect. Any chance I could send you a simple solution zip file for you to run through the diagnostics to check if the same?
I solved the issue for me, but you seem to use untransformed screen coordinates.

The root cause with my code were the Projection and View matrices.

While I stored the world matrix correctly:
sChangesEveryFrame.mWorld = XMMatrixTranspose(XMLoadFloat4x4(&worldMatrix));

I did not do it with the other two:
sNeverChanges.mView = XMLoadFloat4x4(&ViewMatrix);
and
sChangesOnResize.mProjection = XMLoadFloat4x4(&ProjectionMatrix);

The shader has to use a transposed version of the matrices.

AlzPatz - I will get in touch with you via PM.
thanks - yes, i'm just trying to put a triangle on screen as a simple test lol.
AlzPatz did you setup a view and projection matrix?
Hi - no i do not. I didn't think I needed to if my vertices were already in "Screen Space", or Homogenous clip space, or whatever (-1,-1, z to 1, 1,z). maybe i got wrong end of the stick.

This topic is closed to new replies.

Advertisement