从矢量directx9更新纹理

时间:2017-04-12 10:41:04

标签: directx shader directx-9 alphablending

我'我试图为RGB渲染两个纹理,为alpha通道渲染另一个纹理,我将它们与着色器混合在一起。 Alpha通道纹理不能正确地与RGB纹理重叠。它似乎很紧张。

alpha通道纹理在每一帧都会发生变化,我需要通过以下功能从uint8_t数组开始填充:

D3DLOCKED_RECT locked_rect;

HRESULT hr = alpha_tex->LockRect(0, &locked_rect, nullptr, 0);

if (!FAILED(hr)) {
    ret_code = 0;
    BYTE *p_dst = (BYTE *)locked_rect.pBits;

    for (uint y = 0; y < height; y++) {
        memcpy(p_dst, alpha_array, width);
        alpha_array += width;
        p_dst += locked_rect.Pitch;
    }

    alpha_tex->UnlockRect(0);
}

其中alpha_array是包含alpha值的uint8_t数组。 要渲染纹理,我使用以下函数:

hwctx->d3d9device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0);
hwctx->d3d9device->BeginScene();

ctx->mFX->SetTechnique(ctx->mhTech);

ctx->texRGB->GetSurfaceLevel(0, &ctx->surfRGB);
hwctx->d3d9device->StretchRect((IDirect3DSurface9*)s->vdrFrame->data[3], NULL, ctx->surfRGB, NULL, D3DTEXF_LINEAR);

ctx->mFX->SetTexture(ctx->mhTexRGB, ctx->texRGB);
ctx->mFX->SetTexture(ctx->mhTexAlpha, ctx->texAlpha);

// Enable alpha blending.
hwctx->d3d9device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
hwctx->d3d9device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
hwctx->d3d9device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

UINT numPasses = 0;
ctx->mFX->Begin(&numPasses, 0);

for (UINT i = 0; i < numPasses; ++i){
    ctx->mFX->BeginPass(i);
    hwctx->d3d9device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
    ctx->mFX->EndPass();
}

ctx->mFX->End();
hwctx->d3d9device->EndScene();
hwctx->d3d9device->Present(0, 0, 0, 0);

// Disable alpha blending.
hwctx->d3d9device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

我按顶点/像素着色器组合纹理:

uniform extern texture  gTexRGB;
uniform extern texture  gTexAlpha;

sampler TexRGB = sampler_state{
    Texture = <gTexRGB>;
    AddressU  = WRAP;
    AddressV  = WRAP;
};

sampler TexAlpha = sampler_state{
    Texture = <gTexAlpha>;
    AddressU = WRAP;
    AddressV = WRAP;
};

struct OutputVS{
    float4 posH    : POSITION0;
    float2 tex0    : TEXCOORD0;
};

OutputVS TextureBlendingVS(float2 tex0: TEXCOORD0){
    // Zero out our output.
    OutputVS outVS = (OutputVS)0;

    // Pass on texture coordinates to be interpolated in rasterization.
    outVS.tex0 = tex0;

    // Done--return the output.
    return outVS;
}

float4 TextureBlendingPS(float2 tex0 : TEXCOORD0) : COLOR{
    float3  rgb = tex2D(TexRGB, tex0).rgb;
    float   alpha = tex2D(TexAlpha, tex0).a;
    return float4(rgb, alpha);
}

technique DirLightTexTech{
    pass P0 {
        // Specify the vertex and pixel shader associated with this pass.
        vertexShader = compile vs_2_0 TextureBlendingVS();
        pixelShader  = compile ps_2_0 TextureBlendingPS();

    }
}

纹理的大小是相同的,但在渲染过程中出现问题。

请帮帮我。 :)

0 个答案:

没有答案