From 4175b29653a43c806504a3cd8e61cbc7f55106f4 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Tue, 20 Oct 2015 12:07:40 +0200 Subject: [PATCH] DirectX GUI rendering --- .../Rendering/DirectX/Rendering/Renderer.h | 5 + src/game/Rendering/DirectX/Renderer.cpp | 138 ++++++++++++++---- 2 files changed, 113 insertions(+), 30 deletions(-) diff --git a/include/Rendering/DirectX/Rendering/Renderer.h b/include/Rendering/DirectX/Rendering/Renderer.h index 53ad662..d43d81e 100644 --- a/include/Rendering/DirectX/Rendering/Renderer.h +++ b/include/Rendering/DirectX/Rendering/Renderer.h @@ -41,8 +41,11 @@ public: void Initialize() override; void Draw(RenderQueueCollection& rq) override; + D3D11_RASTERIZER_DESC m_RasterDesc; + IDXGISwapChain* m_SwapChain = nullptr; ID3D11Device* m_Device = nullptr; + ID3D11RasterizerState* m_RasterState = nullptr; ID3D11DeviceContext* m_DeviceContext = nullptr; ID3D11RenderTargetView* m_BackBuffer = nullptr; ID3D11DepthStencilView* m_DepthBuffer = nullptr; @@ -72,6 +75,8 @@ private: static DirectX::SimpleMath::Matrix GLMtoDXModelView(glm::mat4 m); static DirectX::SimpleMath::Matrix GLMtoDXProjection(glm::mat4 m); + void setDefaultRasterState(); + void setGUIRasterState(); static bool DepthSort(const std::shared_ptr &i, const std::shared_ptr &j) { return (i->Depth < j->Depth); } }; diff --git a/src/game/Rendering/DirectX/Renderer.cpp b/src/game/Rendering/DirectX/Renderer.cpp index 06b41ed..04acbca 100644 --- a/src/game/Rendering/DirectX/Renderer.cpp +++ b/src/game/Rendering/DirectX/Renderer.cpp @@ -21,7 +21,7 @@ void dd::Renderer::Initialize() monitor = glfwGetPrimaryMonitor(); } glfwWindowHint(GLFW_SAMPLES, 8); - m_Window = glfwCreateWindow(m_Resolution.Width, m_Resolution.Height, "DirectX 11", monitor, nullptr); + m_Window = glfwCreateWindow(m_Resolution.Width, m_Resolution.Height, "Squidout! (DirectX 11)", monitor, nullptr); if (!m_Window) { LOG_ERROR("GLFW: Failed to create window"); exit(EXIT_FAILURE); @@ -30,7 +30,7 @@ void dd::Renderer::Initialize() // Create default camera m_DefaultCamera = std::unique_ptr(new dd::Camera((float)m_Resolution.Width / m_Resolution.Height, 45.f, 0.01f, 5000.f)); - m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); + m_DefaultCamera->SetPosition(glm::vec3(0, 0, 0)); if (m_Camera == nullptr) { m_Camera = m_DefaultCamera.get(); } @@ -83,21 +83,7 @@ void dd::Renderer::Initialize() m_DeviceContext->OMSetRenderTargets(1, &m_BackBuffer, m_DepthBuffer); // Rasterizer settings - D3D11_RASTERIZER_DESC rd; - memset(&rd, 0, sizeof(D3D11_RASTERIZER_DESC)); - rd.AntialiasedLineEnable = false; - rd.CullMode = D3D11_CULL_BACK; - rd.DepthBias = 0; - rd.DepthBiasClamp = 0.0f; - rd.DepthClipEnable = true; - rd.FillMode = D3D11_FILL_SOLID; - rd.FrontCounterClockwise = true; - rd.MultisampleEnable = false; - rd.ScissorEnable = false; - rd.SlopeScaledDepthBias = 0.0f; - ID3D11RasterizerState* rasterState; - m_Device->CreateRasterizerState(&rd, &rasterState); - m_DeviceContext->RSSetState(rasterState); + setDefaultRasterState(); D3D11_BLEND_DESC blend = { 0 }; for (int i = 0; i < 1; i++) { @@ -114,15 +100,6 @@ void dd::Renderer::Initialize() m_Device->CreateBlendState(&blend, &blendState); m_DeviceContext->OMSetBlendState(blendState, nullptr, 0xffffffff); - // Set the viewport - D3D11_VIEWPORT viewport = { 0 }; - viewport.TopLeftX = 0; - viewport.TopLeftY = 0; - viewport.Width = m_Resolution.Width; - viewport.Height = m_Resolution.Height; - viewport.MinDepth = 0; - viewport.MaxDepth = 1; - m_DeviceContext->RSSetViewports(1, &viewport); shaderProgram = new ShaderProgram(); shaderProgram->CompileVertexShader(L"Shaders/DirectX/vertex.hlsl"); @@ -178,10 +155,21 @@ void dd::Renderer::Draw(RenderQueueCollection& rq) rq.Forward.Jobs.sort(dd::Renderer::DepthSort); + m_DeviceContext->OMSetRenderTargets(1, &m_BackBuffer, m_DepthBuffer); float color[] = { 0.f, 0.f, 0.f, 1.f }; m_DeviceContext->ClearRenderTargetView(m_BackBuffer, color); m_DeviceContext->ClearDepthStencilView(m_DepthBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0); + // Set the viewport + D3D11_VIEWPORT viewport = { 0 }; + viewport.TopLeftX = 0; + viewport.TopLeftY = 0; + viewport.Width = m_Resolution.Width; + viewport.Height = m_Resolution.Height; + viewport.MinDepth = 0; + viewport.MaxDepth = 1; + m_DeviceContext->RSSetViewports(1, &viewport); + Matrix proj = DirectX::SimpleMath::Matrix::CreatePerspectiveFieldOfView(m_Camera->m_FOV, m_Camera->m_AspectRatio, m_Camera->m_NearClip, m_Camera->m_FarClip); // Right-handed Matrix view = Matrix::CreateTranslation(Vector3(m_Camera->Position().x, m_Camera->Position().y, -m_Camera->Position().z)); @@ -199,10 +187,6 @@ void dd::Renderer::Draw(RenderQueueCollection& rq) i++; } } - - - //constantBufferStruct.LightPositions[0] = Vector4(2.0f, 0.0f, 5.0f, 0.0f); - //constantBufferStruct.LightPositions[1] = Vector4(-2.0f, 0.0f, 5.0f, 0.0f); for (auto &job : rq.Deferred) { auto modelJob = std::dynamic_pointer_cast(job); @@ -278,6 +262,54 @@ void dd::Renderer::Draw(RenderQueueCollection& rq) } } + m_DeviceContext->OMSetRenderTargets(1, &m_BackBuffer, nullptr); + for (auto &job : rq.GUI) { + auto frameJob = std::dynamic_pointer_cast(job); + if (frameJob) { + Rectangle& vp = frameJob->Viewport; + glViewport(vp.X, m_Resolution.Height - vp.Y - vp.Height, vp.Width, vp.Height); + Rectangle& sc = frameJob->Scissor; + if (sc == Rectangle()) { + glDisable(GL_SCISSOR_TEST); + } else { + glEnable(GL_SCISSOR_TEST); + glScissor(sc.X, m_Resolution.Height - sc.Y - sc.Height, sc.Width, sc.Height); + } + + D3D11_VIEWPORT viewport = { 0 }; + viewport.TopLeftX = vp.X; + viewport.TopLeftY = vp.Y; + viewport.Width = vp.Width; + viewport.Height = vp.Height; + viewport.MinDepth = 0; + viewport.MaxDepth = 1; + m_DeviceContext->RSSetViewports(1, &viewport); + + proj = Matrix::Identity; + view = Matrix::Identity; + Matrix model = Matrix::CreateScale(2.f); + constantBufferStruct.MVP = model * view * proj; + constantBufferStruct.InverseTransposeViewModel = (model * view).Transpose().Invert(); + constantBufferStruct.Color = Vector4(glm::value_ptr(frameJob->Color)); + + m_DeviceContext->UpdateSubresource(constantBuffer, 0, 0, &constantBufferStruct, 0, 0); + m_DeviceContext->PSSetSamplers(0, 1, &m_SamplerState); + + m_DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + UINT stride = sizeof(Model::Vertex); + UINT offset = 0; + m_DeviceContext->IASetVertexBuffers(0, 1, &m_UnitQuad->VertexBuffer, &stride, &offset); + m_DeviceContext->IASetIndexBuffer(m_UnitQuad->IndexBuffer, DXGI_FORMAT_R32_UINT, 0); + + if (frameJob->DiffuseTexture != nullptr) { + m_DeviceContext->PSSetShaderResources(0, 1, &frameJob->DiffuseTexture->m_ShaderResourceView); + } + //m_DeviceContext->Draw(modelJob->EndIndex - modelJob->StartIndex + 1, modelJob->StartIndex); + m_DeviceContext->DrawIndexed(m_UnitQuad->m_Indices.size(), 0, 0); + + continue; + } + } //constantBufferStruct.ModelMatrix = GLMtoDX(glm::mat4(1.f)); //constantBufferStruct.ViewMatrix = GLMtoDX(m_Camera->ViewMatrix()); //constantBufferStruct.ProjectionMatrix = proj; @@ -342,3 +374,49 @@ DirectX::SimpleMath::Matrix dd::Renderer::GLMtoDXProjection(glm::mat4 gm) ); return dm; } + +void dd::Renderer::setDefaultRasterState() +{ + if (m_RasterState != nullptr) { + m_RasterState->Release(); + m_RasterState = nullptr; + } + + D3D11_RASTERIZER_DESC rd; + memset(&rd, 0, sizeof(D3D11_RASTERIZER_DESC)); + rd.AntialiasedLineEnable = false; + rd.CullMode = D3D11_CULL_BACK; + rd.DepthBias = 0; + rd.DepthBiasClamp = 0.0f; + rd.DepthClipEnable = true; + rd.FillMode = D3D11_FILL_SOLID; + rd.FrontCounterClockwise = true; + rd.MultisampleEnable = false; + rd.ScissorEnable = false; + rd.SlopeScaledDepthBias = 0.0f; + m_Device->CreateRasterizerState(&rd, &m_RasterState); + m_DeviceContext->RSSetState(m_RasterState); +} + +void dd::Renderer::setGUIRasterState() +{ + if (m_RasterState != nullptr) { + m_RasterState->Release(); + m_RasterState = nullptr; + } + + D3D11_RASTERIZER_DESC rd; + memset(&rd, 0, sizeof(D3D11_RASTERIZER_DESC)); + rd.AntialiasedLineEnable = false; + rd.CullMode = D3D11_CULL_BACK; + rd.DepthBias = 0; + rd.DepthBiasClamp = 0.0f; + rd.DepthClipEnable = false; + rd.FillMode = D3D11_FILL_SOLID; + rd.FrontCounterClockwise = true; + rd.MultisampleEnable = false; + rd.ScissorEnable = false; + rd.SlopeScaledDepthBias = 0.0f; + m_Device->CreateRasterizerState(&rd, &m_RasterState); + m_DeviceContext->RSSetState(m_RasterState); +}