什么是 DirectX 11 等同于 dev->SetRenderState(D3DRS_ALPHAREF, value);

What is the DirectX 11 equivalent of dev->SetRenderState(D3DRS_ALPHAREF, value);

这里开始之前好好搜索了一下,这个问题:

How to set RenderState in DirectX11?

太笼统了;在研究第一个答案时,我怀疑我需要混合状态,但是如何设置 alpha 比较并不明显。

并且搜索 D3DRS_ALPHAREF 的堆栈溢出只产生了其他七个问题:https://whosebug.com/search?q=D3DRS_ALPHAREF none 其中甚至很接近。

我将其用于执行两次渲染以从一张图像过渡到另一张图像的程序。我有一个控制纹理,它与我正在渲染的纹理大小相同,并且是单通道亮度。

我的像素着色器的最后几行是:

// Copy rgb from the source texture
out.color.rgb = source.color.rgb;
// copy alpha from the control texture.
out.color.a = control.color.r;
return out;

然后在我的渲染设置中我有:

DWORD const reference = static_cast<DWORD>(frameNum);
D3DCMPFUNC const compare = pass == 0 ? D3DCMP_GREATEREQUAL : D3DCMP_LESS;
m_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, reference);
m_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, compare);

其中 frameNum 是过渡的当前帧编号:0 到 255。

-- 编辑 -- 对于那些不太熟悉 DirectX 9 的这一特殊功能的人,最后阶段使用比较函数将像素着色器的 alpha 输出与参考值进行比较,然后它实际绘制像素 iff 比较 returns 为真值。

所有这一切的最终结果是控制纹理的亮度级别控制每个像素在过渡中变化的早晚。

那么,我该如何使用 DirectX 11 执行此操作?

是的,我知道还有其他方法可以达到相同的结果,将 frameNum 传递给适当制作的像素着色器可以让我达到相同的效果。

这不是这里的重点,我不是在寻找替代实现,我希望了解如何在 DirectX 11 中进行 alpha 比较,因为它们在 DirectX 9 中不时被证明是一个有用的工具。

如果您要从 Direct3D 9 迁移到 Direct3D 11,短暂停留一下 Direct3D 10 中的更改是很有用的。MSDN 对此进行了详细介绍。该文章的要点之一是:

Removal of Fixed Function

It is sometimes surprising that even in a Direct3D 9 engine that fully exploits the programmable pipeline, there remains a number of areas that depend on the fixed-function (FF) pipeline. The most common areas are usually related to screen-space aligned rendering for UI. It is for this reason that you are likely to need to build a FF emulation shader or set of shaders which provide the necessary replacement behaviors.

This documentation contains a white paper containing replacement shader sources for the most common FF behaviors (see Fixed Function EMU Sample). Some fixed-function pixel behavior including alpha test has been moved into shaders.

IOW: 您在 Direct3D 10 或更高版本的可编程着色器中执行此操作。

查看 DirectX Tool Kit and in particular the AlphaTestEffect (implemented in this cpp and shader 文件)。