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;
}
Edited by col_anderson, 10 October 2012 - 06:37 AM.