Debian Codegraph @

Source/Core/VideoBackends/OGL/OGLPipeline.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/OGL/OGLPipeline.h"
5
6 #include <utility>
7
8 #include "VideoBackends/OGL/OGLShader.h"
9 #include "VideoBackends/OGL/OGLVertexManager.h"
10 #include "VideoBackends/OGL/ProgramShaderCache.h"
11 #include "VideoCommon/VideoConfig.h"
12
13 namespace OGL
14 {
15 static GLenum MapToGLPrimitive(PrimitiveType primitive_type)
16 {
17 switch (primitive_type)
18 {
19 case PrimitiveType::Points:
20 return GL_POINTS;
21 case PrimitiveType::Lines:
22 return GL_LINES;
23 case PrimitiveType::Triangles:
24 return GL_TRIANGLES;
25 case PrimitiveType::TriangleStrip:
26 return GL_TRIANGLE_STRIP;
27 default:
28 return 0;
29 }
30 }
31 OGLPipeline::OGLPipeline(const AbstractPipelineConfig& config, const GLVertexFormat* vertex_format,
32 RasterizationState rasterization_state, DepthState depth_state,
33 BlendingState blending_state, PipelineProgram* program,
34 GLuint gl_primitive)
35 : AbstractPipeline(config), m_vertex_format(vertex_format),
36 m_rasterization_state(std::move(rasterization_state)), m_depth_state(std::move(depth_state)),
37 m_blending_state(std::move(blending_state)), m_program(program), m_gl_primitive(gl_primitive)
38 {
39 }
40
41 OGLPipeline::~OGLPipeline()
42 {
43 // We don't want to destroy the shaders.
44 ProgramShaderCache::ReleasePipelineProgram(m_program);
45 }
46
47 AbstractPipeline::CacheData OGLPipeline::GetCacheData() const
48 {
49 // More than one pipeline can share the same shaders. To avoid bloating the cache with multiple
50 // copies of the same program combination, we set a flag on the program object so that it can't
51 // be retrieved again. When booting, the pipeline cache is loaded in-order, so the additional
52 // pipelines which use the program combination will re-use the already-created object.
53 if (!g_backend_info.bSupportsPipelineCacheData || m_program->binary_retrieved)
54 return {};
55
56 GLint program_size = 0;
57 glGetProgramiv(m_program->shader.glprogid, GL_PROGRAM_BINARY_LENGTH, &program_size);
58 if (program_size == 0)
59 return {};
60
61 // Clear any existing error.
62 glGetError();
63
64 // We pack the format at the start of the buffer.
65 CacheData data(program_size + sizeof(u32));
66 GLsizei data_size = 0;
67 GLenum program_format = 0;
68 glGetProgramBinary(m_program->shader.glprogid, program_size, &data_size, &program_format,
69 &data[sizeof(u32)]);
70 if (glGetError() != GL_NO_ERROR || data_size == 0)
71 return {};
72
73 u32 program_format_u32 = static_cast<u32>(program_format);
74 std::memcpy(&data[0], &program_format_u32, sizeof(u32));
75 data.resize(data_size + sizeof(u32));
76 m_program->binary_retrieved = true;
77 return data;
78 }
79
80 std::unique_ptr<OGLPipeline> OGLPipeline::Create(const AbstractPipelineConfig& config,
81 const void* cache_data, size_t cache_data_size)
82 {
83 PipelineProgram* program = ProgramShaderCache::GetPipelineProgram(
84 static_cast<const GLVertexFormat*>(config.vertex_format),
85 static_cast<const OGLShader*>(config.vertex_shader),
86 static_cast<const OGLShader*>(config.geometry_shader),
87 static_cast<const OGLShader*>(config.pixel_shader), cache_data, cache_data_size);
88 if (!program)
89 return nullptr;
90
91 const GLVertexFormat* vertex_format = static_cast<const GLVertexFormat*>(config.vertex_format);
92 GLenum gl_primitive = MapToGLPrimitive(config.rasterization_state.primitive);
93 return std::make_unique<OGLPipeline>(config, vertex_format, config.rasterization_state,
94 config.depth_state, config.blending_state, program,
95 gl_primitive);
96 }
97 } // namespace OGL