0

回到我用 DirectXTK 进行的 HLSL 实验,我偶然发现了一些非常奇怪的东西。首先,我设法将我的效果类集成到 DirectXTKs IEffect 子系统中,如下所示:

#include "pch.h"
#include "TmgnEffect.h"

using namespace Tmgn3DApp1;
using namespace Windows::Foundation;
using namespace Windows::System::Threading;
using namespace Concurrency;


TmgnEffect::TmgnEffect(const byte* _myVShaderCode, int _myVShaderCodeSize, Tmgn3DApp1Main* _thisRef)
{
    myVShaderCode = _myVShaderCode;
    myVShaderCodeSize = _myVShaderCodeSize;
    myThisRef = _thisRef;

}


// Helper sets our shaders and constant buffers onto the D3D device.
void TmgnEffect::ApplyShaders(_In_ ID3D11DeviceContext* deviceContext)
{
    // Set shaders.
    auto vertexShader = myThisRef->myVertexShader;
    auto pixelShader = myThisRef->myPixelShader;

    deviceContext->VSSetShader(vertexShader.Get(), nullptr, 0);
    deviceContext->PSSetShader(pixelShader.Get(), nullptr, 0);

#if defined(_XBOX_ONE) && defined(_TITLE)
    void *grfxMemory;
    mConstantBuffer.SetData(deviceContext, constants, &grfxMemory);

    Microsoft::WRL::ComPtr<ID3D11DeviceContextX> deviceContextX;
    ThrowIfFailed(deviceContext->QueryInterface(IID_GRAPHICS_PPV_ARGS(deviceContextX.GetAddressOf())));

    auto buffer = mConstantBuffer.GetBuffer();

    deviceContextX->VSSetPlacementConstantBuffer(0, buffer, grfxMemory);
    deviceContextX->PSSetPlacementConstantBuffer(0, buffer, grfxMemory);
#else
    // Make sure the constant buffer is up to date.

        //dirtyFlags &= ~EffectDirtyFlags::ConstantBuffer;

    XMStoreFloat4x4(&myConstantBufferData.view, view);
    //XMStoreFloat4x4(&myConstantBufferData.model, world);
    XMStoreFloat4x4(&myConstantBufferData.projection, projection);

    // Set the constant buffer.


    auto context = myThisRef->m_deviceResources->GetD3DDeviceContext();

    // Prepare the constant buffer to send it to the graphics device.
    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    DX::ThrowIfFailed(
        myThisRef->m_deviceResources->GetD3DDevice()->CreateBuffer(
            &constantBufferDesc,
            nullptr,
            &m_constantBuffer
        )
    );
    context->UpdateSubresource1(
        m_constantBuffer.Get(),
        0,
        NULL,
        &myConstantBufferData,
        0,
        0,
        0
    );


    deviceContext->VSSetConstantBuffers(0, 1, &m_constantBuffer);

    deviceContext->PSSetConstantBuffers(0, 1, &m_constantBuffer);
#endif
}


void Tmgn3DApp1::TmgnEffect::Apply(ID3D11DeviceContext * deviceContext)
{
    //matrices.SetConstants(dirtyFlags, constants.worldViewProj);


    // Set shaders and constant buffers.
    ApplyShaders(deviceContext);


}




void Tmgn3DApp1::TmgnEffect::GetVertexShaderBytecode(void const ** pShaderByteCode, size_t * pByteCodeLength)
{
    (*pByteCodeLength) = myVShaderCodeSize;
    (*pShaderByteCode) =  myVShaderCode;
}

void XM_CALLCONV Tmgn3DApp1::TmgnEffect::SetWorld(FXMMATRIX value)
{
    world = value;
}

void XM_CALLCONV Tmgn3DApp1::TmgnEffect::SetView(FXMMATRIX value)
{
    view = value;
}

void XM_CALLCONV Tmgn3DApp1::TmgnEffect::SetProjection(FXMMATRIX value)
{
    projection = value;
}

void XM_CALLCONV Tmgn3DApp1::TmgnEffect::SetMatrices(FXMMATRIX world, CXMMATRIX view, CXMMATRIX projection)
{
    SetWorld(world);
    SetView(view);
    SetProjection(projection);
}

遗憾的是,没有绘制模型。如果我用 BasicEffect 替换我的效果工厂,一切正常。我已经检查过了,在初始化期间没有抛出异常,并且在顶点着色器中放置一个(棘手的)无限循环会使程序挂起。顺便说一句,这是那个无限循环的代码:

float i = 1;
while ( i == 1)
{
    i++;
  //Real Code here
    i--;
}

简而言之,我有一些数据丢失的感觉。也许有人知道要寻找什么?

编辑以添加着色器。顶点着色器:

cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
    matrix model;
    matrix view;
    matrix projection;
};

struct VertexShaderInput
{
    float3 pos : SV_Position;
};

struct PixelShaderInput
{
    float4 pos : SV_POSITION;
};

PixelShaderInput main(VertexShaderInput input)
{
    PixelShaderInput output;
    float4 pos = float4(input.pos, 1.0f);

        pos = mul(pos, model);
        pos = mul(pos, view);
        pos = mul(pos, projection);
        output.pos = pos;

    return output;
}

像素着色器:

struct PixelShaderInput
{
    float4 pos : SV_POSITION;
};

float4 main(PixelShaderInput input) : SV_TARGET
{

    return float4(0.1f, 0.1f, 0.1f, 1.0f);
}
4

0 回答 0