2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-11-30 20:00:21 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2011-11-30 20:00:21 -07:00
|
|
|
|
2017-09-08 03:42:56 -06:00
|
|
|
#include <atomic>
|
2017-07-19 23:25:29 -06:00
|
|
|
#include <memory>
|
2018-02-24 05:14:31 -07:00
|
|
|
#include <mutex>
|
2019-07-26 15:48:33 -06:00
|
|
|
#include <string_view>
|
2017-09-08 03:42:56 -06:00
|
|
|
#include <unordered_map>
|
2016-01-01 20:28:42 -07:00
|
|
|
|
2015-09-18 10:40:00 -06:00
|
|
|
#include "Common/GL/GLUtil.h"
|
2018-02-25 00:56:09 -07:00
|
|
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
2017-07-19 23:25:29 -06:00
|
|
|
|
2011-12-26 00:58:52 -07:00
|
|
|
namespace OGL
|
2011-12-25 22:15:54 -07:00
|
|
|
{
|
2017-09-08 03:42:56 -06:00
|
|
|
class OGLShader;
|
2017-07-19 23:25:29 -06:00
|
|
|
class GLVertexFormat;
|
2017-09-08 03:42:56 -06:00
|
|
|
class StreamBuffer;
|
2017-07-19 23:25:29 -06:00
|
|
|
|
2013-02-13 05:12:19 -07:00
|
|
|
struct SHADER
|
|
|
|
{
|
|
|
|
void Destroy()
|
|
|
|
{
|
2017-07-19 23:25:29 -06:00
|
|
|
DestroyShaders();
|
|
|
|
if (glprogid)
|
|
|
|
{
|
|
|
|
glDeleteProgram(glprogid);
|
|
|
|
glprogid = 0;
|
|
|
|
}
|
2013-02-13 05:12:19 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:29 -06:00
|
|
|
GLuint vsid = 0;
|
|
|
|
GLuint gsid = 0;
|
|
|
|
GLuint psid = 0;
|
|
|
|
GLuint glprogid = 0;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-02-13 05:12:19 -07:00
|
|
|
void SetProgramVariables();
|
2016-11-27 01:14:56 -07:00
|
|
|
void SetProgramBindings(bool is_compute);
|
2017-04-22 22:44:34 -06:00
|
|
|
void Bind() const;
|
2017-07-19 23:25:29 -06:00
|
|
|
void DestroyShaders();
|
2013-02-13 05:12:19 -07:00
|
|
|
};
|
2011-12-26 00:58:52 -07:00
|
|
|
|
2017-09-08 03:42:56 -06:00
|
|
|
struct PipelineProgramKey
|
|
|
|
{
|
2019-02-14 18:59:50 -07:00
|
|
|
u64 vertex_shader_id;
|
|
|
|
u64 geometry_shader_id;
|
|
|
|
u64 pixel_shader_id;
|
2017-09-08 03:42:56 -06:00
|
|
|
|
|
|
|
bool operator==(const PipelineProgramKey& rhs) const;
|
|
|
|
bool operator<(const PipelineProgramKey& rhs) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PipelineProgramKeyHash
|
|
|
|
{
|
|
|
|
std::size_t operator()(const PipelineProgramKey& key) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PipelineProgram
|
|
|
|
{
|
2021-09-03 22:43:19 -06:00
|
|
|
PipelineProgramKey key{};
|
2017-09-08 03:42:56 -06:00
|
|
|
SHADER shader;
|
|
|
|
std::atomic_size_t reference_count{1};
|
2019-04-15 07:38:10 -06:00
|
|
|
bool binary_retrieved = false;
|
2017-09-08 03:42:56 -06:00
|
|
|
};
|
|
|
|
|
2011-12-26 00:58:52 -07:00
|
|
|
class ProgramShaderCache
|
2011-11-30 20:00:21 -07:00
|
|
|
{
|
|
|
|
public:
|
2017-07-19 23:25:29 -06:00
|
|
|
static void BindVertexFormat(const GLVertexFormat* vertex_format);
|
2022-11-14 23:38:24 -07:00
|
|
|
static void ReBindVertexFormat();
|
2018-11-27 00:16:53 -07:00
|
|
|
static bool IsValidVertexFormatBound();
|
2017-07-19 23:25:29 -06:00
|
|
|
static void InvalidateVertexFormat();
|
2019-03-05 06:02:40 -07:00
|
|
|
static void InvalidateVertexFormatIfBound(GLuint vao);
|
2017-09-08 03:42:56 -06:00
|
|
|
static void InvalidateLastProgram();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2019-07-26 15:48:33 -06:00
|
|
|
static bool CompileComputeShader(SHADER& shader, std::string_view code);
|
|
|
|
static GLuint CompileSingleShader(GLenum type, std::string_view code);
|
|
|
|
static bool CheckShaderCompileResult(GLuint id, GLenum type, std::string_view code);
|
|
|
|
static bool CheckProgramLinkResult(GLuint id, std::string_view vcode, std::string_view pcode,
|
|
|
|
std::string_view gcode);
|
2017-09-08 03:42:56 -06:00
|
|
|
static StreamBuffer* GetUniformBuffer();
|
|
|
|
static u32 GetUniformBufferAlignment();
|
2013-01-14 05:58:11 -07:00
|
|
|
static void UploadConstants();
|
2018-11-27 00:16:53 -07:00
|
|
|
static void UploadConstants(const void* data, u32 data_size);
|
2011-12-26 00:58:52 -07:00
|
|
|
|
2014-08-15 12:09:53 -06:00
|
|
|
static void Init();
|
|
|
|
static void Shutdown();
|
|
|
|
static void CreateHeader();
|
2011-12-26 00:58:52 -07:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// This counter increments with each shader object allocated, in order to give it a unique ID.
|
|
|
|
// Since the shaders can be destroyed after a pipeline is created, we can't use the shader pointer
|
|
|
|
// as a key for GL programs. For the same reason, we can't use the GL objects either. This ID is
|
|
|
|
// guaranteed to be unique for the emulation session, even if the memory allocator or GL driver
|
|
|
|
// re-uses pointers, therefore we won't have any collisions where the shaders attached to a
|
|
|
|
// pipeline do not match the pipeline configuration.
|
|
|
|
static u64 GenerateShaderID();
|
|
|
|
|
2019-04-15 07:38:10 -06:00
|
|
|
static PipelineProgram* GetPipelineProgram(const GLVertexFormat* vertex_format,
|
|
|
|
const OGLShader* vertex_shader,
|
|
|
|
const OGLShader* geometry_shader,
|
|
|
|
const OGLShader* pixel_shader, const void* cache_data,
|
|
|
|
size_t cache_data_size);
|
|
|
|
static void ReleasePipelineProgram(PipelineProgram* prog);
|
2017-09-08 03:42:56 -06:00
|
|
|
|
2011-12-26 00:58:52 -07:00
|
|
|
private:
|
2019-07-26 16:43:41 -06:00
|
|
|
using PipelineProgramMap =
|
|
|
|
std::unordered_map<PipelineProgramKey, std::unique_ptr<PipelineProgram>,
|
|
|
|
PipelineProgramKeyHash>;
|
2017-07-19 23:25:29 -06:00
|
|
|
|
2018-01-20 07:59:10 -07:00
|
|
|
static void CreateAttributelessVAO();
|
2017-06-24 03:20:34 -06:00
|
|
|
|
2018-02-25 00:56:09 -07:00
|
|
|
static PipelineProgramMap s_pipeline_programs;
|
|
|
|
static std::mutex s_pipeline_program_lock;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-01-14 05:58:11 -07:00
|
|
|
static u32 s_ubo_buffer_size;
|
2013-10-07 09:19:47 -06:00
|
|
|
static s32 s_ubo_align;
|
2018-01-20 07:59:10 -07:00
|
|
|
|
|
|
|
static GLuint s_attributeless_VBO;
|
|
|
|
static GLuint s_attributeless_VAO;
|
|
|
|
static GLuint s_last_VAO;
|
2011-11-30 20:00:21 -07:00
|
|
|
};
|
|
|
|
|
2018-02-25 00:56:09 -07:00
|
|
|
class SharedContextAsyncShaderCompiler : public VideoCommon::AsyncShaderCompiler
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool WorkerThreadInitMainThread(void** param) override;
|
|
|
|
bool WorkerThreadInitWorkerThread(void* param) override;
|
|
|
|
void WorkerThreadExit(void* param) override;
|
|
|
|
};
|
|
|
|
|
2011-11-30 20:00:21 -07:00
|
|
|
} // namespace OGL
|