summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/D3D/Render.cpp
diff options
context:
space:
mode:
authorAnthony <Helios747@users.noreply.github.com>2018-02-23 09:18:19 -0800
committerGitHub <noreply@github.com>2018-02-23 09:18:19 -0800
commitb66f96c617c87d0edbc4a9cb84c8c8e18ead915c (patch)
tree11876004e4d5c0536a2382a9d7fd380488d47fbe /Source/Core/VideoBackends/D3D/Render.cpp
parenta62343bd4fe2ea425b8543d60922bb3d54d39ab2 (diff)
parentfec6bb4d5676de8834c90d93127c1459f46f8dcb (diff)
Merge pull request #6042 from stenzek/videocommon-pipelines
VideoCommon pipelines ("Abstract Pipeline")
Diffstat (limited to 'Source/Core/VideoBackends/D3D/Render.cpp')
-rw-r--r--Source/Core/VideoBackends/D3D/Render.cpp167
1 files changed, 138 insertions, 29 deletions
diff --git a/Source/Core/VideoBackends/D3D/Render.cpp b/Source/Core/VideoBackends/D3D/Render.cpp
index 0e8c822d23..f1751e44f2 100644
--- a/Source/Core/VideoBackends/D3D/Render.cpp
+++ b/Source/Core/VideoBackends/D3D/Render.cpp
@@ -13,6 +13,7 @@
#include <strsafe.h>
#include <tuple>
+#include "Common/Assert.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MathUtil.h"
@@ -23,6 +24,8 @@
#include "VideoBackends/D3D/D3DBase.h"
#include "VideoBackends/D3D/D3DState.h"
#include "VideoBackends/D3D/D3DUtil.h"
+#include "VideoBackends/D3D/DXPipeline.h"
+#include "VideoBackends/D3D/DXShader.h"
#include "VideoBackends/D3D/DXTexture.h"
#include "VideoBackends/D3D/FramebufferManager.h"
#include "VideoBackends/D3D/GeometryShaderCache.h"
@@ -41,6 +44,12 @@
namespace DX11
{
+// Reserve 512KB for vertices, and 64KB for uniforms.
+// This should be sufficient for our usages, and if more is required,
+// we split it into multiple draws.
+constexpr u32 UTILITY_VBO_SIZE = 512 * 1024;
+constexpr u32 UTILITY_UBO_SIZE = 64 * 1024;
+
// Nvidia stereo blitting struct defined in "nvstereo.h" from the Nvidia SDK
typedef struct _Nv_Stereo_Image_Header
{
@@ -165,6 +174,16 @@ void Renderer::SetupDeviceObjects()
D3D::SetDebugObjectName(m_reset_rast_state, "rasterizer state for Renderer::ResetAPIState");
m_screenshot_texture = nullptr;
+
+ CD3D11_BUFFER_DESC vbo_desc(UTILITY_VBO_SIZE, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC,
+ D3D11_CPU_ACCESS_WRITE);
+ hr = D3D::device->CreateBuffer(&vbo_desc, nullptr, &m_utility_vertex_buffer);
+ CHECK(SUCCEEDED(hr), "Create utility VBO");
+
+ CD3D11_BUFFER_DESC ubo_desc(UTILITY_UBO_SIZE, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC,
+ D3D11_CPU_ACCESS_WRITE);
+ hr = D3D::device->CreateBuffer(&ubo_desc, nullptr, &m_utility_uniform_buffer);
+ CHECK(SUCCEEDED(hr), "Create utility UBO");
}
// Kill off all device objects
@@ -184,6 +203,8 @@ void Renderer::TeardownDeviceObjects()
SAFE_RELEASE(m_reset_rast_state);
SAFE_RELEASE(m_screenshot_texture);
SAFE_RELEASE(m_3d_vision_texture);
+ SAFE_RELEASE(m_utility_vertex_buffer);
+ SAFE_RELEASE(m_utility_uniform_buffer);
}
void Renderer::Create3DVisionTexture(int width, int height)
@@ -229,6 +250,109 @@ void Renderer::RenderText(const std::string& text, int left, int top, u32 color)
D3D::DrawTextScaled(static_cast<float>(left), static_cast<float>(top), 20.f, 0.0f, color, text);
}
+std::unique_ptr<AbstractShader> Renderer::CreateShaderFromSource(ShaderStage stage,
+ const char* source, size_t length)
+{
+ return DXShader::CreateFromSource(stage, source, length);
+}
+
+std::unique_ptr<AbstractShader> Renderer::CreateShaderFromBinary(ShaderStage stage,
+ const void* data, size_t length)
+{
+ return DXShader::CreateFromBinary(stage, data, length);
+}
+
+std::unique_ptr<AbstractPipeline> Renderer::CreatePipeline(const AbstractPipelineConfig& config)
+{
+ return DXPipeline::Create(config);
+}
+
+void Renderer::UpdateUtilityUniformBuffer(const void* uniforms, u32 uniforms_size)
+{
+ _dbg_assert_(VIDEO, uniforms_size > 0 && uniforms_size < UTILITY_UBO_SIZE);
+ D3D11_MAPPED_SUBRESOURCE mapped;
+ HRESULT hr = D3D::context->Map(m_utility_uniform_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
+ CHECK(SUCCEEDED(hr), "Map utility UBO");
+ std::memcpy(mapped.pData, uniforms, uniforms_size);
+ D3D::context->Unmap(m_utility_uniform_buffer, 0);
+}
+
+void Renderer::UpdateUtilityVertexBuffer(const void* vertices, u32 vertex_stride, u32 num_vertices)
+{
+ D3D11_MAPPED_SUBRESOURCE mapped;
+ HRESULT hr = D3D::context->Map(m_utility_vertex_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
+ CHECK(SUCCEEDED(hr), "Map utility VBO");
+ std::memcpy(mapped.pData, vertices, num_vertices * vertex_stride);
+ D3D::context->Unmap(m_utility_vertex_buffer, 0);
+}
+
+void Renderer::SetPipeline(const AbstractPipeline* pipeline)
+{
+ const DXPipeline* dx_pipeline = static_cast<const DXPipeline*>(pipeline);
+
+ D3D::stateman->SetRasterizerState(dx_pipeline->GetRasterizerState());
+ D3D::stateman->SetDepthState(dx_pipeline->GetDepthState());
+ D3D::stateman->SetBlendState(dx_pipeline->GetBlendState());
+ D3D::stateman->SetPrimitiveTopology(dx_pipeline->GetPrimitiveTopology());
+ D3D::stateman->SetInputLayout(dx_pipeline->GetInputLayout());
+ D3D::stateman->SetVertexShader(dx_pipeline->GetVertexShader());
+ D3D::stateman->SetGeometryShader(dx_pipeline->GetGeometryShader());
+ D3D::stateman->SetPixelShader(dx_pipeline->GetPixelShader());
+}
+
+void Renderer::DrawUtilityPipeline(const void* uniforms, u32 uniforms_size, const void* vertices,
+ u32 vertex_stride, u32 num_vertices)
+{
+ // Textures are fine, they're set directly via SetTexture.
+ // Since samplers are set via gx_state, we need to fix this up here.
+ for (size_t stage = 0; stage < m_gx_state.samplers.size(); stage++)
+ D3D::stateman->SetSampler(stage, m_state_cache.Get(m_gx_state.samplers[stage]));
+
+ // Copy in uniforms.
+ if (uniforms_size > 0)
+ {
+ UpdateUtilityUniformBuffer(uniforms, uniforms_size);
+ D3D::stateman->SetVertexConstants(m_utility_uniform_buffer);
+ D3D::stateman->SetPixelConstants(m_utility_uniform_buffer);
+ D3D::stateman->SetGeometryConstants(m_utility_uniform_buffer);
+ }
+
+ // If the vertices are larger than our buffer, we need to break it up into multiple draws.
+ const char* vertices_ptr = static_cast<const char*>(vertices);
+ while (num_vertices > 0)
+ {
+ u32 vertices_this_draw = num_vertices;
+ if (vertices_ptr)
+ {
+ vertices_this_draw = std::min(vertices_this_draw, UTILITY_VBO_SIZE / vertex_stride);
+ _dbg_assert_(VIDEO, vertices_this_draw > 0);
+ UpdateUtilityVertexBuffer(vertices_ptr, vertex_stride, vertices_this_draw);
+ D3D::stateman->SetVertexBuffer(m_utility_vertex_buffer, vertex_stride, 0);
+ }
+
+ // Apply pending state and draw.
+ D3D::stateman->Apply();
+ D3D::context->Draw(vertices_this_draw, 0);
+ vertices_ptr += vertex_stride * vertices_this_draw;
+ num_vertices -= vertices_this_draw;
+ }
+}
+
+void Renderer::DispatchComputeShader(const AbstractShader* shader, const void* uniforms,
+ u32 uniforms_size, u32 groups_x, u32 groups_y, u32 groups_z)
+{
+ D3D::stateman->SetComputeShader(static_cast<const DXShader*>(shader)->GetD3DComputeShader());
+
+ if (uniforms_size > 0)
+ {
+ UpdateUtilityUniformBuffer(uniforms, uniforms_size);
+ D3D::stateman->SetComputeConstants(m_utility_uniform_buffer);
+ }
+
+ D3D::stateman->Apply();
+ D3D::context->Dispatch(groups_x, groups_y, groups_z);
+}
+
TargetRectangle Renderer::ConvertEFBRectangle(const EFBRectangle& rc)
{
TargetRectangle result;
@@ -405,8 +529,8 @@ void Renderer::PokeEFB(EFBAccessType type, const EfbPokeData* points, size_t num
}
else // if (type == EFBAccessType::PokeZ)
{
- D3D::stateman->PushBlendState(m_clear_blend_states[3]);
- D3D::stateman->PushDepthState(m_clear_depth_states[1]);
+ D3D::stateman->SetBlendState(m_clear_blend_states[3]);
+ D3D::stateman->SetDepthState(m_clear_depth_states[1]);
D3D11_VIEWPORT vp =
CD3D11_VIEWPORT(0.0f, 0.0f, (float)GetTargetWidth(), (float)GetTargetHeight());
@@ -417,12 +541,6 @@ void Renderer::PokeEFB(EFBAccessType type, const EfbPokeData* points, size_t num
D3D::DrawEFBPokeQuads(type, points, num_points);
- if (type == EFBAccessType::PokeZ)
- {
- D3D::stateman->PopDepthState();
- D3D::stateman->PopBlendState();
- }
-
RestoreAPIState();
}
@@ -446,21 +564,21 @@ void Renderer::ClearScreen(const EFBRectangle& rc, bool colorEnable, bool alphaE
ResetAPIState();
if (colorEnable && alphaEnable)
- D3D::stateman->PushBlendState(m_clear_blend_states[0]);
+ D3D::stateman->SetBlendState(m_clear_blend_states[0]);
else if (colorEnable)
- D3D::stateman->PushBlendState(m_clear_blend_states[1]);
+ D3D::stateman->SetBlendState(m_clear_blend_states[1]);
else if (alphaEnable)
- D3D::stateman->PushBlendState(m_clear_blend_states[2]);
+ D3D::stateman->SetBlendState(m_clear_blend_states[2]);
else
- D3D::stateman->PushBlendState(m_clear_blend_states[3]);
+ D3D::stateman->SetBlendState(m_clear_blend_states[3]);
// TODO: Should we enable Z testing here?
// if (!bpmem.zmode.testenable) D3D::stateman->PushDepthState(s_clear_depth_states[0]);
// else
if (zEnable)
- D3D::stateman->PushDepthState(m_clear_depth_states[1]);
+ D3D::stateman->SetDepthState(m_clear_depth_states[1]);
else /*if (!zEnable)*/
- D3D::stateman->PushDepthState(m_clear_depth_states[2]);
+ D3D::stateman->SetDepthState(m_clear_depth_states[2]);
// Update the view port for clearing the picture
TargetRectangle targetRc = Renderer::ConvertEFBRectangle(rc);
@@ -474,9 +592,6 @@ void Renderer::ClearScreen(const EFBRectangle& rc, bool colorEnable, bool alphaE
u32 rgbaColor = (color & 0xFF00FF00) | ((color >> 16) & 0xFF) | ((color << 16) & 0xFF0000);
D3D::drawClearQuad(rgbaColor, 1.0f - (z & 0xFFFFFF) / 16777216.0f);
- D3D::stateman->PopDepthState();
- D3D::stateman->PopBlendState();
-
RestoreAPIState();
}
@@ -646,26 +761,23 @@ void Renderer::UpdateBackbufferSize()
// ALWAYS call RestoreAPIState for each ResetAPIState call you're doing
void Renderer::ResetAPIState()
{
- D3D::stateman->PushBlendState(m_reset_blend_state);
- D3D::stateman->PushDepthState(m_reset_depth_state);
- D3D::stateman->PushRasterizerState(m_reset_rast_state);
+ D3D::stateman->SetBlendState(m_reset_blend_state);
+ D3D::stateman->SetDepthState(m_reset_depth_state);
+ D3D::stateman->SetRasterizerState(m_reset_rast_state);
}
void Renderer::RestoreAPIState()
{
// Gets us back into a more game-like state.
- D3D::stateman->PopBlendState();
- D3D::stateman->PopDepthState();
- D3D::stateman->PopRasterizerState();
BPFunctions::SetViewport();
BPFunctions::SetScissor();
}
void Renderer::ApplyState()
{
- D3D::stateman->PushBlendState(m_state_cache.Get(m_gx_state.blend));
- D3D::stateman->PushDepthState(m_state_cache.Get(m_gx_state.zmode));
- D3D::stateman->PushRasterizerState(m_state_cache.Get(m_gx_state.raster));
+ D3D::stateman->SetBlendState(m_state_cache.Get(m_gx_state.blend));
+ D3D::stateman->SetDepthState(m_state_cache.Get(m_gx_state.zmode));
+ D3D::stateman->SetRasterizerState(m_state_cache.Get(m_gx_state.raster));
D3D::stateman->SetPrimitiveTopology(
StateCache::GetPrimitiveTopology(m_gx_state.raster.primitive));
FramebufferManager::SetIntegerEFBRenderTarget(m_gx_state.blend.logicopenable);
@@ -683,9 +795,6 @@ void Renderer::ApplyState()
void Renderer::RestoreState()
{
- D3D::stateman->PopBlendState();
- D3D::stateman->PopDepthState();
- D3D::stateman->PopRasterizerState();
}
void Renderer::SetRasterizationState(const RasterizationState& state)