D3D11_TEXTURE2D_DESC SpriteRenderer::SetPerBatchData(ID3D11ShaderResourceView* texture) { // Set per-batch constants VSPerBatchCB perBatch; // Get the viewport dimensions UINT numViewports = 1; D3D11_VIEWPORT vp; context->RSGetViewports(&numViewports, &vp); perBatch.ViewportSize = XMFLOAT2(static_cast<float>(vp.Width), static_cast<float>(vp.Height)); // Get the size of the texture ID3D11Resource* resource; ID3D11Texture2DPtr texResource; D3D11_TEXTURE2D_DESC desc; texture->GetResource(&resource); texResource.Attach(reinterpret_cast<ID3D11Texture2D*>(resource)); texResource->GetDesc(&desc); perBatch.TextureSize = XMFLOAT2(static_cast<float>(desc.Width), static_cast<float>(desc.Height)); // Copy it into the buffer D3D11_MAPPED_SUBRESOURCE mapped; DXCall(context->Map(vsPerBatchCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); CopyMemory(mapped.pData, &perBatch, sizeof(VSPerBatchCB)); context->Unmap(vsPerBatchCB, 0); return desc; }
void PostProcessorBase::PostProcess(ID3D11PixelShader* pixelShader, const wchar* name) { Assert_(context); Assert_(inputs.size() <= MaxInputs); D3DPERF_BeginEvent(0xFFFFFFFF, name); // Set the outputs ID3D11RenderTargetView** renderTargets = reinterpret_cast<ID3D11RenderTargetView**>(&outputs[0]); uint32 numRTs = static_cast<uint32>(outputs.size()); if(uaViews.size() == 0) context->OMSetRenderTargets(numRTs, renderTargets, nullptr); else { ID3D11UnorderedAccessView** uavs = reinterpret_cast<ID3D11UnorderedAccessView**>(&uaViews[0]); UINT numUAVs = static_cast<uint32>(uaViews.size()); UINT initialCounts[D3D11_PS_CS_UAV_REGISTER_COUNT] = { 0 }; context->OMSetRenderTargetsAndUnorderedAccessViews(numRTs, renderTargets, nullptr, numRTs, numUAVs, uavs, initialCounts); } // Set the input textures ID3D11ShaderResourceView** textures = reinterpret_cast<ID3D11ShaderResourceView**>(&inputs[0]); context->PSSetShaderResources(0, static_cast<uint32>(inputs.size()), textures); // Set the constants D3D11_MAPPED_SUBRESOURCE mapped; DXCall(context->Map(psConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); PSConstants* constants = reinterpret_cast<PSConstants*>(mapped.pData); for (size_t i = 0; i < inputs.size(); ++i) { if(inputs[i] == nullptr) { constants->InputSize[i].x = 0.0f; constants->InputSize[i].y = 0.0f; continue; } ID3D11Resource* resource; ID3D11Texture2DPtr texture; D3D11_TEXTURE2D_DESC desc; D3D11_SHADER_RESOURCE_VIEW_DESC srDesc; inputs[i]->GetDesc(&srDesc); uint32 mipLevel = srDesc.Texture2D.MostDetailedMip; inputs[i]->GetResource(&resource); texture.Attach(reinterpret_cast<ID3D11Texture2D*>(resource)); texture->GetDesc(&desc); constants->InputSize[i].x = static_cast<float>(std::max<uint32>(desc.Width / (1 << mipLevel), 1)); constants->InputSize[i].y = static_cast<float>(std::max<uint32>(desc.Height / (1 << mipLevel), 1)); } ID3D11Resource* resource; ID3D11Texture2DPtr texture; D3D11_TEXTURE2D_DESC desc; D3D11_RENDER_TARGET_VIEW_DESC rtDesc; outputs[0]->GetResource(&resource); outputs[0]->GetDesc(&rtDesc); uint32 mipLevel = rtDesc.Texture2D.MipSlice; texture.Attach(reinterpret_cast<ID3D11Texture2D*>(resource)); texture->GetDesc(&desc); constants->OutputSize.x = static_cast<float>(std::max<uint32>(desc.Width / (1 << mipLevel), 1)); constants->OutputSize.y = static_cast<float>(std::max<uint32>(desc.Height / (1 << mipLevel), 1)); context->Unmap(psConstants, 0); ID3D11Buffer* constantBuffers[1] = { psConstants }; context->PSSetConstantBuffers(0, 1, constantBuffers); // Set the viewports D3D11_VIEWPORT viewports[16]; for (UINT_PTR i = 0; i < 16; ++i) { viewports[i].Width = static_cast<float>(std::max<uint32>(desc.Width / (1 << mipLevel), 1)); viewports[i].Height = static_cast<float>(std::max<uint32>(desc.Height / (1 << mipLevel), 1)); viewports[i].TopLeftX = 0; viewports[i].TopLeftY = 0; viewports[i].MinDepth = 0.0f; viewports[i].MaxDepth = 1.0f; } context->RSSetViewports(static_cast<uint32>(outputs.size()), viewports); // Set the pixel shader context->PSSetShader(pixelShader, nullptr, 0); // Draw the quad context->DrawIndexed(6, 0, 0); // Clear the SRV's and RT's ID3D11ShaderResourceView* srViews[16] = { nullptr }; context->PSSetShaderResources(0, static_cast<uint32>(inputs.size()), srViews); ID3D11RenderTargetView* rtViews[16] = { nullptr }; context->OMSetRenderTargets(static_cast<uint32>(outputs.size() + uaViews.size()), rtViews, nullptr); inputs.clear(); outputs.clear(); uaViews.clear(); texture = nullptr; D3DPERF_EndEvent(); }
void PostProcessor::PostProcess(ID3D11PixelShader* pixelShader, const WCHAR* name) { _ASSERT(context); _ASSERT(inputs.size() <= MaxInputs); D3DPERF_BeginEvent(0xFFFFFFFF, name); // Set the outputs ID3D11RenderTargetView** renderTargets = reinterpret_cast<ID3D11RenderTargetView**>(&outputs[0]); context->OMSetRenderTargets(static_cast<UINT>(outputs.size()), renderTargets, NULL); // Set the input textures ID3D11ShaderResourceView** textures = reinterpret_cast<ID3D11ShaderResourceView**>(&inputs[0]); context->PSSetShaderResources(0, static_cast<UINT>(inputs.size()), textures); // Set the constants D3D11_MAPPED_SUBRESOURCE mapped; DXCall(context->Map(psConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); PSConstants* constants = reinterpret_cast<PSConstants*>(mapped.pData); for (size_t i = 0; i < inputs.size(); ++i) { ID3D11Resource* resource; ID3D11Texture2DPtr texture; D3D11_TEXTURE2D_DESC desc; D3D11_SHADER_RESOURCE_VIEW_DESC srDesc; inputs[i]->GetDesc(&srDesc); UINT mipLevel = srDesc.Texture2D.MostDetailedMip; inputs[i]->GetResource(&resource); texture.Attach(reinterpret_cast<ID3D11Texture2D*>(resource)); texture->GetDesc(&desc); constants->InputSize[i].x = static_cast<float>(max(desc.Width / (1 << mipLevel), 1)); constants->InputSize[i].y = static_cast<float>(max(desc.Height / (1 << mipLevel), 1)); } ID3D11Resource* resource; ID3D11Texture2DPtr texture; D3D11_TEXTURE2D_DESC desc; D3D11_RENDER_TARGET_VIEW_DESC rtDesc; outputs[0]->GetResource(&resource); outputs[0]->GetDesc(&rtDesc); UINT mipLevel = rtDesc.Texture2D.MipSlice; texture.Attach(reinterpret_cast<ID3D11Texture2D*>(resource)); texture->GetDesc(&desc); constants->OutputSize.x = static_cast<float>(max(desc.Width / (1 << mipLevel), 1)); constants->OutputSize.y = static_cast<float>(max(desc.Height / (1 << mipLevel), 1)); context->Unmap(psConstants, 0); ID3D11Buffer* constantBuffers[1] = { psConstants }; context->PSSetConstantBuffers(0, 1, constantBuffers); // Set the viewports D3D11_VIEWPORT viewports[16]; for (UINT_PTR i = 0; i < 16; ++i) { viewports[i].Width = static_cast<float>(max(desc.Width / (1 << mipLevel), 1)); viewports[i].Height = static_cast<float>(max(desc.Height / (1 << mipLevel), 1)); viewports[i].TopLeftX = 0; viewports[i].TopLeftY = 0; viewports[i].MinDepth = 0.0f; viewports[i].MaxDepth = 1.0f; } context->RSSetViewports(static_cast<UINT>(outputs.size()), viewports); // Set the pixel shader context->PSSetShader(pixelShader, NULL, 0); // Draw the quad context->DrawIndexed(6, 0, 0); inputs.erase(inputs.begin(), inputs.end()); outputs.erase(outputs.begin(), outputs.end()); texture = NULL; D3DPERF_EndEvent(); }