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