编写 HLSL4 像素着色器以从 2D 纹理执行查找
我是一名初学者像素着色器编写者,我遇到了一些麻烦。我想要获取 256x256、16 位输入 (DXGI_FORMAT_R16_UINT) 图像,并将其传递给 256x256 查找纹理 (DXGI_FORMAT_R8_UNORM),以将其转换为 256x256 8 位输出。
不幸的是,我似乎遇到了很多麻烦,输出似乎总是限制为黑色或白色。
另外,我不确定应该使用哪种 DXGI 格式,以及哪种数据类型与每种格式相关。
// Global Variables
Texture2D<uint> imgTexture : register( t0 );
Texture2D lutTexture : register( t1 );
SamplerState SampleType : register( s0 );
// Structures
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
// Pixel Shader
float4 PS( PS_INPUT input) : SV_Target
{
uint pixelValue = imgTexture[input.Tex];
uint2 index = { pixelValue / 256, pixelValue % 256 };
// uint row = pixelValue / 256;
// uint col = pixelValue % 256;
float4 output = lutTexture[index];
output.g = output.r;
output.b = output.r;
output.a = 1.0f;
return output;
}
在尝试将 PixelValue 转换为 2D 索引之前,我是否应该对其进行标准化?
我应该在使用索引之前对其进行标准化吗?
我应该取样吗?
我是否走在正确的道路上?
我将不胜感激任何帮助,谢谢!
I'm a beginner pixel shader writer and I'm running into some trouble. I want to take a 256x256, 16-bit input (DXGI_FORMAT_R16_UINT) image, and pass it through a 256x256 look-up texture (DXGI_FORMAT_R8_UNORM) to convert it to a 256x256 8-bit output.
Unfortunately, I seem to be running into a lot of trouble and the output seems to always clamp to black or white.
Also, I'm not sure which DXGI formats I should be using, and also, which data type correlates with each format.
// Global Variables
Texture2D<uint> imgTexture : register( t0 );
Texture2D lutTexture : register( t1 );
SamplerState SampleType : register( s0 );
// Structures
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
// Pixel Shader
float4 PS( PS_INPUT input) : SV_Target
{
uint pixelValue = imgTexture[input.Tex];
uint2 index = { pixelValue / 256, pixelValue % 256 };
// uint row = pixelValue / 256;
// uint col = pixelValue % 256;
float4 output = lutTexture[index];
output.g = output.r;
output.b = output.r;
output.a = 1.0f;
return output;
}
Should I be normalizing the pixelValue before trying to turn it into a 2D index?
Should I be normalizing the index before using it?
Should I be sampling instead?
Am I even on the right path here?
I would appreciate ANY help, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您绝对走在正确的道路上。但是,就像 Valmond 提到的那样,pixelValue 的值将在 [0..1] 范围内。
LUT到底是如何设置的?我猜第一个轴是要转换的值,但第二个轴是什么?一旦我知道了这一点,我就可以用代码给出解决方案。
You are definitely on the right track. But, like Valmond mentioned, the value of pixelValue will be in the range [0..1].
How exactly is the LUT set up? I'm guessing that the first axis is the value to be transformed, but what is the second? Once I know that, I can give a solution with code.
您正在使用 :
,它需要像素值,但您提供的是 input.Tex (我猜它是从 0 到 1 标准化的)。
所以在这种情况下,您将加载相同的像素。
您可以使用 :
或 :
代替。
是的,您的格式对于您的用例来说是正确的(不需要规范化或非规范化)。
You are using :
Which requires pixel value, but you are providing input.Tex (which I guess is normalized from 0 to 1).
So in that case you are loading the same pixel.
you can use :
or :
instead.
And yes your formats are correct for your use case (no need to normalize or denormalize).