Directx 似乎没有调用我的像素着色器

Directx 似乎没有调用我的像素着色器,directx,hlsl,vertex-shader,pixel-shader,pix,Directx,Hlsl,Vertex Shader,Pixel Shader,Pix,我一直在摆弄DirectX,我一直在研究我的这个奇怪的想法有一段时间了。似乎我的像素着色器由于某种奇怪的原因没有被调用。我不知道这是否可能,或者这是我的愚蠢错误。代码如下:我从教程中复制了大部分代码 着色器HLSL代码如下所示: /* ******************************************************************************************************** * Constant Buffers (Can o

我一直在摆弄DirectX,我一直在研究我的这个奇怪的想法有一段时间了。似乎我的像素着色器由于某种奇怪的原因没有被调用。我不知道这是否可能,或者这是我的愚蠢错误。代码如下:我从教程中复制了大部分代码

着色器HLSL代码如下所示:

/*
********************************************************************************************************
*   Constant Buffers (Can only change per render call).
********************************************************************************************************
*/
cbuffer SConstantBuffer : register( b0 )
{
    float3 origin;
    float3 direction;
    float planeDistance;
    int screenWidth;
    int screenHeight;
};


/*
********************************************************************************************************
*   Input and Output Structures
********************************************************************************************************
*/
struct VS_INPUT
{
    float3 position     :   POSITION;
    float4 color        :   COLOR;
};

struct VS_OUTPUT
{
    float4 position     :   SV_POSITION;
    float4 color        :   COLOR;
};


/*
********************************************************************************************************
*   Vertex Shader main
********************************************************************************************************
*/
VS_OUTPUT VS( VS_INPUT input )
{
    VS_OUTPUT output = (VS_OUTPUT)0;

    float param_d;
    float3 pixelPoint;
    float3 zAxis = { 0.0, 0.0, 1.0 };
    float3 planeOrigin = origin + (direction * planeDistance);
    float3 pointDirection = (input.position - origin) / length( (input.position - origin) );

    param_d = dot( (planeOrigin - origin), direction ) / dot( pointDirection, direction );
    pixelPoint = ( pointDirection * param_d ) + origin;

    output.position.xyz = cross ( pixelPoint, direction );
    output.position.x = output.position.x * 5.0;
    output.position.y = output.position.y * 5.0;
    output.position.z = 20.0;
    output.position.w = output.position.z;

    output.color = input.color;

    return output;
}


/*
********************************************************************************************************
*   Pixel Shader main
********************************************************************************************************
*/
float4 PS( VS_OUTPUT input ) : SV_Target
{
    return float4( 1.0, 1.0, 1.0, 1.0); //input.color;
}
DirectX初始化和呈现代码如下:

#include "Renderer.h"

CRenderer::CRenderer ( )
{
    this->m_dxDevice = NULL;
    this->m_dxDeviceContext = NULL;
    this->m_dxRenderTargetView = NULL;
    this->m_dxSwapChain = NULL;
    this->m_dxBackBuffer = NULL;
    this->m_dxVertexLayout = NULL;
    this->m_dxVertexBuffer = NULL;
    this->m_dxPixelShader = NULL;
    this->m_dxConstantBuffer = NULL;
}

HRESULT CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut )
{
    HRESULT hr = S_OK;

    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

    ID3DBlob* pErrorBlob;
    hr = D3DX11CompileFromFile( szFileName, NULL, NULL, szEntryPoint, szShaderModel, 
        dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL );
    if( FAILED(hr) )
    {
        if( pErrorBlob != NULL )
            OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
        if( pErrorBlob ) pErrorBlob->Release();
        return hr;
    }
    if( pErrorBlob ) pErrorBlob->Release();

    return S_OK;
}

HRESULT CRenderer::InitDevice ( HWND arg_hWnd )
{
    HRESULT status = S_OK;
    this->m_hWnd = arg_hWnd;

    RECT rctWindowSize;
    GetClientRect( arg_hWnd, &rctWindowSize );
    UINT dWidth = rctWindowSize.right - rctWindowSize.left;
    UINT dHeight = rctWindowSize.bottom - rctWindowSize.top;

    UINT dCreateDeviceFlag = 0;
#ifdef _DEBUG
    dCreateDeviceFlag |= D3D11_CREATE_DEVICE_DEBUG;
#endif _DEBUG

    D3D_DRIVER_TYPE pDriverType [] = 
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE
    };
    UINT numDriverType = ARRAYSIZE(pDriverType);

    D3D_FEATURE_LEVEL pFeatureLevel [] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0
    };
    UINT numFeatureLevel = ARRAYSIZE(pFeatureLevel);


    //Fillout Device Descriptor
    DXGI_SWAP_CHAIN_DESC objSwapChainDesc;
    ZeroMemory(&objSwapChainDesc, sizeof(objSwapChainDesc));
    objSwapChainDesc.BufferCount = 1;
    objSwapChainDesc.BufferDesc.Width = dWidth;
    objSwapChainDesc.BufferDesc.Height = dHeight;
    objSwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    objSwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    objSwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    objSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    objSwapChainDesc.OutputWindow = this->m_hWnd;
    objSwapChainDesc.SampleDesc.Count = 1;
    objSwapChainDesc.SampleDesc.Quality = 0;
    objSwapChainDesc.Windowed = true;


    //Parse and create the appropriate type of device
    for ( UINT driverTypeIndex = 0; driverTypeIndex < numDriverType; driverTypeIndex++ )
    {
        this->m_objDriverType = pDriverType[driverTypeIndex];
        status = D3D11CreateDeviceAndSwapChain ( NULL, 
                                                 this->m_objDriverType, 
                                                 NULL, 
                                                 dCreateDeviceFlag, 
                                                 pFeatureLevel, 
                                                 numFeatureLevel, 
                                                 D3D11_SDK_VERSION,
                                                 &objSwapChainDesc, 
                                                 &(this->m_dxSwapChain), 
                                                 &(this->m_dxDevice), 
                                                 &(this->m_objFeatureLevel), 
                                                 &(this->m_dxDeviceContext) );
        if ( SUCCEEDED(status) )
            break;
    }
    if ( FAILED(status) )
        return status;


    //Create a render traget
    this->m_dxBackBuffer = NULL;
    status = this->m_dxSwapChain->GetBuffer ( 0, __uuidof(ID3D11Texture2D), (LPVOID*)(&this->m_dxBackBuffer) );
    if ( FAILED(status) )
        return status;

    status = this->m_dxDevice->CreateRenderTargetView ( this->m_dxBackBuffer, NULL, &(this->m_dxRenderTargetView) );
    if ( FAILED(status) )
        return status;


    //CreatDepth Stencil
    D3D11_TEXTURE2D_DESC objDepthStencilDesc;
    ZeroMemory(&objDepthStencilDesc, sizeof(objDepthStencilDesc));

    D3D11_DEPTH_STENCIL_VIEW_DESC objDepthStencilViewDesc;
    ZeroMemory(&objDepthStencilViewDesc, sizeof(objDepthStencilViewDesc));

    objDepthStencilDesc.Width = dWidth;
    objDepthStencilDesc.Height = dHeight;
    objDepthStencilDesc.MipLevels = 1;
    objDepthStencilDesc.ArraySize = 1;
    objDepthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    objDepthStencilDesc.SampleDesc.Count = 1;
    objDepthStencilDesc.SampleDesc.Quality = 0;
    objDepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
    objDepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    objDepthStencilDesc.MiscFlags = 0;
    objDepthStencilDesc.CPUAccessFlags = 0;

    status = this->m_dxDevice->CreateTexture2D ( 
                                                &objDepthStencilDesc, 
                                                NULL,
                                                &(this->m_dxDepthStencilBuffer)
                                                );
    if ( FAILED(status) )
        return status;

    objDepthStencilViewDesc.Format = objDepthStencilDesc.Format;
    objDepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    objDepthStencilViewDesc.Texture2D.MipSlice = 0;

    status = this->m_dxDevice->CreateDepthStencilView ( 
                                                       this->m_dxDepthStencilBuffer,
                                                       &objDepthStencilViewDesc,
                                                       &(this->m_dxDepthStencilView)
                                                      );
    if ( FAILED(status) )
        return status;

    this->m_dxDeviceContext->OMSetRenderTargets ( 1, &(this->m_dxRenderTargetView), this->m_dxDepthStencilView );


    //Set View Port (Can be commented out;
    this->m_objViewport.Width       = (FLOAT)dWidth;
    this->m_objViewport.Height      = (FLOAT)dHeight;
    this->m_objViewport.MinDepth    = 0.0;
    this->m_objViewport.MaxDepth    = 1.0;
    this->m_objViewport.TopLeftX    = 0;
    this->m_objViewport.TopLeftY    = 0;

    this->m_dxDeviceContext->RSSetViewports ( 1, &(this->m_objViewport) );


    //Setting up Shaders
    //Compiling Vertex Shader file
    ID3DBlob * dxVSBlob = NULL;
    status = CompileShaderFromFile ( L"RTShaders.fx", "VS", "vs_4_0", &dxVSBlob );
    if (FAILED (status))
    {
        dxVSBlob->Release ( );
        return status;
    }

    //Creating Compiled Shader
    status = this->m_dxDevice->CreateVertexShader ( dxVSBlob->GetBufferPointer ( ), 
                                                    dxVSBlob->GetBufferSize ( ),
                                                    NULL,
                                                    &(this->m_dxVertexShader) );
    if( FAILED(status) )
    {
        dxVSBlob->Release ( );
        return status;
    }

    //Describe Layout
    D3D11_INPUT_ELEMENT_DESC layout [] =
    {
        {
            "POSITION",
            0,
            DXGI_FORMAT_R32G32B32_FLOAT,
            0,
            0,
            D3D11_INPUT_PER_VERTEX_DATA,
            0
        },
/*      {
            "NORMAL",
            0,
            DXGI_FORMAT_R32G32B32A32_FLOAT,
            0,
            16,
            D3D11_INPUT_PER_VERTEX_DATA,
            0
        },
*/      {
            "COLOR",
            0,
            DXGI_FORMAT_R32G32B32_FLOAT,
            0,
            12,                             //NEED TO CHANGE THIS TO 32 WHEN UNCOMMENTING THE ABOVE BLOCK
            D3D11_INPUT_PER_VERTEX_DATA,
            0
        },
    };
    UINT numElements = ARRAYSIZE(layout);

    //Create Layout
    status = this->m_dxDevice->CreateInputLayout ( layout, 
                                                   numElements, 
                                                   dxVSBlob->GetBufferPointer ( ), 
                                                   dxVSBlob->GetBufferSize ( ), 
                                                   &(this->m_dxVertexLayout) );
    dxVSBlob->Release ( );
    dxVSBlob = NULL;
    if ( FAILED(status) )
    {
        return status;
    }

    //Set Vertex Layout to the pipeline
    this->m_dxDeviceContext->IASetInputLayout ( this->m_dxVertexLayout );

    //Compiling Pixel Shader File
    ID3DBlob * dxPSBlob = NULL;
    status = CompileShaderFromFile ( L"RTShaders.fx", "PS", "ps_4_0", &dxPSBlob );
    if ( FAILED(status) )
    {
        if ( dxPSBlob )
            dxPSBlob->Release ( );
        return status;
    }

    //Creating Compiled Pixel Shader
    status = this->m_dxDevice->CreatePixelShader ( 
                                                 dxPSBlob->GetBufferPointer ( ),
                                                 dxPSBlob->GetBufferSize ( ),
                                                 NULL,
                                                 &(this->m_dxPixelShader)
                                                 );
    dxPSBlob->Release ( );
    dxPSBlob = NULL;
    if ( FAILED(status) )
    {
        return status;
    }

    return S_OK;
}
HRESULT CRenderer::CreateVertexBuffer ( )
{
    HRESULT status = S_OK;

    D3D11_BUFFER_DESC objBufferDesc;
    D3D11_SUBRESOURCE_DATA objSubresData;
    UINT dStride = sizeof (SVertex);
    UINT dOffset = 0;

    ZeroMemory(&objBufferDesc, sizeof(objBufferDesc));
    ZeroMemory(&objSubresData, sizeof(objSubresData));

    objBufferDesc.ByteWidth = sizeof(SVertex) * 1681;
    objBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    objBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    objBufferDesc.CPUAccessFlags = 0;

    objSubresData.pSysMem = pVertexData;

    status = this->m_dxDevice->CreateBuffer (
                                            &objBufferDesc,
                                            &objSubresData,
                                            &(this->m_dxVertexBuffer)
                                            );
    if ( FAILED(status) )
        return status;

    this->m_dxDeviceContext->IASetVertexBuffers ( 0, 1, &(this->m_dxVertexBuffer), &dStride, &dOffset );

    this->m_dxDeviceContext->IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_POINTLIST );

    return S_OK;
}

HRESULT CRenderer::CreateConstantBuffer ( )
{
    HRESULT status = S_OK;

    D3D11_BUFFER_DESC objBufferDesc;

    ZeroMemory(&objBufferDesc, sizeof(objBufferDesc));

    objBufferDesc.ByteWidth = sizeof(SConstantBuffer) + (16 - sizeof(SConstantBuffer)%16);
    objBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    objBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    objBufferDesc.CPUAccessFlags = 0;

    status = this->m_dxDevice->CreateBuffer (
                                            &objBufferDesc,
                                            NULL,
                                            &(this->m_dxConstantBuffer)
                                            );

    return status;
}

HRESULT CRenderer::Render ( )
{
    HRESULT status = S_OK;
    RECT screenRect;
    SConstantBuffer objConstBuffer;

    float pClearColor [4] = { 0.0f, 0.125f, 0.5f, 1.0f };

    this->m_dxDeviceContext->ClearRenderTargetView ( this->m_dxRenderTargetView, pClearColor );
    this->m_dxDeviceContext->ClearDepthStencilView ( this->m_dxDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );

    GetClientRect ( this->m_hWnd, &screenRect );
    objConstBuffer.f4Origin = XMFLOAT3 ( 0.0f, 0.0f, 0.0f );
    objConstBuffer.f4Direction = XMFLOAT3 ( 0.0f, 0.0f, 1.0f );
    objConstBuffer.fDistanceFromPlane = 2.0f;
    objConstBuffer.dScreenWidth = screenRect.right - screenRect.left;
    objConstBuffer.dScreenHeight = screenRect.bottom - screenRect.top;
    this->m_dxDeviceContext->UpdateSubresource ( this->m_dxConstantBuffer, 0, NULL, &objConstBuffer, 0, 0 );

    this->m_dxDeviceContext->VSSetShader ( this->m_dxVertexShader, NULL, 0 );
    this->m_dxDeviceContext->VSSetConstantBuffers ( 0, 1, &(this->m_dxConstantBuffer) );
    this->m_dxDeviceContext->PSSetShader ( this->m_dxPixelShader, NULL, 0 );
    this->m_dxDeviceContext->Draw ( 1680, 0 );

    this->m_dxSwapChain->Present ( 0, 0 );

    return status;
}

CRenderer::~CRenderer ( )
{
    if ( this->m_dxDeviceContext ) 
        this->m_dxDeviceContext->ClearState ( );

    if ( this->m_dxPixelShader )
        this->m_dxPixelShader->Release ( );
    this->m_dxPixelShader = NULL;

    if ( this->m_dxVertexLayout )
        this->m_dxVertexLayout->Release ( );
    this->m_dxVertexLayout = NULL;

    if ( this->m_dxVertexBuffer )
        this->m_dxVertexBuffer->Release ( );
    this->m_dxVertexBuffer = NULL;

    if ( this->m_dxDepthStencilBuffer )
        this->m_dxDepthStencilBuffer->Release ( );
    this->m_dxDepthStencilBuffer = NULL;

    if ( this->m_dxDepthStencilView )
        this->m_dxDepthStencilView->Release ( );
    this->m_dxDepthStencilView = NULL;

    if ( this->m_dxRenderTargetView )
        this->m_dxRenderTargetView->Release ( );
    this->m_dxRenderTargetView = NULL;

    if ( this->m_dxBackBuffer->Release ( ) )
        this->m_dxBackBuffer->Release ( );
    this->m_dxBackBuffer = NULL;

    if ( this->m_dxSwapChain )
        this->m_dxSwapChain->Release ( );
    this->m_dxSwapChain = NULL;

    if ( this->m_dxDeviceContext )
        this->m_dxDeviceContext->Release ( );
    this->m_dxDeviceContext = NULL;

    if ( this->m_dxDevice )
        this->m_dxDevice->Release ( );
    this->m_dxDevice = NULL;
}
屏幕上没有任何渲染,我应该看到点或一个大的白色补丁,但我看到的只是渲染目标被清除。我一直在运行Pix,PreVS和PostVS转换似乎很好。
请帮忙

您的代码乍一看似乎很好。你能贴一张PIX'mesh窗口的截图吗?也可以在“渲染”窗口中的像素上单击鼠标右键,然后选择“调试像素”以逐步完成着色器阶段。您还将看到,如果某个像素由于某种原因被删除,那么您的常量缓冲区可能会出现问题,您能否发布m_dxConstantBuffer和头文件的代码?其中的数据是16个字节对齐的,所以很可能存在填充问题。可能我很密集,但我看不出你在顶点缓冲区中放置了什么?你正在调用所有必要的函数。。。i、 CreateConstantBuffer、CreateVertexBuffer等。您的代码乍一看似乎很好。你能贴一张PIX'mesh窗口的截图吗?也可以在“渲染”窗口中的像素上单击鼠标右键,然后选择“调试像素”以逐步完成着色器阶段。您还将看到,如果某个像素由于某种原因被删除,那么您的常量缓冲区可能会出现问题,您能否发布m_dxConstantBuffer和头文件的代码?其中的数据是16个字节对齐的,所以很可能存在填充问题。可能我很密集,但我看不出你在顶点缓冲区中放置了什么?你正在调用所有必要的函数。。。i、 e.CreateConstantBuffer、CreateVertexBuffer等。。