无法访问 DXGI_FORMAT_R8_UINT 纹理

发布于 2024-10-30 21:37:54 字数 3492 浏览 2 评论 0原文

我想对相机发送的 8 位图像进行一些计算,但我不知道如何访问它们。

以下是我创建纹理对象/资源视图的代码:

D3D11_TEXTURE2D_DESC tDesc;
tDesc.Height = 480;
tDesc.Width = 640;
tDesc.Usage = D3D11_USAGE_DYNAMIC;
tDesc.MipLevels = 1;
tDesc.ArraySize = 1;
tDesc.SampleDesc.Count = 1;
tDesc.SampleDesc.Quality = 0;
tDesc.Format = DXGI_FORMAT_R8_UINT;
tDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
tDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
tDesc.MiscFlags = 0;

V_RETURN(pd3dDevice->CreateTexture2D(&tDesc, NULL, &g_pCurrentImage));

D3D11_SHADER_RESOURCE_VIEW_DESC rvDesc;
g_pCurrentImage->GetDesc(&tDesc);
rvDesc.Format = DXGI_FORMAT_R8_UINT;
rvDesc.Texture2D.MipLevels = tDesc.MipLevels;
rvDesc.Texture2D.MostDetailedMip = tDesc.MipLevels - 1;
rvDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
V_RETURN(pd3dDevice->CreateShaderResourceView(g_pCurrentImage, &rvDesc, &g_pImageRV));

我使用以下代码更新纹理:

if( !g_updateDone ) {
    D3D11_MAPPED_SUBRESOURCE resource;
    resource.pData = g_Images.pData;
    resource.RowPitch = 640;
    resource.DepthPitch = 1;
    okay = pd3dImmediateContext->Map(g_pCurrentImage, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
    g_updateDone = true;
    }
pd3dImmediateContext->PSSetShaderResources(0, 1, &g_pImageRV);

HLSL 文件(采用四边形,在屏幕上显示为四边形):

//-----------------------------------------------------------------------------------------
// Textures and Samplers
//-----------------------------------------------------------------------------------------

Texture2D <int> g_txDiffuse : register( t0 );
SamplerState g_samLinear : register( s0 );

//--------------------------------------------------------------------------------------
// shader input/output structure
//--------------------------------------------------------------------------------------

struct VS_INPUT
{
    float4 Position     : POSITION; // vertex position 
    float2 TextureUV    : TEXCOORD0;// vertex texture coords 
};

struct VS_OUTPUT
{
    float4 Position     : SV_POSITION; // vertex position 
    float2 TextureUV    : TEXCOORD0;   // vertex texture coords 
};

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
    VS_OUTPUT Output;

    Output.Position = input.Position;

    Output.TextureUV = input.TextureUV; 

    return Output;    
}

//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------

float4 RenderScenePS( VS_OUTPUT In ) : SV_TARGET
{ 
    int3 loc;
    loc.x = 0;
    loc.y = 0;
    loc.z = 0;
    int r = g_txDiffuse.Load(loc);
    float fTest = (float) r;

    fTest = fTest / 256;

    return float4( In.TextureUV.x, In.TextureUV.y, In.TextureUV.x + In.TextureUV.y, fTest);
}

当我尝试运行此文件时,出现错误 X4532:无法将表达式映射到像素着色器指令集

当我尝试调试它时,当我尝试查看 g_pCurrentImage 的数据时,它只是说无法显示表面,并且当用

return float4( In.TextureUV.x, In.TextureUV.y, In.TextureUV.x + In.TextureUV.y, 1);

DirectX 调试器(在本例中为 PIX)替换它时,只会忽略第一个线,它们永远不会到达,因此我无法调试它。我已经没有办法处理这个问题了,我想做的只是一些基本的数学指令,但我完全无法让像素着色器工作。

I want to do some calculations on a 8bit image sent by a camera, and I just can't figure out the way to access them.

Following is my code to create the texture object / resource view:

D3D11_TEXTURE2D_DESC tDesc;
tDesc.Height = 480;
tDesc.Width = 640;
tDesc.Usage = D3D11_USAGE_DYNAMIC;
tDesc.MipLevels = 1;
tDesc.ArraySize = 1;
tDesc.SampleDesc.Count = 1;
tDesc.SampleDesc.Quality = 0;
tDesc.Format = DXGI_FORMAT_R8_UINT;
tDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
tDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
tDesc.MiscFlags = 0;

V_RETURN(pd3dDevice->CreateTexture2D(&tDesc, NULL, &g_pCurrentImage));

D3D11_SHADER_RESOURCE_VIEW_DESC rvDesc;
g_pCurrentImage->GetDesc(&tDesc);
rvDesc.Format = DXGI_FORMAT_R8_UINT;
rvDesc.Texture2D.MipLevels = tDesc.MipLevels;
rvDesc.Texture2D.MostDetailedMip = tDesc.MipLevels - 1;
rvDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
V_RETURN(pd3dDevice->CreateShaderResourceView(g_pCurrentImage, &rvDesc, &g_pImageRV));

I update the texture with following code:

if( !g_updateDone ) {
    D3D11_MAPPED_SUBRESOURCE resource;
    resource.pData = g_Images.pData;
    resource.RowPitch = 640;
    resource.DepthPitch = 1;
    okay = pd3dImmediateContext->Map(g_pCurrentImage, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
    g_updateDone = true;
    }
pd3dImmediateContext->PSSetShaderResources(0, 1, &g_pImageRV);

The HLSL File (takes a quad, displays as quad on screen):

//-----------------------------------------------------------------------------------------
// Textures and Samplers
//-----------------------------------------------------------------------------------------

Texture2D <int> g_txDiffuse : register( t0 );
SamplerState g_samLinear : register( s0 );

//--------------------------------------------------------------------------------------
// shader input/output structure
//--------------------------------------------------------------------------------------

struct VS_INPUT
{
    float4 Position     : POSITION; // vertex position 
    float2 TextureUV    : TEXCOORD0;// vertex texture coords 
};

struct VS_OUTPUT
{
    float4 Position     : SV_POSITION; // vertex position 
    float2 TextureUV    : TEXCOORD0;   // vertex texture coords 
};

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
    VS_OUTPUT Output;

    Output.Position = input.Position;

    Output.TextureUV = input.TextureUV; 

    return Output;    
}

//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------

float4 RenderScenePS( VS_OUTPUT In ) : SV_TARGET
{ 
    int3 loc;
    loc.x = 0;
    loc.y = 0;
    loc.z = 0;
    int r = g_txDiffuse.Load(loc);
    float fTest = (float) r;

    fTest = fTest / 256;

    return float4( In.TextureUV.x, In.TextureUV.y, In.TextureUV.x + In.TextureUV.y, fTest);
}

When I try to run this, I get an error X4532: cannot map expression to pixel shader instruction set

When I try to debug it, when I try to see the data of the g_pCurrentImage, it just says cannot display surface, and when replacing that with

return float4( In.TextureUV.x, In.TextureUV.y, In.TextureUV.x + In.TextureUV.y, 1);

the DirectX debugger (PIX in this case) just ignores the first lines, they are never reached and thus I cannot debug it. I have run out of options on how to deal with this, all I want to do are some basic math instructions, yet I am completely unable to get the pixel shader to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-11-06 21:37:54

您尝试将数据输入纹理的方式不起作用。 Map() 调用填充 D3D11_MAPPED_SUBRESOURCE 结构:它为您提供一个用于写入数据的指针,并告诉您必须遵守的行和切片间距。因此,您调用 Map(),将数据写入提供的指针,然后调用 Unmap()。

或者,您可以使用 UpdateSubresource() 方法,该方法获取您提供的指针和音高信息,并将数据复制到纹理中,就像您尝试使用 Map() 所做的那样。

The way you're trying to get data into the texture doesn't work. The Map() call fills out the D3D11_MAPPED_SUBRESOURCE structure: it gives you a pointer to write the data into, and tells you what row and slice pitch you must respect. So you call Map(), write your data into the provided pointer, and then call Unmap().

Alternately you can use the UpdateSubresource() method, which takes the pointer and pitch info you provide and copies the data into the texture, like you're trying to do with Map().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文