经过几个小时的努力来理解和弥补微软在Windows 8中对D3DX的弃用之后,我遇到了一个我无法摆脱的问题。
我第一次启动程序时,由于访问冲突而变得不朽。重新启动计算机并重新编译并运行相同的代码后,它不会崩溃,但我应该看到的立方体不存在。
我还得到一个警告,即在堆上声明的对象可能不会对齐16"。 AFAIK来自我的研究,这通常是因为XNA Math而发生的。因此,在找到this discussion之后,我尝试了所有建议的内容。除了XMFLOAT4X4解决方案之外的所有工作都有效,但我的立方体仍然是隐形的。以下是我认为相关的代码部分:
class IEGame : public DX11InfernalEngineBase
{
public:
//...
bool LoadContent()
{
//...
Vertex vertices[] =
{
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) }
};
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(vertexDesc));
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(VertexPos)*24;
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
resourceData.pSysMem = vertices;
try{
d3dresult = d3dDevice_->CreateBuffer(&vertexDesc, &resourceData, &vertexBuffer_);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Creating Vertex Buffer", MB_OK);
return false;
}
WORD indices[] = {
3, 1, 0, 2, 1, 3,
6, 4, 5, 7, 4, 6,
11, 9, 8, 10, 9, 11,
14, 12, 13, 15, 12, 14,
19, 17, 16, 18, 17, 19,
22, 20, 21, 23, 20, 22
};
D3D11_BUFFER_DESC indexDesc;
ZeroMemory(&indexDesc, sizeof(indexDesc));
indexDesc.Usage = D3D11_USAGE_DEFAULT;
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexDesc.ByteWidth = sizeof(WORD)* 36;
indexDesc.CPUAccessFlags = 0;
resourceData.pSysMem = indices;
try{
d3dresult = d3dDevice_->CreateBuffer(&indexDesc, &resourceData, &indexBuffer_);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Creating Index Buffer", MB_OK);
return false;
}
try{
std::vector<byte> textureFile = LoadFile("C:\\Users\\Marcus\\documents\\visual studio 2013\\Projects\\infernalEngine\\Debug\\guide.png");
d3dresult = CreateWICTextureFromMemory(d3dDevice_, d3dContext_, textureFile.data(), textureFile.size(), nullptr, &colorMapView_, 0);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Loading Texture", MB_OK);
return false;
}
D3D11_SAMPLER_DESC colorMapDesc;
ZeroMemory(&colorMapDesc, sizeof(colorMapDesc));
colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;
try{
d3dresult = d3dDevice_->CreateSamplerState(&colorMapDesc, &colorMapSampler_);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Creating Sampler State", MB_OK);
return false;
}
D3D11_BUFFER_DESC constDesc;
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof(XMMATRIX);
constDesc.Usage = D3D11_USAGE_DEFAULT;
try{
d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &viewCB_);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Creating View Matrix", MB_OK);
return false;
}
try{
d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &projCB_);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Creating Projection Matrix", MB_OK);
return false;
}
try{
d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &worldCB_);
if (FAILED(d3dresult))
{
throw _com_error(d3dresult);
}
}
catch (_com_error & comEx){
MessageBox(0, comEx.ErrorMessage(), "Error Creating World Matrix", MB_OK);
return false;
}
viewMatrix_ = XMMatrixIdentity();
projMatrix_ = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);
viewMatrix_ = XMMatrixTranspose(viewMatrix_);
projMatrix_ = XMMatrixTranspose(projMatrix_);
return true;
}
void UnloadContent()
{
if (colorMapSampler_) colorMapSampler_->Release();
if (colorMapView_) colorMapView_->Release();
if (solidColorVS_) solidColorVS_->Release();
if (solidColorPS_) solidColorPS_->Release();
if (inputLayout_) inputLayout_->Release();
if (vertexBuffer_) vertexBuffer_->Release();
if (viewCB_) viewCB_->Release();
if (projCB_) projCB_->Release();
if (worldCB_) worldCB_->Release();
colorMapSampler_ = 0;
colorMapView_ = 0;
solidColorVS_ = 0;
solidColorPS_ = 0;
inputLayout_ = 0;
vertexBuffer_ = 0;
viewCB_ = 0;
projCB_ = 0;
worldCB_ = 0;
}
void Render()
{
if (d3dContext_ == 0)
return;
float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);
d3dContext_->ClearDepthStencilView(depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0);
unsigned int nStride = sizeof(VertexPos);
unsigned int nOffset = 0;
d3dContext_->IASetInputLayout(inputLayout_);
d3dContext_->IASetVertexBuffers(0, 1, &vertexBuffer_, &nStride, &nOffset);
d3dContext_->IASetIndexBuffer(indexBuffer_, DXGI_FORMAT_R16_UINT, 0);
d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dContext_->VSSetShader(solidColorVS_, 0, 0);
d3dContext_->PSSetShader(solidColorPS_, 0, 0);
d3dContext_->PSSetShaderResources(0, 1, &colorMapView_);
d3dContext_->PSSetSamplers(0, 1, &colorMapSampler_);
XMMATRIX rotationMatrix;
rotationMatrix = XMMatrixRotationRollPitchYaw(0.0f, 0.7f, 0.7f);
XMMATRIX translationMatrix;
translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.6f);
XMMATRIX worldMatrix;
worldMatrix = rotationMatrix * translationMatrix;
worldMatrix = XMMatrixTranspose(worldMatrix);
d3dContext_->UpdateSubresource(worldCB_, 0, nullptr, &worldMatrix, 0, 0);
d3dContext_->UpdateSubresource(viewCB_, 0, nullptr, &viewMatrix_, 0, 0);
d3dContext_->UpdateSubresource(projCB_, 0, nullptr, &projMatrix_, 0, 0);
d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
d3dContext_->VSSetConstantBuffers(1, 1, &viewCB_);
d3dContext_->VSSetConstantBuffers(2, 1, &projCB_);
d3dContext_->DrawIndexed(36, 0, 0);
swapChain_->Present(0, 0);
}
private:
ID3D11VertexShader * solidColorVS_;
ID3D11PixelShader * solidColorPS_;
ID3D11InputLayout * inputLayout_;
ID3D11Buffer * vertexBuffer_;
ID3D11Buffer * indexBuffer_;
ID3D11ShaderResourceView * colorMapView_;
ID3D11SamplerState * colorMapSampler_;
ID3D11Buffer * viewCB_;
ID3D11Buffer * projCB_;
ID3D11Buffer * worldCB_;
XMMATRIX viewMatrix_;
XMMATRIX projMatrix_;
};
更新:因此,在启用D3D调试设备后,我在输出中得到了这个。我真的无法理解它。
D3D11 WARNING: Process is terminating. Using simple reporting. Please call ReportLiveObjects() at runtime for standard reporting. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Producer at 0x00F7F3F4, Refcount: 3. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x00F80218, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0403A110, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0403801C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04040304, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0404054C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0404089C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04040AAC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04040CE0, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04041394, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x040428E4, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04043534, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04043894, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0404490C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0404F5CC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0405C7EC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0405B65C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04069D0C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0406701C, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x040653AC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0408243C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04036FFC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0403719C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x04085C04, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x0408656C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object at 0x040618CC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Object : 25 [ STATE_CREATION WARNING #0: UNKNOWN]
DXGI WARNING: Live Producer at 0x00F4AE50, Refcount: 4. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live Object at 0x00F4D480, Refcount: 2. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live Object : 1 [ STATE_CREATION WARNING #0: ]
更新:我的XMMATRIX现已正确对齐。虽然立方体仍然是隐形的。我也找不到Visual Studio图形调试器。根据我的研究,我知道它应该在哪里,它就不存在。
答案 0 :(得分:0)
这里的关键问题是你使用的XMMATRIX
需要16字节对齐作为堆分配的类成员,但x86(32位)上的new
仅提供8字节默认情况下对齐。
这正是XMFLOAT4X4
,XMLoadFloat4x4
和XMStoreFloat4x4
存在要解决的问题。
XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);
XMStoreFloat4x4( &viewMatrix_, XMMatrixTranspose(vm) );
XMStoreFlaot4x4( &projMatrix_, XMMatrixTranspose(pm) );
XMFLOAT4X4 viewMatrix_;
XMFLOAT4X4 projMatrix_;
那或者您可以切换到x64(64位)本机编码,默认情况下具有16字节对齐,您可以使用XMMATRIX
或XMVECTOR
作为类成员而无需担心在大多数情况下都是对齐的。
这是在suspend the program by typing Ctrl-Z的MSDN上解决的。见DirectXMath Programming Guide
您还应该查看Getting Started, Type Usage Guidelines中的SimpleMath包装器。这些类型使用C ++&#34; magic&#34;:
处理此加载/存储行为#include <SimpleMath.h>
XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);
viewMatrix_ = XMMatrixTranspose(vm);
projMatrix_ = XMMatrixTranspose(pm);
DirectX::SimpleMath::Matrix viewMatrix_;
DirectX::SimpleMath::Matrix projMatrix_;
如果您还没有看到这些,请务必阅读:
编辑对于Windows桌面DirectX开发,如果您尚未使用VS 2013 Professional +,则应使用Visual Studio 2013 Community Edition。它包括VS Graphics Debugger,VS 2013 Express for Windows Desktop没有。
答案 1 :(得分:0)
好吧,我终于找到了解决方案。由于VS Graphics Debugger没有出错,我不得不做一些实际工作的随机事情。
我必须在任何可见之前创建一个光栅化器状态。事实证明我的立方体也在里面,所以我将D3D11_RASTERIZER_DESC的FrontCounterClockWise字段设置为false。我还通过在Render()中设置以下行来缩小相机:
nickname = models.CharField(unique=True, max_length=24)
biography = models.TextField(default=BIO_DEFAULT_STRING)
avatar = models.ImageField(upload_to='members/avatars/')
我还创造了一个剪刀矩形;我不知道这是否与解决方案有关。
这是我找到所有相关内容的地方: This MSDN page.
感谢大家的帮助,我使用内联函数清除了try-catch废话。