2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
#include <atomic>
|
2017-07-20 05:25:29 +00:00
|
|
|
#include <memory>
|
2018-02-24 12:14:31 +00:00
|
|
|
#include <mutex>
|
2019-07-26 21:48:33 +00:00
|
|
|
#include <string_view>
|
2017-09-08 09:42:56 +00:00
|
|
|
#include <unordered_map>
|
2016-01-02 03:28:42 +00:00
|
|
|
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLUtil.h"
|
2018-02-25 07:56:09 +00:00
|
|
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
2017-07-20 05:25:29 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
namespace OGL
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2017-09-08 09:42:56 +00:00
|
|
|
class OGLShader;
|
2017-07-20 05:25:29 +00:00
|
|
|
class GLVertexFormat;
|
2017-09-08 09:42:56 +00:00
|
|
|
class StreamBuffer;
|
2017-07-20 05:25:29 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
struct SHADER
|
|
|
|
{
|
|
|
|
void Destroy()
|
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
DestroyShaders();
|
|
|
|
if (glprogid)
|
|
|
|
{
|
|
|
|
glDeleteProgram(glprogid);
|
|
|
|
glprogid = 0;
|
|
|
|
}
|
2013-02-13 12:12:19 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
GLuint vsid = 0;
|
|
|
|
GLuint gsid = 0;
|
|
|
|
GLuint psid = 0;
|
|
|
|
GLuint glprogid = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
void SetProgramVariables();
|
2016-11-27 08:14:56 +00:00
|
|
|
void SetProgramBindings(bool is_compute);
|
2017-04-23 04:44:34 +00:00
|
|
|
void Bind() const;
|
2017-07-20 05:25:29 +00:00
|
|
|
void DestroyShaders();
|
2013-02-13 12:12:19 +00:00
|
|
|
};
|
2011-12-26 07:58:52 +00:00
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
struct PipelineProgramKey
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
u64 vertex_shader_id;
|
|
|
|
u64 geometry_shader_id;
|
|
|
|
u64 pixel_shader_id;
|
2017-09-08 09:42:56 +00: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-04 04:43:19 +00:00
|
|
|
PipelineProgramKey key{};
|
2017-09-08 09:42:56 +00:00
|
|
|
SHADER shader;
|
|
|
|
std::atomic_size_t reference_count{1};
|
2019-04-15 13:38:10 +00:00
|
|
|
bool binary_retrieved = false;
|
2017-09-08 09:42:56 +00:00
|
|
|
};
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
class ProgramShaderCache
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-07-20 05:25:29 +00:00
|
|
|
static void BindVertexFormat(const GLVertexFormat* vertex_format);
|
2022-11-15 06:38:24 +00:00
|
|
|
static void ReBindVertexFormat();
|
2018-11-27 07:16:53 +00:00
|
|
|
static bool IsValidVertexFormatBound();
|
2017-07-20 05:25:29 +00:00
|
|
|
static void InvalidateVertexFormat();
|
2019-03-05 13:02:40 +00:00
|
|
|
static void InvalidateVertexFormatIfBound(GLuint vao);
|
2017-09-08 09:42:56 +00:00
|
|
|
static void InvalidateLastProgram();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2019-07-26 21:48:33 +00: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 09:42:56 +00:00
|
|
|
static StreamBuffer* GetUniformBuffer();
|
|
|
|
static u32 GetUniformBufferAlignment();
|
2013-01-14 12:58:11 +00:00
|
|
|
static void UploadConstants();
|
2018-11-27 07:16:53 +00:00
|
|
|
static void UploadConstants(const void* data, u32 data_size);
|
2011-12-26 07:58:52 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
static void Init();
|
|
|
|
static void Shutdown();
|
|
|
|
static void CreateHeader();
|
2011-12-26 07:58:52 +00:00
|
|
|
|
2019-02-15 01:59:50 +00: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 13:38:10 +00: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 09:42:56 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
private:
|
2019-07-26 22:43:41 +00:00
|
|
|
using PipelineProgramMap =
|
|
|
|
std::unordered_map<PipelineProgramKey, std::unique_ptr<PipelineProgram>,
|
|
|
|
PipelineProgramKeyHash>;
|
2017-07-20 05:25:29 +00:00
|
|
|
|
2018-01-20 14:59:10 +00:00
|
|
|
static void CreateAttributelessVAO();
|
2017-06-24 09:20:34 +00:00
|
|
|
|
2018-02-25 07:56:09 +00:00
|
|
|
static PipelineProgramMap s_pipeline_programs;
|
|
|
|
static std::mutex s_pipeline_program_lock;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-01-14 12:58:11 +00:00
|
|
|
static u32 s_ubo_buffer_size;
|
2013-10-07 15:19:47 +00:00
|
|
|
static s32 s_ubo_align;
|
2018-01-20 14:59:10 +00:00
|
|
|
|
|
|
|
static GLuint s_attributeless_VBO;
|
|
|
|
static GLuint s_attributeless_VAO;
|
|
|
|
static GLuint s_last_VAO;
|
2011-12-01 03:00:21 +00:00
|
|
|
};
|
|
|
|
|
2018-02-25 07:56:09 +00:00
|
|
|
class SharedContextAsyncShaderCompiler : public VideoCommon::AsyncShaderCompiler
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool WorkerThreadInitMainThread(void** param) override;
|
|
|
|
bool WorkerThreadInitWorkerThread(void* param) override;
|
|
|
|
void WorkerThreadExit(void* param) override;
|
|
|
|
};
|
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
} // namespace OGL
|