Direct2D自定义效果:HLSL多纹理绑定失败求助
问题:D2D自定义Effect中第二个纹理槽采样黑屏
我已花费两天时间调试HLSL着色器绑定双纹理的问题:第一个纹理绑定到t0槽并采样返回颜色完全正常,但将同一纹理绑定到t1槽时却始终黑屏。第二个纹理通过OpenCV加载(加载代码已验证可用),第一个纹理是之前着色器运算的结果(也已确认可用)。测试将第一个纹理放到第二个槽同样无法正常显示,因此怀疑是第二个绑定槽本身存在问题,与纹理加载无关。
加载第二个纹理的代码
cv::Mat png_image = cv::imread("<path to png, also confirmed>", cv::IMREAD_UNCHANGED); if (!png_image.data) { ASSERT(false); } ASSERT(4 == png_image.channels()); // and then the texture data is successfully uploaded to the GPU, // this also has to work as the same function has been used multiple times before
自定义Effect代码
// BaseCustomEffect extends from "public ID2D1EffectImpl, public ID2D1DrawTransform" // and those implement the "Initialize", "PrepareForRender" and "SetGraph" class CustomEffect : public BaseCustomEffect { private: CustomEffect(); public: //! Register effect static HRESULT Register(ID2D1Factory1* pFactory); static HRESULT __stdcall CreateCustomEffect(IUnknown** ppEffectImpl); }; CustomEffect::CustomEffect() { // BLOT_CustomShader is the compiled binary of the shader (compiled successfully). m_shaderBlob = BLOT_CustomShader; m_shaderBlobSize = _ARRAYSIZE(BLOT_CustomShader); m_shaderGUID = GUID_CustomEffectShader; } #define XML(X) TEXT(#X) HRESULT CustomEffect::Register(ID2D1Factory1* pFactory) { // The inspectable metadata of an effect is defined in XML. This can be passed in from an // external source as well, however for simplicity we just inline the XML. PCWSTR pszXml = XML ( <?xml version = '1.0'?> <Effect> <!--System Properties--> <Property name = 'DisplayName' type = 'string' value = 'Custom Effect' /> <Property name = 'Author' type = 'string' value = '<author>' /> <Property name = 'Category' type = 'string' value = '<category>' /> <Property name = 'Description' type = 'string' value = 'Custom effect.' /> <Inputs minimum = '2' maximum = '2'> <Input name = 'Source' /> <Input name = 'CustomTexture' /> </Inputs> </Effect> ); // Must match name attribute in XML. const D2D1_PROPERTY_BINDING bindings[] = { // this is set here, but not relevant for the problem // D2D1_VALUE_TYPE_BINDING(L"Intensity", &SetIntensity, &GetIntensity), }; return pFactory->RegisterEffectFromString(GUID_CustomEffectShader, pszXml, bindings, _ARRAYSIZE(bindings), CreateCustomEffect); } HRESULT __stdcall CustomEffect::CreateCustomffect(IUnknown** ppEffectImpl) { *ppEffectImpl = static_cast<ID2D1EffectImpl*>(new CustomEffect()); return (nullptr == *ppEffectImpl) ? E_OUTOFMEMORY : S_OK; }
自定义Effect渲染代码
inD2DContext->SetTarget(inTargetBmp); inD2DContext->BeginDraw(); inD2DContext->Clear(D2D1::ColorF(D2D1::ColorF::Black, 0.f)); inConversion.mEffect->SetInputCount(2); inConversion.mEffect->SetInput(0, inSourceBmp); inConversion.mEffect->SetInput(1, texture->GetData()->GetD2dBitmap()); inD2DContext->DrawImage(inConversion.mEffect); inD2DContext->EndDraw();
HLSL着色器代码
#define D2D_INPUT_COUNT 2 Texture2D InputTexture : register(t0); // works, if I sample using the InputSampler, as expected Texture2D CustomTexture : register(t1); // does not work SamplerState InputSampler : register(s0); SamplerState CustomSampler : register(s1); /* // not relevant here cbuffer constants : register(b0) { float Intensity : packoffset(c0); }; */ float4 main( float4 pos : SV_POSITION, float4 posScene : SCENE_POSITION, float2 uv0 : TEXCOORD0 ) : SV_Target { #if 1 // works //return float4(1, 0, 0, 1); // not working //return CustomTexture.Sample(InputSampler, uv0); // also not working, not expected, as the uvs are now hardcoded return CustomTexture.Sample(CustomSampler, float2(0.5f, 0.5f)); // also not working, this should output the custom texture itself and display it to the screen //return CustomTexture.Sample(CustomSampler, uv0); #else // actual shader code, not relevant here #endif }
内容的提问来源于stack exchange,提问作者Can
相关产品推荐
相关产品推荐

