2013-04-13 149 views
0

我試圖用DXUT11繪製一個簡單的三角形,但我看不到我窗口中的三角形。
我努力工作,但仍然找不到問題出在哪裏-_-DirectX11繪製簡單的三角形

我已經檢查了D3D函數的每個返回值,並且它們都返回了S_OK。
我可以看到我的窗口顯示出來,背景色是我設置爲清除窗口的顏色, 因此它看起來d3d設備是正確創建的,並且後臺緩衝區已呈現到窗口 ,但是位於哪裏的黃色三角形?
T_T ...............

我的代碼如下:(爲了簡化問題,我已經刪除了所有的檢查S_OK句)

#define WIN32_LEAN_AND_MEAN 
#include <Windows.h> 
#include <d3d11.h> 
#define DXUT_AUTOLIB 
#include <DXUT.h> 
#include <DXUTres.h> 
#include <D3DX11.h> 
#include <xnamath.h> 
using namespace std; 

#pragma comment(lib,"d3d11.lib") 
#pragma comment(lib,"dxut.lib") 


ID3D10Blob * g_pVertexShaderBuffer = 0; 
ID3D10Blob * g_pPixelShaderBuffer = 0; 
ID3D11VertexShader * g_pVertexShader = 0; 
ID3D11PixelShader * g_pPixelShader = 0; 
ID3D11InputLayout * g_pInputLayout = 0; 

ID3D11Buffer * g_pVertexBuffer = 0; 
ID3D11Buffer * g_pIndexBuffer = 0; 

struct Vertex 
{ 
XMFLOAT3 pos; 
}; 


bool CALLBACK IsD3D11DeviceAcceptable(const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo, 
         DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext) 
{ 
    return true; 
} 

HRESULT CALLBACK onDeviceCreated(ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext) 
{ 
    ID3D11DeviceContext * pd3dImmediateContext = DXUTGetD3D11DeviceContext(); 
    ID3D10Blob * err = NULL; 
    D3DX11CompileFromFile(L"Tutorial03.fx",0,0,"VS","vs_4_0",D3DCOMPILE_DEBUG|D3DCOMPILE_SKIP_OPTIMIZATION,0,0,&g_pVertexShaderBuffer,&err,0); 
    D3DX11CompileFromFile(L"Tutorial03.fx",0,0,"PS","ps_4_0",D3DCOMPILE_DEBUG|D3DCOMPILE_SKIP_OPTIMIZATION,0,0,&g_pPixelShaderBuffer,&err,0); 

    HRESULT res = 0; 
    res = pd3dDevice->CreateVertexShader(g_pVertexShaderBuffer->GetBufferPointer(),g_pVertexShaderBuffer->GetBufferSize(),NULL,&g_pVertexShader); 
    res = pd3dDevice->CreatePixelShader(g_pPixelShaderBuffer->GetBufferPointer(),g_pPixelShaderBuffer->GetBufferSize(),NULL,&g_pPixelShader); 

    pd3dImmediateContext->VSSetShader(g_pVertexShader,0,0); 
    pd3dImmediateContext->PSSetShader(g_pPixelShader,0,0); 


    D3D11_INPUT_ELEMENT_DESC inputElement[]= 
    { 
    {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0, D3D11_INPUT_PER_VERTEX_DATA,0} 
    }; 

    int numElements = sizeof(inputElement)/sizeof(inputElement[0]); 
    pd3dDevice->CreateInputLayout(inputElement ,numElements,g_pVertexShaderBuffer->GetBufferPointer(),g_pVertexShaderBuffer->GetBufferSize(),&g_pInputLayout); 
    SAFE_RELEASE(g_pVertexShaderBuffer); 
    SAFE_RELEASE(g_pPixelShaderBuffer); 

    Vertex vertexData[3]; 
    vertexData[0].pos=XMFLOAT3(0.0f, 0.5f, 0.5f); 
    vertexData[1].pos=XMFLOAT3(0.5f, -0.5f, 0.5f); 
    vertexData[2].pos=XMFLOAT3(-0.5f, -0.5f, 0.5f); 

    D3D11_BUFFER_DESC desc; 
    ZeroMemory(&desc,sizeof(desc)); 
    desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 
    desc.ByteWidth = sizeof(Vertex) * 3; 
    desc.CPUAccessFlags = 0; 
    desc.MiscFlags = 0; 
    desc.Usage = D3D11_USAGE_DEFAULT; 

    D3D11_SUBRESOURCE_DATA dataPointer; 
    ZeroMemory(&dataPointer,sizeof(dataPointer)); 
    dataPointer.pSysMem = vertexData; 
    pd3dDevice->CreateBuffer(&desc,&dataPointer,&g_pVertexBuffer); 
    UINT stride = sizeof(Vertex); 
    UINT offset = 0; 
    pd3dImmediateContext->IASetVertexBuffers(0,1,&g_pVertexBuffer,&stride,&offset); 
    pd3dImmediateContext->IASetInputLayout(g_pInputLayout); 
    pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 
    return 0; 
} 

void CALLBACK onRender(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext) 
{ 
    float ClearColor[4] = { 0.0f, 0.25f, 0.25f, 0.55f }; 
    ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView(); 
    pd3dImmediateContext->ClearRenderTargetView(pRTV, ClearColor); 
    pd3dImmediateContext->Draw(3,0); 
} 

void CALLBACK onDestroyed(void* pUserContext) 
{ 
    SAFE_RELEASE(g_pIndexBuffer); 
    SAFE_RELEASE(g_pVertexBuffer); 
    SAFE_RELEASE(g_pInputLayout); 
    SAFE_RELEASE(g_pPixelShader); 
    SAFE_RELEASE(g_pVertexShader); 
} 

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) 
{ 

    DXUTSetCallbackD3D11DeviceAcceptable(IsD3D11DeviceAcceptable); 
    DXUTSetCallbackD3D11DeviceCreated(onDeviceCreated); 
    DXUTSetCallbackD3D11FrameRender(onRender); 
    DXUTSetCallbackD3D11DeviceDestroyed(onDestroyed); 

    DXUTInit(); 
    DXUTCreateWindow(L"SMD",hInstance); 
    DXUTCreateDevice(D3D_FEATURE_LEVEL_11_0,true,300,300); 
    DXUTMainLoop(); 
    return 0; 
} 

希望有一個人有耐心看完所有這些長碼-_-
和我着色器代碼:

float4 VS(float4 Pos : POSITION) : SV_POSITION 
{ 
    return Pos; 
} 
float4 PS(float4 Pos : SV_POSITION) : SV_Target 
{ 
    return float4(1.0f, 1.0f, 0.0f, 1.0f); // Yellow, with Alpha = 1 
} 

程序單獨運行良好,但是當我試圖用PIX跟蹤它,它墜毀並返回以下信息:

.PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 24, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 24, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 25, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 25, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 26, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 26, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 27, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 27, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 28, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 28, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 29, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 29, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 30, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 30, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 31, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 31, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 32, 0x008DE4FC) 
Frame 000001 ........POST: <S_OK><this=0x06736580> ID3D11Device::CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 32, 0x008DE4FC) 
Frame 000001 ........PRE: <this=0x06736580>ID3D11Device::Release() 
Frame 000001 ........POST: <1><this=0x06736580> ID3D11Device::Release() 
Frame 000001 ........PRE: <this=0x067ffa30>ID3D11DeviceContext::Release() 
Frame 000001 ............PRE: RemoveObject(D3D11 Device Context, 0x067FFA30, 0x00C5F11C) 
Frame 000001 ............POST: <> RemoveObject(D3D11 Device Context, 0x067FFA30, 0x00C5F11C) 
Frame 000001 ............PRE: RemoveObject(D3D11 Device, 0x06736580, 0x00C3F384) 
Frame 000001 ............POST: <> RemoveObject(D3D11 Device, 0x06736580, 0x00C3F384) 
Frame 000001 ............PRE: RemoveObject(DXGI Device, 0x0672AC80, 0x00C3F07C) 
Frame 000001 ............POST: <> RemoveObject(DXGI Device, 0x0672AC80, 0x00C3F07C) 
Frame 000001 ............PRE: RemoveObject(DXGI Adapter, 0x06736B40, 0x07651C68) 
Frame 000001 ............POST: <> RemoveObject(DXGI Adapter, 0x06736B40, 0x07651C68) 
Frame 000001 ............PRE: RemoveObject(DXGI Factory, 0x06729D08, 0x00C31828) 
Frame 000001 ............POST: <> RemoveObject(DXGI Factory, 0x06729D08, 0x00C31828) 
Frame 000001 ........POST: <0><this=0x067ffa30> ID3D11DeviceContext::Release() 
Frame 000001 ........PRE: <this=0x06775320>IDXGIFactory::EnumAdapters(2, 0x008DEA44) 
Frame 000001 ........POST: <DXGI_ERROR_NOT_FOUND><this=0x06775320> IDXGIFactory::EnumAdapters(2, 0x008DEA44) 
Frame 000001 ........PRE: D3DPERF_EndEvent() 
Frame 000001 ........POST: <-1> D3DPERF_EndEvent() 
Frame 000001 ........PRE: <this=0x0678df30>IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........POST: <S_OK><this=0x0678df30> IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........PRE: <this=0x0678df30>IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........POST: <S_OK><this=0x0678df30> IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........PRE: <this=0x0678df30>IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........POST: <S_OK><this=0x0678df30> IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........PRE: <this=0x0678df30>IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........POST: <S_OK><this=0x0678df30> IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........PRE: <this=0x0678df30>IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........POST: <S_OK><this=0x0678df30> IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........PRE: <this=0x0678df30>IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........POST: <S_OK><this=0x0678df30> IDXGIOutput::GetDesc(0x008DEB20) 
Frame 000001 ........PRE: <this=0x06775320>IDXGIFactory::MakeWindowAssociation(0x005305E0, 0) 
Frame 000001 ........POST: <S_OK><this=0x06775320> IDXGIFactory::MakeWindowAssociation(0x005305E0, 0) 
Frame 000001 ........PRE: <this=0x06775320>IDXGIFactory::EnumAdapters1(0, 0x008DEE18) 
Frame 000001 ............PRE: AddObject(DXGI Adapter, 0x067368D8, 0x07651C68) 
Frame 000001 ............POST: <TRUE> AddObject(DXGI Adapter, 0x067368D8, 0x07651C68) 
Frame 000001 ........POST: <S_OK><this=0x06775320> IDXGIFactory::EnumAdapters1(0, 0x008DEE18) 
Frame 000001 ........PRE: D3D11CreateDevice(0x067368D8, D3D_DRIVER_TYPE_UNKNOWN, NULL, 2, 0x00A21AA4, 1, 7, 0x008DEE60, 0x008DEE48, 0x008DEE54) 
Frame 000001 ............PRE: AddObject(D3D11 Device, 0x0672AB00, 0x00C3D4D4) 
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Device, 0x0672AB00, 0x00C3D4D4) 
Frame 000001 ............PRE: AddObject(DXGI Device, 0x0672A6E0, 0x00C3D4D0) 
Frame 000001 ............POST: <TRUE> AddObject(DXGI Device, 0x0672A6E0, 0x00C3D4D0) 
Frame 000001 ............PRE: AddObject(D3D11 Device Context, 0x06807880, 0x075C6800) 
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Device Context, 0x06807880, 0x075C6800) 
Frame 000001 ........POST: <S_OK> D3D11CreateDevice(0x067368D8, D3D_DRIVER_TYPE_UNKNOWN, NULL, 2, 0x00A21AA4, 1, 7, 0x008DEE60, 0x008DEE48, 0x008DEE54) 
Frame 000001 ........PRE: <this=0x0672ab00>ID3D11Device::QueryInterface(IID_IDXGIDevice1, 0x008DEE00) 
Frame 000001 ........POST: <S_OK><this=0x0672ab00> ID3D11Device::QueryInterface(IID_IDXGIDevice1, 0x008DEE00) 
Frame 000001 ........PRE: <this=0x0672a6e0>IDXGIDevice::Release() 
Frame 000001 ........POST: <2><this=0x0672a6e0> IDXGIDevice::Release() 
Frame 000001 ........PRE: <this=0x0672ab00>ID3D11Device::CreateRasterizerState(0x008DEDC4, 0x008DEDB8) 
Frame 000001 ............PRE: AddObject(D3D11 Rasterizer State, 0x0672B350, 0x00C14DD4) 
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Rasterizer State, 0x0672B350, 0x00C14DD4) 
Frame 000001 ........POST: <S_OK><this=0x0672ab00> ID3D11Device::CreateRasterizerState(0x008DEDC4, 0x008DEDB8) 
Frame 000001 ........PRE: <this=0x0672b350>ID3D11RasterizerState::SetPrivateData({0x429b8c22, 0x9188, 0x4b0c, 0x87, 0x42, 0xac, 0xb0, 0xbf, 0x85, 0xc2, 0x00}, 12, 0x01114E74) 
Frame 000001 ........POST: <S_OK><this=0x0672b350> ID3D11RasterizerState::SetPrivateData({0x429b8c22, 0x9188, 0x4b0c, 0x87, 0x42, 0xac, 0xb0, 0xbf, 0x85, 0xc2, 0x00}, 12, 0x01114E74) 
Frame 000001 ........PRE: <this=0x06807880>ID3D11DeviceContext::RSSetState(0x0672B350) 
Frame 000001 ........POST: <><this=0x06807880> ID3D11DeviceContext::RSSetState(0x0672B350) 
Frame 000001 ........PRE: <this=0x067368d8>IDXGIAdapter::EnumOutputs(0, 0x008DED94) 
Frame 000001 ............PRE: AddObject(DXGI Output, 0x06736A90, 0x0764B090) 
Frame 000001 ............POST: <TRUE> AddObject(DXGI Output, 0x06736A90, 0x0764B090) 
Frame 000001 ........POST: <S_OK><this=0x067368d8> IDXGIAdapter::EnumOutputs(0, 0x008DED94) 
Frame 000001 ........PRE: <this=0x06736a90>IDXGIOutput::Release() 
Frame 000001 ............PRE: RemoveObject(DXGI Output, 0x06736A90, 0x0764B090) 
Frame 000001 ............POST: <> RemoveObject(DXGI Output, 0x06736A90, 0x0764B090) 
Frame 000001 ........POST: <0><this=0x06736a90> IDXGIOutput::Release() 
Frame 000001 ........PRE: <this=0x067368d8>IDXGIAdapter::EnumOutputs(1, 0x008DED94) 
Frame 000001 ........POST: <DXGI_ERROR_NOT_FOUND><this=0x067368d8> IDXGIAdapter::EnumOutputs(1, 0x008DED94) 
Frame 000001 ........PRE: <this=0x067368d8>IDXGIAdapter::EnumOutputs(0, 0x00A21BD0) 
Frame 000001 ............PRE: AddObject(DXGI Output, 0x06736E00, 0x0764B090) 
Frame 000001 ............POST: <TRUE> AddObject(DXGI Output, 0x06736E00, 0x0764B090) 
Frame 000001 ........POST: <S_OK><this=0x067368d8> IDXGIAdapter::EnumOutputs(0, 0x00A21BD0) 
Frame 000001 ........PRE: <this=0x06775320>IDXGIFactory::CreateSwapChain(0x0672AB00, 0x00A21A54, 0x008DEE3C) 
Frame 000001 ............PRE: AddObject(DXGI Swap Chain, 0x0672B3C8, 0x00C3F048) 
Frame 000001 ............POST: <TRUE> AddObject(DXGI Swap Chain, 0x0672B3C8, 0x00C3F048) 
Frame 000001 ............PRE: AddObject(DXGI Surface, 0x0672ACE0, 0x074C4D80) 
Frame 000001 ............POST: <TRUE> AddObject(DXGI Surface, 0x0672ACE0, 0x074C4D80) 
Frame 000001 ............PRE: AddObject(D3D10 Texture2D, 0x067755C0, 0x074C4D28) 
Frame 000001 ............POST: <TRUE> AddObject(D3D10 Texture2D, 0x067755C0, 0x074C4D28) 
Frame 000001 ........POST: <S_OK><this=0x06775320> IDXGIFactory::CreateSwapChain(0x0672AB00, 0x00A21A54, 0x008DEE3C) 
Frame 000001 ........PRE: <this=0x0672b3c8>IDXGISwapChain::GetBuffer(0, IID_ID3D11Texture2D, 0x008DEC2C) 
Frame 000001 ........POST: <S_OK><this=0x0672b3c8> IDXGISwapChain::GetBuffer(0, IID_ID3D11Texture2D, 0x008DEC2C) 
Frame 000001 ........PRE: <this=0x067755c0>ID3D10Texture2D::Map(9300960, Unknown D3D10_MAP, 9301104, NULL) 
D3D11 CORRUPTION: ID3D10Texture2D::Map: Fourth parameter is corrupt or NULL. [ MISCELLANEOUS CORRUPTION #16: CORRUPTED_PARAMETER4] 
An unhandled exception occurred. 
+0

只是一個建議,因爲我沒有時間完整閱讀代碼 - 您的訂單是否正確?嘗試交換兩個頂點。 – OlduwanSteve

+0

感謝您的建議,但我已經嘗試過並注意到新的事情發生了...... – Wood

+0

您是否在運行時啓用了調試功能?如果你這樣做,你可能會在調試輸出中得到一個有用的錯誤。 – OlduwanSteve

回答

2

問題解決了,謝謝大家〜

我清除Z緩存和模板緩衝區「的OnRender」功能,和三角形終於出現了! 使用DirectX11的2010年6月的DX SDK中的教程03不會清除「onFrameRender」中的Z緩衝區,但它在顯示三角形時沒有問題。也許有一些細節我錯過了

+0

是的 - 直到教程05才添加深度緩衝區。 :) – Slagh