顶点着色器创建 [英] Vertex Shader Creating

查看:158
本文介绍了顶点着色器创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习D3D,目前我在着色器中。我编写并构建了代码-成功,但是在调试时出现此错误:

I am learning D3D and currently i am at shaders. I wrote the code and built it - succeed but when debug i get this error:

错误

以下是调试器中断的行:

Here is the line where debugger breake:

dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);

这是整个函数:

void InitPipeline()
{
    // load and compile the two shaders
    ID3D10Blob *VS, *PS;
    D3DX11CompileFromFile("Shaders.shader", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &VS, 0, 0);
    D3DX11CompileFromFile("Shaders.shader", 0, 0, "PShader", "ps_4_0", 0, 0, 0, &PS, 0, 0);

    // encapsulate both shaders into shader objects
    dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
    dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);

    // set the shader objects
    devcon->VSSetShader(pVS, 0, 0);
    devcon->PSSetShader(pPS, 0, 0);

    // create the input layout object
    D3D11_INPUT_ELEMENT_DESC ied[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };

    dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
    devcon->IASetInputLayout(pLayout);
}


推荐答案

(1) 您没有检查任何HRESULT值是否失败。对于任何COM API使用(包括Direct3D),这都是必不可少的编码步骤。您很有可能在compile语句中失败,因此VS返回null。

(1) You are not checking any of the HRESULT values for failure. For any COM API usage, including Direct3D, this is an essential coding step. Most likely you are getting a failure in the compile statements, so VS is coming back null.

您可以使用 FAILED SUCCEEDED 宏直接执行错误处理,或者 DX :: ThrowIfFailed 在现代C ++示例中使用的帮助程序。

You can use the FAILED and SUCCEEDED macros directly to do the error handling, or the DX::ThrowIfFailed helper used in modern C++ samples.

if (FAILED(D3DX11CompileFromFile("Shaders.shader", nullptr, nullptr, "VShader", "vs_4_0",
    0, 0, nullptr, &VS, nullptr, nullptr)))
    // error
if (FAILED(D3DX11CompileFromFile("Shaders.shader", nullptr, nullptr, "PShader", "ps_4_0",
    0, 0, nullptr, &PS, nullptr, nullptr)))
    // error




您还应该使用 nullptr 而不是0,以帮助使它们与正在使用0的各种DWORD参数保持一致。

You should also use nullptr with VS 2010 or later instead of 0 when passing NULL pointer parameters to help keep them straight with the various DWORD parameters you are using 0 for.

(2)对于COM API,您还应该考虑使用诸如 Microsoft :: WRL :: ComPtr 而不是原始指针,当变量超出范围时,原始指针将自动处理调用 Release

(2) For the COM API, you should also consider using a smart-pointer like Microsoft::WRL::ComPtr rather than raw pointers which will automatically handle calling Release when the variable goes out of scope.

#include <wrl/client.h>

using Microsoft::WRL::ComPtr;

ComPtr<ID3D10Blob> VS, PS;
DX::ThrowIfFailed(D3DX11CompileFromFile("Shaders.shader", nullptr, nullptr, "VShader",
    "vs_4_0", 0, 0, nullptr, &VS, nullptr, nullptr));
DX::ThrowIfFailed(D3DX11CompileFromFile("Shaders.shader", nullptr, nullptr, "PShader",
    "ps_4_0", 0, 0, nullptr, &PS, nullptr, nullptr));

DX::ThrowIfFailed(dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(),
    nullptr, &pVS)(;
DX::ThrowIfFailed(dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(),
    nullptr, &pPS));

在上面的代码中,您从未在 VS上调用 Release PS ,因此最终会泄漏该内存。

In your code above, you are never calling Release on VS or PS so would end up leaking that memory.

(3 ),包括D3DX11在内的所有D3DX版本都已弃用,DirectX SDK本身也已弃用(请参见 MSDN )。对于VS 2012或更高版本,您已经具有Windows 8.x SDK,该SDK具有Direct3D 11标头和HLSL编译器。而不是使用 D3DX11CompileFromFile 只是一个包装函数,直接调用 D3DCompileFromFile (请参阅 HLSL,FXC和D3DCompile )。

(3) All versions of D3DX including D3DX11 are deprecated, as is the DirectX SDK itself (See MSDN). With VS 2012 or later, you already have the Windows 8.x SDK which has Direct3D 11 headers and the HLSL compiler. Instead of using D3DX11CompileFromFile which is just a wrapper function, call D3DCompileFromFile directly (see HLSL, FXC, and D3DCompile).

#include <wrl/client.h>
#include <d3dcompiler.h>

using Microsoft::WRL::ComPtr;

ComPtr<ID3DBlob> VS, PS;
DX::ThrowIfFailed(D3DCompileFromFile("Shaders.shader", nullptr, nullptr, "VShader",
    "vs_4_0", 0, 0, &VS, nullptr));
DX::ThrowIfFailed(D3DCompileFromFile("Shaders.shader", nullptr, nullptr, "PShader",
    "ps_4_0", 0, 0, &PS, nullptr));

DX::ThrowIfFailed(dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(),
    nullptr, &pVS)(;
DX::ThrowIfFailed(dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(),
    nullptr, &pPS));

更新:如果需要更多有关错误的详细信息,请从 D3DCompileFromFile ,您应该提供 ID3DBlob 来解决以下错误:

UPDATE: If you need more detail on the error you are getting back from D3DCompileFromFile you should provide a ID3DBlob for the errors:

ComPtr<ID3DBlob> VS, Errors;
DX::ThrowIfFailed(D3DCompileFromFile("Shaders.shader", nullptr, nullptr, "VShader",
    "vs_4_0", 0, 0, &VS, &Errors));
if ( Errors )
{
    OutputDebugStringA( reinterpret_cast<const char*>( Errors->GetBufferPointer() ) );
}

这篇关于顶点着色器创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆