C++11 如何将附近的像素数据发送到Direct3D 11中的像素着色器?
我一直在学习这些教程(我只允许2个链接):https://code.msdn.microsoft.com/Direct3D-Tutorial-Win32-82999ef 并通过Direct3D 11图形管道读取: 我目前有一个用HLSL编码的像素(aka.Fragment)着色器,由以下代码组成:C++11 如何将附近的像素数据发送到Direct3D 11中的像素着色器?,c++11,graphics,3d,directx,sample,C++11,Graphics,3d,Directx,Sample,我一直在学习这些教程(我只允许2个链接):https://code.msdn.microsoft.com/Direct3D-Tutorial-Win32-82999ef 并通过Direct3D 11图形管道读取: 我目前有一个用HLSL编码的像素(aka.Fragment)着色器,由以下代码组成: //Pixel Shader input. struct psInput { float4 Position: SV_POSITION; float4 Color: COLOR; };
//Pixel Shader input.
struct psInput
{
float4 Position: SV_POSITION;
float4 Color: COLOR;
};
//Pixel (aka. Fragment) Shader.
float4 PS(psInput input): SV_TARGET
{
return input.Color;
}
我(想我)想做的是对像素着色器中的每个像素进行多采样并访问附近的像素数据,这样我就可以执行一种自定义的抗锯齿处理,比如FXAA()。据我所知,我需要为每个渲染使用PSSetShaderResources将纹理传递给HLSL,但除此之外我不知道。所以,我的问题是:
如何将附近的像素数据发送到Direct3D 11中的像素着色器?
能够做到这一点也将极大地帮助我理解C++和HLSL如何相互交流,超越了我在教程中发现的“通过一些FLAT4到着色”的标准。这似乎是D3D开发中最关键的方面,但我在网上找不到很多这样的例子
我考虑过传统的MSAA(多采样消除混叠),但我找不到任何关于如何在D3D11中成功实现这一点的信息,除此之外,我需要首先使用“比特块传输”(BitBlt)模型交换链。(参见DXGI_SAMPLE_DESC1和DXGI_SAMPLE_DESC;只有计数为1和质量为0(无AA)才会导致绘制内容。)此外,我想知道如何执行上述操作,以便在我的项目的其他方面需要时获得一般理解。关于如何在D3D11中执行MSAA的答案也很受欢迎 请仅使用D3D 11和HLSL代码谢谢大家! 要执行FXAA之类的自定义抗锯齿,需要将场景渲染到屏幕外渲染目标: -使用绑定标志创建一个
ID3D11Texture2D
和D3D11\u bind\u RENDER\u TARGET
和D3D11\u bind\u SHADER\u资源
-为步骤1中创建的纹理创建ID3D11ShaderResourceView
和ID3D11RenderTargetView
-将场景渲染到在步骤2中创建的ID3D11RenderTargetView
-将backbuffer设置为渲染目标,并将在步骤2中创建的ID3D11ShaderResourceView
绑定到正确的像素着色器插槽
-您将能够在像素着色器中对包含场景的纹理进行采样(使用该函数)
当你尝试做传统的MSAA时,你记得设置吗?我再次回答我自己的问题,有点像(从未使用过FXAA…)。我在这里提供我的答案是为了善待那些追随我脚步的人 原来我错过了MSAA的深度模具视图。您希望禁用MSAA的采样计数为1U,2XMSAA的采样计数为2U,4XMSAA的采样计数为4U,8XMSAA的采样计数为8U,等等(用于“探测”可行MSAA级别…)您几乎总是希望禁用MSAA的采样计数为0U,启用MSAA的采样计数为1U 下面是我的工作MSAA代码(您应该能够填写其余部分)。请注意,我使用了DXGI_格式_D24_UNORM_S8_UINT和D3D11_DSV_维度_纹理2DMS,深度纹理和深度模具视图的格式值相同,采样计数和采样质量值相同 祝你好运
unsigned int SampleCount = 1U;
unsigned int SampleQuality = (SampleCount > 1U ? 1U : 0U);
//Create swap chain.
IDXGIFactory2* dxgiFactory2 = nullptr;
d3dResult = dxgiFactory->QueryInterface(__uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgiFactory2));
if (dxgiFactory2)
{
//DirectX 11.1 or later.
d3dResult = D3DDevice->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void**>(&D3DDevice1));
if (SUCCEEDED(d3dResult))
{
D3DDeviceContext->QueryInterface(__uuidof(ID3D11DeviceContext1), reinterpret_cast<void**>(&D3DDeviceContext1));
}
DXGI_SWAP_CHAIN_DESC1 swapChain;
ZeroMemory(&swapChain, sizeof(swapChain));
swapChain.Width = width;
swapChain.Height = height;
swapChain.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChain.SampleDesc.Count = SampleCount;
swapChain.SampleDesc.Quality = SampleQuality;
swapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChain.BufferCount = 2U;
d3dResult = dxgiFactory2->CreateSwapChainForHwnd(D3DDevice, w32Window, &swapChain, nullptr, nullptr, &SwapChain1);
if (SUCCEEDED(d3dResult))
{
d3dResult = SwapChain1->QueryInterface(__uuidof(IDXGISwapChain), reinterpret_cast<void**>(&SwapChain));
}
dxgiFactory2->Release();
}
else
{
//DirectX 11.0.
DXGI_SWAP_CHAIN_DESC swapChain;
ZeroMemory(&swapChain, sizeof(swapChain));
swapChain.BufferCount = 2U;
swapChain.BufferDesc.Width = width;
swapChain.BufferDesc.Height = height;
swapChain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChain.BufferDesc.RefreshRate.Numerator = 60U;
swapChain.BufferDesc.RefreshRate.Denominator = 1U;
swapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChain.OutputWindow = w32Window;
swapChain.SampleDesc.Count = SampleCount;
swapChain.SampleDesc.Quality = SampleQuality;
swapChain.Windowed = true;
d3dResult = dxgiFactory->CreateSwapChain(D3DDevice, &swapChain, &SwapChain);
}
//Disable Alt + Enter and Print Screen shortcuts.
dxgiFactory->MakeWindowAssociation(w32Window, DXGI_MWA_NO_PRINT_SCREEN | DXGI_MWA_NO_ALT_ENTER);
dxgiFactory->Release();
if (FAILED(d3dResult))
{
return false;
}
//Create render target view.
ID3D11Texture2D* backBuffer = nullptr;
d3dResult = SwapChain->GetBuffer(0U, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer));
if (FAILED(d3dResult))
{
return false;
}
d3dResult = D3DDevice->CreateRenderTargetView(backBuffer, nullptr, &RenderTargetView);
backBuffer->Release();
if (FAILED(d3dResult))
{
return false;
}
//Create depth stencil texture.
ID3D11Texture2D* DepthStencilTexture = nullptr;
D3D11_TEXTURE2D_DESC depthTextureLayout;
ZeroMemory(&depthTextureLayout, sizeof(depthTextureLayout));
depthTextureLayout.Width = width;
depthTextureLayout.Height = height;
depthTextureLayout.MipLevels = 1U;
depthTextureLayout.ArraySize = 1U;
depthTextureLayout.Usage = D3D11_USAGE_DEFAULT;
depthTextureLayout.CPUAccessFlags = 0U;
depthTextureLayout.MiscFlags = 0U;
depthTextureLayout.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthTextureLayout.SampleDesc.Count = SampleCount;
depthTextureLayout.SampleDesc.Quality = SampleQuality;
depthTextureLayout.BindFlags = D3D11_BIND_DEPTH_STENCIL;
d3dResult = D3DDevice->CreateTexture2D(&depthTextureLayout, nullptr, &DepthStencilTexture);
if (FAILED(d3dResult))
{
return false;
}
//Create depth stencil.
D3D11_DEPTH_STENCIL_DESC depthStencilLayout;
depthStencilLayout.DepthEnable = true;
depthStencilLayout.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilLayout.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilLayout.StencilEnable = true;
depthStencilLayout.StencilReadMask = 0xFF;
depthStencilLayout.StencilWriteMask = 0xFF;
depthStencilLayout.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilLayout.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilLayout.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilLayout.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthStencilLayout.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilLayout.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilLayout.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilLayout.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
ID3D11DepthStencilState* depthStencilState;
D3DDevice->CreateDepthStencilState(&depthStencilLayout, &depthStencilState);
D3DDeviceContext->OMSetDepthStencilState(depthStencilState, 1U);
//Create depth stencil view.
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewLayout;
ZeroMemory(&depthStencilViewLayout, sizeof(depthStencilViewLayout));
depthStencilViewLayout.Format = depthTextureLayout.Format;
depthStencilViewLayout.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
depthStencilViewLayout.Texture2D.MipSlice = 0U;
d3dResult = D3DDevice->CreateDepthStencilView(DepthStencilTexture, &depthStencilViewLayout, &DepthStencilView);
DepthStencilTexture->Release();
if (FAILED(d3dResult))
{
return false;
}
//Set output-merger render targets.
D3DDeviceContext->OMSetRenderTargets(1U, &RenderTargetView, DepthStencilView);
unsigned int SampleCount=1U;
无符号整数采样质量=(采样计数>1U?1U:0U);
//创建交换链。
IDXGIFactory2*dxgiFactory2=nullptr;
d3dResult=dxgiFactory->QueryInterface(uuuIdof(IDXGIFactory2),重新解释cast(&dxgiFactory2));
if(dxgiFactory2)
{
//DirectX 11.1或更高版本。
d3dResult=D3DDevice->QueryInterface(uu uuidof(ID3D11Device1),重新解释转换(&D3DDevice1));
if(成功(d3dResult))
{
D3DDeviceContext->QueryInterface(uu uuidof(ID3D11DeviceContext1),重新解释转换(&D3DDeviceContext1));
}
DXGI交换链描述交换链;
零内存(&swapChain,sizeof(swapChain));
交换链宽度=宽度;
交换链高度=高度;
swapChain.Format=DXGI_Format_R8G8B8A8_UNORM;
swapChain.SampleDesc.Count=采样计数;
swapChain.SampleDesc.Quality=样本质量;
swapChain.BufferUsage=DXGI\u USAGE\u RENDER\u TARGET\u输出;
swapChain.BufferCount=2U;
d3dResult=dxgiFactory2->CreateSwapChainForHwnd(D3DDevice、W32窗口和swapChain、nullptr、nullptr和SwapChain1);
if(成功(d3dResult))
{
d3dResult=SwapChain1->QueryInterface(uu uuidof(IDXGISwapChain),重新解释转换(&SwapChain));
}
dxgiFactory2->Release();
}
其他的
{
//DirectX 11.0。
DXGI交换链描述交换链;
零内存(&swapChain,sizeof(swapChain));
swapChain.BufferCount=2U;
swapChain.BufferDesc.Width=宽度;
swapChain.BufferDesc.Height=高度;
swapChain.BufferDesc.Format=DXGI_Format_R8G8B8A8_UNORM;
swapChain.BufferDesc.RefreshRate.Numerator=60U;
swapChain.BufferDesc.RefreshRate.Denominor=1U;
swapChain.BufferUsage=DXGI\u USAGE\u RENDER\u TARGET\u输出;
swapChain.OutputWindow=W32窗口;
swapChain.SampleDesc.Count=采样计数;
swapChain.SampleDesc.Quality=样本质量;
swapChain.Windowed=true;
d3dResult=dxgiFactory->CreateSwapChain(D3DDevice,&swapChain,&swapChain);
}
//禁用Alt+Enter和打印屏幕快捷方式。
dxgiFactory->MakeWindowAssociation(W32窗口,DXGI_MWA_NO_PRINT_SCREEN | DXGI_MWA_NO_ALT_ENTER);
dxgiFactory->Release();
如果(失败(d3dResult))
{
返回false;
}
//创建渲染目标视图。
ID3D11Texture2D*backBuffer=nullptr;
d3dResult=SwapChain->GetBuffer(0U,u-uuidof(ID3D11Texture2D),重新解释强制转换(&backBuffer));
如果(失败(d3dResult))
{
返回false;
}
d3dResult=D3DDevice->CreateRenderTargetView(backBuffer、nullptr和RenderTargetView);
backBuffer->Release();
如果(失败(d3dResult))
{
返回false;
}
//创建深度模具纹理。
ID3D11Texture2D*DepthStencilTexture=nullptr;
D3D11_纹理2D_描述深度文本