diff options
| author | Miles Macklin <[email protected]> | 2018-03-19 15:10:24 +1300 |
|---|---|---|
| committer | Miles Macklin <[email protected]> | 2018-03-19 15:10:24 +1300 |
| commit | 8ee05c79ae1748ef132a12e4fb0af284899faec6 (patch) | |
| tree | 82bd5aa1892e28ce7886b6cfeafe66a47ff38e67 /demo/d3d11 | |
| parent | Flex 1.2 (beta 2) (diff) | |
| download | flex-8ee05c79ae1748ef132a12e4fb0af284899faec6.tar.xz flex-8ee05c79ae1748ef132a12e4fb0af284899faec6.zip | |
Flex 1.2.0 release
Diffstat (limited to 'demo/d3d11')
| -rw-r--r-- | demo/d3d11/demoContextD3D11.cpp | 78 | ||||
| -rw-r--r-- | demo/d3d11/demoContextD3D11.h | 2 | ||||
| -rw-r--r-- | demo/d3d11/fluidRenderD3D11.cpp | 102 | ||||
| -rw-r--r-- | demo/d3d11/fluidRenderD3D11.h | 8 |
4 files changed, 172 insertions, 18 deletions
diff --git a/demo/d3d11/demoContextD3D11.cpp b/demo/d3d11/demoContextD3D11.cpp index 0eaf0f4..11227e1 100644 --- a/demo/d3d11/demoContextD3D11.cpp +++ b/demo/d3d11/demoContextD3D11.cpp @@ -16,7 +16,7 @@ #include "appD3D11Ctx.h" -#include "../d3d/demoContext.h" +#include "demoContext.h" #include "../d3d/loader.h" #include <d3d11.h> @@ -82,6 +82,7 @@ DemoContextD3D11::DemoContextD3D11() m_fluidResolvedTarget = nullptr; m_fluidResolvedTargetSRV = nullptr; + m_fluidResolvedStage = nullptr; m_debugLineRender = new DebugLineRenderD3D11; m_meshRenderer = new MeshRendererD3D11; @@ -139,6 +140,7 @@ DemoContextD3D11::~DemoContextD3D11() COMRelease(m_fluidResolvedTarget); COMRelease(m_fluidResolvedTargetSRV); + COMRelease(m_fluidResolvedStage); delete m_immediateMesh; delete m_debugLineRender; @@ -264,10 +266,10 @@ void DemoContextD3D11::_onWindowSizeChanged(int width, int height, bool minimize COMRelease(m_fluidResolvedTarget); COMRelease(m_fluidResolvedTargetSRV); + COMRelease(m_fluidResolvedStage); } // Recreate... - ID3D11Device* device = m_appGraphCtxD3D11->m_device; // resolved texture target (for refraction / scene sampling) @@ -300,6 +302,15 @@ void DemoContextD3D11::_onWindowSizeChanged(int width, int height, bool minimize { return; } + + texDesc.Usage = D3D11_USAGE_STAGING; + texDesc.BindFlags = 0u; + texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + + if (FAILED(device->CreateTexture2D(&texDesc, nullptr, &m_fluidResolvedStage))) + { + return; + } } } @@ -347,6 +358,29 @@ void DemoContextD3D11::presentFrame(bool fullsync) AppGraphCtxFramePresent(m_appGraphCtx, fullsync); } +void DemoContextD3D11::readFrame(int* buffer, int width, int height) +{ + auto deviceContext = m_appGraphCtxD3D11->m_deviceContext; + if (m_msaaSamples > 1) + { + deviceContext->ResolveSubresource(m_fluidResolvedTarget, 0, m_appGraphCtxD3D11->m_backBuffer, 0, DXGI_FORMAT_R8G8B8A8_UNORM); + deviceContext->CopyResource(m_fluidResolvedStage, m_fluidResolvedTarget); + } + else + { + deviceContext->CopyResource(m_fluidResolvedStage, m_appGraphCtxD3D11->m_backBuffer); + } + + D3D11_MAPPED_SUBRESOURCE mapped; + deviceContext->Map(m_fluidResolvedStage, 0u, D3D11_MAP_READ, 0, &mapped); + // y-coordinate is flipped for DirectX + for (int i = 0; i < height; i++) + { + memcpy(buffer + (width * i), ((int *)mapped.pData) + (width * (height - i)), width * sizeof(int)); + } + deviceContext->Unmap(m_fluidResolvedStage, 0u); +} + void DemoContextD3D11::getViewRay(int x, int y, Vec3& origin, Vec3& dir) { using namespace DirectX; @@ -902,6 +936,11 @@ void DemoContextD3D11::drawPoints(FluidRenderBuffers* buffersIn, int n, int offs m_pointRenderer->draw(¶ms, buffers.m_positions.Get(), buffers.m_densities.Get(), buffers.m_indices.Get(), n, offset); } +#if 0 +extern Mesh* g_mesh; +void DrawShapes(); +#endif + void DemoContextD3D11::renderEllipsoids(FluidRenderer* rendererIn, FluidRenderBuffers* buffersIn, int n, int offset, float radius, float screenWidth, float screenAspect, float fov, Vec3 lightPos, Vec3 lightTarget, Matrix44 lightTransform, ShadowMap* shadowMap, Vec4 color, float blur, float ior, bool debug) { FluidRenderBuffersD3D11& buffers = *reinterpret_cast<FluidRenderBuffersD3D11*>(buffersIn); @@ -914,7 +953,7 @@ void DemoContextD3D11::renderEllipsoids(FluidRenderer* rendererIn, FluidRenderBu FluidDrawParamsD3D params; params.renderMode = FLUID_RENDER_SOLID; - params.cullMode = FLUID_CULL_BACK; + params.cullMode = FLUID_CULL_NONE;// FLUID_CULL_BACK; params.model = (const XMMATRIX&)Matrix44::kIdentity; params.view = (const XMMATRIX&)m_view; params.projection = (XMMATRIX&)m_proj; @@ -927,15 +966,32 @@ void DemoContextD3D11::renderEllipsoids(FluidRenderer* rendererIn, FluidRenderBu params.invViewport = float3(1.0f / screenWidth, screenAspect / screenWidth, 1.0f); params.invProjection = float3(screenAspect * viewHeight, viewHeight, 1.0f); + // make sprites larger to get smoother thickness texture + const float thicknessScale = 4.0f; + params.pointRadius = thicknessScale * radius; + params.shadowMap = (ShadowMapD3D*)m_shadowMap; - renderer.m_depthTexture.bindAndClear(m_appGraphCtxD3D11->m_deviceContext); + renderer.m_thicknessTexture.bindAndClear(m_appGraphCtxD3D11->m_deviceContext); +#if 0 + // This seems redundant. + { + m_meshDrawParams.renderStage = MESH_DRAW_LIGHT; + m_meshDrawParams.cullMode = MESH_CULL_NONE; - // draw static shapes into depth buffer - //DrawShapes(); + if (g_mesh) + DrawMesh(g_mesh, Vec3(1.0f)); - renderer.drawEllipsoids(¶ms, &buffers); + DrawShapes(); + m_meshDrawParams.renderStage = MESH_DRAW_NULL; + m_meshDrawParams.cullMode = MESH_CULL_BACK; + } +#endif + renderer.drawThickness(¶ms, &buffers); + + renderer.m_depthTexture.bindAndClear(m_appGraphCtxD3D11->m_deviceContext); + renderer.drawEllipsoids(¶ms, &buffers); //--------------------------------------------------------------- // build smooth depth @@ -949,7 +1005,7 @@ void DemoContextD3D11::renderEllipsoids(FluidRenderer* rendererIn, FluidRenderBu params.debug = debug; renderer.drawBlurDepth(¶ms); - + //--------------------------------------------------------------- // composite @@ -975,17 +1031,17 @@ void DemoContextD3D11::renderEllipsoids(FluidRenderer* rendererIn, FluidRenderBu params.lightPos = (const float3&)lightPos; params.lightDir = (const float3&)-Normalize(lightTarget - lightPos); params.lightTransform = (const XMMATRIX&)(ConvertToD3DProjection(lightTransform)); - + // Resolve MS back buffer/copy if (m_msaaSamples > 1) { deviceContext->ResolveSubresource(m_fluidResolvedTarget, 0, m_appGraphCtxD3D11->m_backBuffer, 0, DXGI_FORMAT_R8G8B8A8_UNORM); - } + } else { deviceContext->CopyResource(m_fluidResolvedTarget, m_appGraphCtxD3D11->m_backBuffer); } - + renderer.drawComposite(¶ms, m_fluidResolvedTargetSRV); deviceContext->OMSetBlendState(nullptr, 0, 0xffff); diff --git a/demo/d3d11/demoContextD3D11.h b/demo/d3d11/demoContextD3D11.h index e01dfe2..1183d3d 100644 --- a/demo/d3d11/demoContextD3D11.h +++ b/demo/d3d11/demoContextD3D11.h @@ -20,6 +20,7 @@ public: virtual void startFrame(Vec4 colorIn); virtual void endFrame(); virtual void presentFrame(bool fullsync); + virtual void readFrame(int* backbuffer, int width, int height); virtual void getViewRay(int x, int y, Vec3& origin, Vec3& dir); virtual void setView(Matrix44 view, Matrix44 projection); virtual void renderEllipsoids(FluidRenderer* renderer, FluidRenderBuffers* buffers, int n, int offset, float radius, float screenWidth, float screenAspect, float fov, Vec3 lightPos, Vec3 lightTarget, Matrix44 lightTransform, ::ShadowMap* shadowMap, Vec4 color, float blur, float ior, bool debug); @@ -85,6 +86,7 @@ protected: // Target texture resolved/copied to for combination rendering from for water surface ID3D11Texture2D* m_fluidResolvedTarget; ID3D11ShaderResourceView* m_fluidResolvedTargetSRV; + ID3D11Texture2D* m_fluidResolvedStage; AppGraphCtx* m_appGraphCtx; AppGraphCtxD3D11* m_appGraphCtxD3D11; diff --git a/demo/d3d11/fluidRenderD3D11.cpp b/demo/d3d11/fluidRenderD3D11.cpp index fa36f9b..1b75030 100644 --- a/demo/d3d11/fluidRenderD3D11.cpp +++ b/demo/d3d11/fluidRenderD3D11.cpp @@ -23,6 +23,9 @@ #include "fluidRenderD3D11.h" +#include "../d3d/shaders/pointThicknessVS.hlsl.h" +#include "../d3d/shaders/pointThicknessGS.hlsl.h" +#include "../d3d/shaders/pointThicknessPS.hlsl.h" #include "../d3d/shaders/ellipsoidDepthVS.hlsl.h" #include "../d3d/shaders/ellipsoidDepthGS.hlsl.h" #include "../d3d/shaders/ellipsoidDepthPS.hlsl.h" @@ -54,11 +57,30 @@ void FluidRendererD3D11::init(ID3D11Device* device, ID3D11DeviceContext* context m_sceneWidth = width; m_sceneHeight = height; + m_thicknessTexture.init(device, width, height); m_depthTexture.init(device, width, height); m_depthSmoothTexture.init(device, width, height, false); m_device = device; m_deviceContext = context; + + // create the input layout + { + D3D11_INPUT_ELEMENT_DESC inputElementDescs[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "U", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "V", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "W", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + }; + + m_device->CreateInputLayout(inputElementDescs, 4, g_pointThicknessVS, sizeof(g_pointThicknessVS), &m_pointThicknessLayout); + } + + // create the shaders + m_device->CreateVertexShader(g_pointThicknessVS, sizeof(g_pointThicknessVS), nullptr, &m_pointThicknessVs); + m_device->CreateGeometryShader(g_pointThicknessGS, sizeof(g_pointThicknessGS), nullptr, &m_pointThicknessGs); + m_device->CreatePixelShader(g_pointThicknessPS, sizeof(g_pointThicknessPS), nullptr, &m_pointThicknessPs); // create the input layout { @@ -180,6 +202,72 @@ void FluidRendererD3D11::_createScreenQuad() } } +void FluidRendererD3D11::drawThickness(const FluidDrawParamsD3D* params, const FluidRenderBuffersD3D11* buffers) +{ + ID3D11DeviceContext* deviceContext = m_deviceContext; + + // update constant buffer + { + + D3D11_BUFFER_DESC desc; + m_constantBuffer->GetDesc(&desc); + + D3D11_MAPPED_SUBRESOURCE mappedResource = {}; + if (SUCCEEDED(deviceContext->Map(m_constantBuffer.Get(), 0u, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource))) + { + Hlsl::FluidShaderConst constBuf; + RenderParamsUtilD3D::calcFluidConstantBuffer(*params, constBuf); + memcpy(mappedResource.pData, &constBuf, sizeof(Hlsl::FluidShaderConst)); + deviceContext->Unmap(m_constantBuffer.Get(), 0u); + } + } + + deviceContext->VSSetShader(m_pointThicknessVs.Get(), nullptr, 0u); + deviceContext->GSSetShader(m_pointThicknessGs.Get(), nullptr, 0u); + deviceContext->PSSetShader(m_pointThicknessPs.Get(), nullptr, 0u); + + deviceContext->IASetInputLayout(m_pointThicknessLayout.Get()); + deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); + + deviceContext->VSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf()); + deviceContext->GSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf()); + deviceContext->PSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf()); + + ID3D11Buffer* vertexBuffers[4] = + { + buffers->m_positions.Get(), + buffers->m_anisotropiesArr[0].Get(), + buffers->m_anisotropiesArr[1].Get(), + buffers->m_anisotropiesArr[2].Get() + }; + + unsigned int vertexBufferStrides[4] = + { + sizeof(float4), + sizeof(float4), + sizeof(float4), + sizeof(float4) + }; + + unsigned int vertexBufferOffsets[4] = { 0 }; + + deviceContext->IASetVertexBuffers(0, 4, vertexBuffers, vertexBufferStrides, vertexBufferOffsets); + deviceContext->IASetIndexBuffer(buffers->m_indices.Get(), DXGI_FORMAT_R32_UINT, 0u); + + float depthSign = DirectX::XMVectorGetW(params->projection.r[2]); + if (depthSign < 0.f) + { + deviceContext->RSSetState(m_rasterizerState[params->renderMode][params->cullMode].Get()); + } + + deviceContext->DrawIndexed(params->n, params->offset, 0); + + if (depthSign < 0.f) + { + deviceContext->RSSetState(nullptr); + } +} + void FluidRendererD3D11::drawEllipsoids(const FluidDrawParamsD3D* params, const FluidRenderBuffersD3D11* buffers) { ID3D11DeviceContext* deviceContext = m_deviceContext; @@ -211,7 +299,7 @@ void FluidRendererD3D11::drawEllipsoids(const FluidDrawParamsD3D* params, const deviceContext->GSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf()); deviceContext->PSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf()); - ID3D11Buffer* vertexBuffers[4] = + ID3D11Buffer* vertexBuffers[4] = { buffers->m_positions.Get(), buffers->m_anisotropiesArr[0].Get(), @@ -237,7 +325,7 @@ void FluidRendererD3D11::drawEllipsoids(const FluidDrawParamsD3D* params, const { deviceContext->RSSetState(m_rasterizerState[params->renderMode][params->cullMode].Get()); } - + deviceContext->DrawIndexed(params->n, params->offset, 0); if (depthSign < 0.f) @@ -266,8 +354,11 @@ void FluidRendererD3D11::drawBlurDepth(const FluidDrawParamsD3D* params) deviceContext->GSSetShader(nullptr, nullptr, 0u); deviceContext->PSSetShader(m_blurDepthPs.Get(), nullptr, 0u); - ID3D11ShaderResourceView* srvs[1] = { m_depthTexture.m_backSrv.Get() }; - deviceContext->PSSetShaderResources(0, 1, srvs); + ID3D11ShaderResourceView* srvs[2] = { m_depthTexture.m_backSrv.Get(), m_thicknessTexture.m_backSrv.Get() }; + deviceContext->PSSetShaderResources(0, 2, srvs); + + ID3D11SamplerState* samps[1] = { m_thicknessTexture.m_linearSampler.Get() }; + deviceContext->PSSetSamplers(0, 1, samps); deviceContext->IASetInputLayout(m_passThroughLayout.Get()); deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); @@ -331,8 +422,7 @@ void FluidRendererD3D11::drawComposite(const FluidDrawParamsD3D* params, ID3D11S depthMap->m_linearSampler.Get() , shadowMap->m_linearSampler.Get() }; - deviceContext->PSSetSamplers(0, 2, samps); - + deviceContext->PSSetSamplers(0, 2, samps); deviceContext->IASetInputLayout(m_passThroughLayout.Get()); deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); diff --git a/demo/d3d11/fluidRenderD3D11.h b/demo/d3d11/fluidRenderD3D11.h index ad59534..fc48f16 100644 --- a/demo/d3d11/fluidRenderD3D11.h +++ b/demo/d3d11/fluidRenderD3D11.h @@ -65,6 +65,7 @@ struct FluidRendererD3D11 { void init(ID3D11Device* device, ID3D11DeviceContext* context, int width, int height); + void drawThickness(const FluidDrawParamsD3D* params, const FluidRenderBuffersD3D11* buffers); void drawEllipsoids(const FluidDrawParamsD3D* params, const FluidRenderBuffersD3D11* buffers); void drawBlurDepth(const FluidDrawParamsD3D* params); void drawComposite(const FluidDrawParamsD3D* params, ID3D11ShaderResourceView* sceneMap); @@ -79,6 +80,11 @@ struct FluidRendererD3D11 ID3D11Device* m_device; ID3D11DeviceContext* m_deviceContext; + ComPtr<ID3D11InputLayout> m_pointThicknessLayout; + ComPtr<ID3D11VertexShader> m_pointThicknessVs; + ComPtr<ID3D11GeometryShader> m_pointThicknessGs; + ComPtr<ID3D11PixelShader> m_pointThicknessPs; + ComPtr<ID3D11InputLayout> m_ellipsoidDepthLayout; ComPtr<ID3D11VertexShader> m_ellipsoidDepthVs; ComPtr<ID3D11GeometryShader> m_ellipsoidDepthGs; @@ -98,9 +104,9 @@ struct FluidRendererD3D11 ComPtr<ID3D11Buffer> m_quadVertexBuffer; ComPtr<ID3D11Buffer> m_quadIndexBuffer; + RenderTargetD3D11 m_thicknessTexture; RenderTargetD3D11 m_depthTexture; RenderTargetD3D11 m_depthSmoothTexture; - RenderTargetD3D11 m_thicknessTexture; int m_sceneWidth; int m_sceneHeight; |