2014-03-12 89 views
0

我有一個錯誤,同時創造vertexbuffer 這裏是我的代碼:錯誤創建頂點緩衝

bool ColorShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename) 
{ 



HRESULT result; 
    ID3D10Blob* errorMessage; 
    ID3D10Blob* vertexShaderBuffer; 
    ID3D10Blob* pixelShaderBuffer; 
    D3D11_INPUT_ELEMENT_DESC polygonLayout[2]; 
    unsigned int numElements; 
    D3D11_BUFFER_DESC matrixBufferDesc; 


// Initialize the pointers this function will use to null. 
errorMessage = 0; 
vertexShaderBuffer = 0; 
pixelShaderBuffer = 0; 

// Compile the vertex shader code. 
result = D3DX11CompileFromFile(vsFilename, NULL, NULL, "ColorVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
           &vertexShaderBuffer, &errorMessage, NULL); 
if(FAILED(result)) 
{ 
    // If the shader failed to compile it should have writen something to the error message. 
    if(errorMessage) 
    { 
     OutputShaderErrorMessage(errorMessage, hwnd, vsFilename); 
    } 
    // If there was nothing in the error message then it simply could not find the shader file itself. 
    else 
    { 
     MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK); 
    } 

    return false; 
} 

// Compile the pixel shader code. 
result = D3DX11CompileFromFile(psFilename, NULL, NULL, "ColorPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, 
           &pixelShaderBuffer, &errorMessage, NULL); 
if(FAILED(result)) 
{ 
    // If the shader failed to compile it should have writen something to the error message. 
    if(errorMessage) 
    { 
     OutputShaderErrorMessage(errorMessage, hwnd, psFilename); 
    } 
    // If there was nothing in the error message then it simply could not find the file itself. 
    else 
    { 
     MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK); 
    } 

    return false; 
} 

// Create the vertex shader from the buffer. 
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, 
    &m_vertexShader); 
if(FAILED(result)) 
{ 
    return false; 
} 

// Create the pixel shader from the buffer. 
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, 
    &m_pixelShader); 
if(FAILED(result)) 
{ 
    return false; 
} 

// Create the vertex input layout description. 
// This setup needs to match the VertexType stucture in the ModelClass and in the shader. 
polygonLayout[0].SemanticName = "POSITION"; 
polygonLayout[0].SemanticIndex = 0; 
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 
polygonLayout[0].InputSlot = 0; 
polygonLayout[0].AlignedByteOffset = 0; 
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
polygonLayout[0].InstanceDataStepRate = 0; 

polygonLayout[1].SemanticName = "COLOR"; 
polygonLayout[1].SemanticIndex = 0; 
polygonLayout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 
polygonLayout[1].InputSlot = 0; 
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; 
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 
polygonLayout[1].InstanceDataStepRate = 0; 

// Get a count of the elements in the layout. 
numElements = sizeof(polygonLayout)/sizeof(polygonLayout[0]); 

// Create the vertex input layout. 
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), 
            vertexShaderBuffer->GetBufferSize(), &m_layout); 
if(FAILED(result)) 
{ 
    return false; 
} 

// Release the vertex shader buffer and pixel shader buffer since they are no longer needed. 
vertexShaderBuffer->Release(); 
vertexShaderBuffer = 0; 

pixelShaderBuffer->Release(); 
pixelShaderBuffer = 0; 

// Setup the description of the dynamic matrix constant buffer that is in the vertex shader. 
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType); 
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 
matrixBufferDesc.MiscFlags = 0; 
matrixBufferDesc.StructureByteStride = 0; 

// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class. 
result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer); 
if(FAILED(result)) 
{ 
    return false; 
} 

return true; 
} 

而且順便說一句的是,我從教程http://www.rastertek.com/dx11tut04.html去。你可以看到孔代碼那裏。 我聽說問題可能是,我的視頻卡不支持DirectX 11.如果這是問題,我可以使用功能級別11或類似的東西做軟件頂點處理,所以它可以工作。

+0

好的,我覺得它的硬件問題。所以問題仍然可以在筆記本上做這個東西,哪個硬件不支持DirectX 11? –

+0

至少你應該告訴我們你遇到了什麼錯誤。 – zdd

回答

0

如果您的顯卡不支持Direct3D 11功能,程序將在設備初始化時失敗,並且不會着色器創建,所以如果您使用D3D_FEATURE_LEVEL_11_0初始化設備並且成功,那意味着您的問題出現在着色器代碼中的某處。這很可能是因爲智能感知不適用於HLSL。

幸運的是,您的框架帶有一些錯誤報告功能,所以您只需查看shader_errors.txt文件即可查看問題所在。

如果上述情況並非如此(即你已經改變了你的Direct3D功能級別),那麼你有幾種選擇:

  1. 更改您的着色器模型的Direct3D功能級別在D3DX11CompileFromFile()方法匹配,例如如果您使用D3D_FEATURE_LEVEL_10_0,則將着色器型號設置爲vs_4_0ps_4_0

  2. 如果你使用的是Windows 8(不支持DX11的機器上有點不太可能),那麼你可以在你的設備初始化,這是一個快速軟件光柵,但在舊版本的Windows不使用D3D_DRIVER_TYPE_WARP支持11_0功能級別。

  3. 作爲最後的手段,您可以使用D3D_DRIVER_TYPE_REFERENCE,它支持所有Direct3D功能,但極其緩慢,無法使用。

最終總是有某種形式的情況下,你正運行在不支持您需要的功能級別的硬件後備選項的一個好主意。例如:

D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0; 

HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL, &m_deviceContext); 
if (FAILED(result)) 
{ 
    featureLevel = D3D_FEATURE_LEVEL_10_0; 
    result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL, &m_deviceContext); 
    if (FAILED(result)) 
    { 
     // OK, now quit, we need at least DirectX 10 compatible hardware 
     return false; 
    } 
} 

// In shader class 
if (m_device->GetFeatureLevel() == D3D_FEATURE_LEVEL_11_0) 
{ 
    // use 5.0 shader model 
} 
else 
{ 
    // use 4.0 shader model 
} 
+0

非常感謝您的回答,對於不清楚的問題表示抱歉。現在我再多解釋一下。問題是我可以做些什麼來使DirectX11在硬件上工作,但不支持它。我在另一臺計算機上運行我的程序,但該計算機支持directx11,所以它在那裏工作。此程序一直工作到着色器因爲我將功能級別更改爲directx 9.現在,我將檢查它是否適用於directX10。並且...是的,它的工作表示感謝。 –

+0

我記得您可以啓用硬件抽象層來模擬測試用軟件丟失的任何硬件。就我的筆記而言,您可以將DirectX設置爲使用「參考設備」類型,並在創建設備時將其設置爲第二個參數。我知道對於DirectX 9來說是這樣,查看它,至少如果你還有這些文件等,看看它已經有一段時間了。 – user3079666