0

我尝试使用 DXUT11 绘制一个简单的三角形,但在我的窗口中看不到三角形。
我努力了,但还是找不到问题出在哪里-_-

我检查了 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.
4

1 回答 1

2

问题解决了,谢谢大家~

我在“onRender”函数中清除了Z缓冲区和模板缓冲区,三角形终于出现了!!!DX SDK 2010 年 6 月使用 DirectX11 的教程 03 没有清除“onFrameRender”中的 Z 缓冲区,但显示三角形没有问题。也许我错过了一些细节

于 2013-04-14T08:07:48.617 回答