2013-12-19 17:10:14 +00:00
|
|
|
#pragma once
|
2020-03-28 16:58:01 +00:00
|
|
|
#include "hw/pvr/ta_structs.h"
|
|
|
|
#include "hw/pvr/ta_ctx.h"
|
2019-10-04 10:22:18 +00:00
|
|
|
#include "rend/TexCache.h"
|
2019-10-18 19:57:08 +00:00
|
|
|
#include "wsi/gl_context.h"
|
2020-12-27 08:10:19 +00:00
|
|
|
#include "glcache.h"
|
2020-03-28 16:58:01 +00:00
|
|
|
|
|
|
|
#include <unordered_map>
|
2019-08-12 17:56:44 +00:00
|
|
|
#include <glm/glm.hpp>
|
2014-01-21 22:12:04 +00:00
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
#define glCheck() do { if (unlikely(config::OpenGlChecks)) { verify(glGetError()==GL_NO_ERROR); } } while(0)
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
#define VERTEX_POS_ARRAY 0
|
|
|
|
#define VERTEX_COL_BASE_ARRAY 1
|
|
|
|
#define VERTEX_COL_OFFS_ARRAY 2
|
|
|
|
#define VERTEX_UV_ARRAY 3
|
2018-10-04 08:29:23 +00:00
|
|
|
// OIT only
|
|
|
|
#define VERTEX_COL_BASE1_ARRAY 4
|
|
|
|
#define VERTEX_COL_OFFS1_ARRAY 5
|
|
|
|
#define VERTEX_UV1_ARRAY 6
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
//vertex types
|
|
|
|
extern u32 gcflip;
|
|
|
|
|
2019-08-12 17:56:44 +00:00
|
|
|
extern glm::mat4 ViewportMatrix;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
void DrawStrips();
|
|
|
|
|
|
|
|
struct PipelineShader
|
|
|
|
{
|
|
|
|
GLuint program;
|
|
|
|
|
2020-05-11 13:50:16 +00:00
|
|
|
GLint depth_scale;
|
|
|
|
GLint pp_ClipTest;
|
|
|
|
GLint cp_AlphaTestValue;
|
|
|
|
GLint sp_FOG_COL_RAM;
|
|
|
|
GLint sp_FOG_COL_VERT;
|
|
|
|
GLint sp_FOG_DENSITY;
|
|
|
|
GLint trilinear_alpha;
|
|
|
|
GLint fog_clamp_min, fog_clamp_max;
|
|
|
|
GLint normal_matrix;
|
2020-07-08 16:17:15 +00:00
|
|
|
GLint palette_index;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
//
|
2020-05-11 13:50:16 +00:00
|
|
|
bool cp_AlphaTest;
|
|
|
|
bool pp_InsideClipping;
|
|
|
|
bool pp_Texture;
|
|
|
|
bool pp_UseAlpha;
|
|
|
|
bool pp_IgnoreTexA;
|
|
|
|
u32 pp_ShadInstr;
|
|
|
|
bool pp_Offset;
|
|
|
|
u32 pp_FogCtrl;
|
|
|
|
bool pp_Gouraud;
|
|
|
|
bool pp_BumpMap;
|
2018-09-01 10:56:37 +00:00
|
|
|
bool fog_clamping;
|
2018-11-23 21:33:51 +00:00
|
|
|
bool trilinear;
|
2020-07-08 16:17:15 +00:00
|
|
|
bool palette;
|
2013-12-19 17:10:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct gl_ctx
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
GLuint program;
|
|
|
|
|
2020-05-11 13:50:16 +00:00
|
|
|
GLint depth_scale;
|
|
|
|
GLint sp_ShaderColor;
|
|
|
|
GLint normal_matrix;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
} modvol_shader;
|
|
|
|
|
2019-04-04 20:26:21 +00:00
|
|
|
std::unordered_map<u32, PipelineShader> shaders;
|
2019-04-04 17:08:21 +00:00
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
struct
|
|
|
|
{
|
2019-03-02 13:20:05 +00:00
|
|
|
GLuint program;
|
2020-05-11 13:50:16 +00:00
|
|
|
GLint scale;
|
2019-10-21 14:39:16 +00:00
|
|
|
GLuint vao;
|
|
|
|
GLuint geometry;
|
|
|
|
GLuint osd_tex;
|
2013-12-19 17:10:14 +00:00
|
|
|
} OSD_SHADER;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
GLuint geometry,modvols,idxs,idxs2;
|
2014-06-26 21:51:57 +00:00
|
|
|
GLuint vao;
|
2013-12-19 17:10:14 +00:00
|
|
|
} vbo;
|
|
|
|
|
2019-02-18 23:49:24 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
u32 TexAddr;
|
|
|
|
GLuint depthb;
|
|
|
|
GLuint tex;
|
|
|
|
GLuint fbo;
|
|
|
|
} rtt;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
GLuint depthb;
|
2019-04-07 22:21:06 +00:00
|
|
|
GLuint colorb;
|
2019-02-18 23:49:24 +00:00
|
|
|
GLuint tex;
|
|
|
|
GLuint fbo;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
} ofbo;
|
|
|
|
|
2018-08-07 07:44:52 +00:00
|
|
|
const char *gl_version;
|
|
|
|
const char *glsl_version_header;
|
|
|
|
int gl_major;
|
2020-02-07 15:55:32 +00:00
|
|
|
int gl_minor;
|
2018-08-07 07:44:52 +00:00
|
|
|
bool is_gles;
|
2020-07-08 16:17:15 +00:00
|
|
|
GLuint single_channel_format;
|
2018-12-13 21:26:25 +00:00
|
|
|
GLenum index_type;
|
2019-02-18 16:42:07 +00:00
|
|
|
bool GL_OES_packed_depth_stencil_supported;
|
|
|
|
bool GL_OES_depth24_supported;
|
2019-11-14 21:10:40 +00:00
|
|
|
bool highp_float_supported;
|
2018-12-13 21:26:25 +00:00
|
|
|
|
|
|
|
size_t get_index_size() { return index_type == GL_UNSIGNED_INT ? sizeof(u32) : sizeof(u16); }
|
2013-12-19 17:10:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern gl_ctx gl;
|
2018-08-26 14:58:10 +00:00
|
|
|
extern GLuint fbTextureId;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-10-04 11:33:08 +00:00
|
|
|
u64 gl_GetTexture(TSP tsp,TCW tcw);
|
2015-08-05 15:02:34 +00:00
|
|
|
struct text_info {
|
|
|
|
u16* pdata;
|
|
|
|
u32 width;
|
|
|
|
u32 height;
|
|
|
|
u32 textype; // 0 565, 1 1555, 2 4444
|
|
|
|
};
|
2018-10-04 12:04:15 +00:00
|
|
|
enum ModifierVolumeMode { Xor, Or, Inclusion, Exclusion, ModeCount };
|
2015-08-05 15:02:34 +00:00
|
|
|
|
2018-10-04 12:49:20 +00:00
|
|
|
void gl_load_osd_resources();
|
2019-02-17 23:25:06 +00:00
|
|
|
void gl_free_osd_resources();
|
2018-10-04 10:32:26 +00:00
|
|
|
bool ProcessFrame(TA_context* ctx);
|
2018-10-04 12:49:20 +00:00
|
|
|
void UpdateFogTexture(u8 *fog_table, GLenum texture_slot, GLint fog_image_format);
|
2020-07-08 16:17:15 +00:00
|
|
|
void UpdatePaletteTexture(GLenum texture_slot);
|
2019-02-20 18:22:24 +00:00
|
|
|
void findGLVersion();
|
2019-08-12 17:56:44 +00:00
|
|
|
void GetFramebufferScaling(float& scale_x, float& scale_y, float& scissoring_scale_x, float& scissoring_scale_y);
|
|
|
|
void GetFramebufferSize(float& dc_width, float& dc_height);
|
|
|
|
void SetupMatrices(float dc_width, float dc_height,
|
|
|
|
float scale_x, float scale_y, float scissoring_scale_x, float scissoring_scale_y,
|
|
|
|
float &ds2s_offs_x, glm::mat4& normal_mat, glm::mat4& scissor_mat);
|
2018-10-04 08:29:23 +00:00
|
|
|
|
2015-08-05 15:02:34 +00:00
|
|
|
text_info raw_GetTexture(TSP tsp, TCW tcw);
|
2018-10-04 08:29:23 +00:00
|
|
|
void SetCull(u32 CullMode);
|
2018-10-04 10:32:26 +00:00
|
|
|
s32 SetTileClip(u32 val, GLint uniform);
|
2018-10-04 12:04:15 +00:00
|
|
|
void SetMVS_Mode(ModifierVolumeMode mv_mode, ISP_Modvol ispc);
|
2014-05-08 01:29:14 +00:00
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
void BindRTT(u32 addy, u32 fbw, u32 fbh, u32 channels, u32 fmt);
|
2018-05-08 16:47:00 +00:00
|
|
|
void ReadRTTBuffer();
|
2018-08-26 14:58:10 +00:00
|
|
|
void RenderFramebuffer();
|
2019-10-10 17:43:34 +00:00
|
|
|
void DrawFramebuffer();
|
2019-02-19 10:36:59 +00:00
|
|
|
GLuint init_output_framebuffer(int width, int height);
|
|
|
|
bool render_output_framebuffer();
|
|
|
|
void free_output_framebuffer();
|
2018-08-26 14:58:10 +00:00
|
|
|
|
2019-03-05 02:15:11 +00:00
|
|
|
void OSD_DRAW(bool clear_screen);
|
2020-05-11 13:50:16 +00:00
|
|
|
PipelineShader *GetProgram(bool cp_AlphaTest, bool pp_InsideClipping,
|
|
|
|
bool pp_Texture, bool pp_UseAlpha, bool pp_IgnoreTexA, u32 pp_ShadInstr, bool pp_Offset,
|
2020-07-08 16:17:15 +00:00
|
|
|
u32 pp_FogCtrl, bool pp_Gouraud, bool pp_BumpMap, bool fog_clamping, bool trilinear,
|
|
|
|
bool palette);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-10-04 08:29:23 +00:00
|
|
|
GLuint gl_CompileShader(const char* shader, GLuint type);
|
|
|
|
GLuint gl_CompileAndLink(const char* VertexShader, const char* FragmentShader);
|
2013-12-24 00:56:44 +00:00
|
|
|
bool CompilePipelineShader(PipelineShader* s);
|
2018-06-26 12:24:45 +00:00
|
|
|
|
2018-07-03 18:36:13 +00:00
|
|
|
extern struct ShaderUniforms_t
|
|
|
|
{
|
|
|
|
float PT_ALPHA;
|
|
|
|
float depth_coefs[4];
|
|
|
|
float fog_den_float;
|
|
|
|
float ps_FOG_COL_RAM[3];
|
|
|
|
float ps_FOG_COL_VERT[3];
|
|
|
|
float trilinear_alpha;
|
2018-09-01 10:56:37 +00:00
|
|
|
float fog_clamp_min[4];
|
|
|
|
float fog_clamp_max[4];
|
2019-08-12 17:56:44 +00:00
|
|
|
glm::mat4 normal_mat;
|
2020-05-11 13:50:16 +00:00
|
|
|
struct {
|
|
|
|
bool enabled;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
} base_clipping;
|
2020-07-31 10:16:10 +00:00
|
|
|
int palette_index;
|
2018-07-03 18:36:13 +00:00
|
|
|
|
2019-11-14 21:10:40 +00:00
|
|
|
void Set(const PipelineShader* s)
|
2018-07-03 18:36:13 +00:00
|
|
|
{
|
|
|
|
if (s->cp_AlphaTestValue!=-1)
|
|
|
|
glUniform1f(s->cp_AlphaTestValue,PT_ALPHA);
|
|
|
|
|
|
|
|
if (s->depth_scale!=-1)
|
|
|
|
glUniform4fv( s->depth_scale, 1, depth_coefs);
|
|
|
|
|
|
|
|
if (s->sp_FOG_DENSITY!=-1)
|
|
|
|
glUniform1f( s->sp_FOG_DENSITY,fog_den_float);
|
|
|
|
|
|
|
|
if (s->sp_FOG_COL_RAM!=-1)
|
|
|
|
glUniform3fv( s->sp_FOG_COL_RAM, 1, ps_FOG_COL_RAM);
|
|
|
|
|
|
|
|
if (s->sp_FOG_COL_VERT!=-1)
|
|
|
|
glUniform3fv( s->sp_FOG_COL_VERT, 1, ps_FOG_COL_VERT);
|
|
|
|
|
2018-09-01 10:56:37 +00:00
|
|
|
if (s->fog_clamp_min != -1)
|
|
|
|
glUniform4fv(s->fog_clamp_min, 1, fog_clamp_min);
|
|
|
|
if (s->fog_clamp_max != -1)
|
|
|
|
glUniform4fv(s->fog_clamp_max, 1, fog_clamp_max);
|
2019-08-12 17:56:44 +00:00
|
|
|
|
|
|
|
if (s->normal_matrix != -1)
|
|
|
|
glUniformMatrix4fv(s->normal_matrix, 1, GL_FALSE, &normal_mat[0][0]);
|
2020-07-08 16:17:15 +00:00
|
|
|
|
|
|
|
if (s->palette_index != -1)
|
2020-07-31 10:16:10 +00:00
|
|
|
glUniform1i(s->palette_index, palette_index);
|
2018-07-03 18:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} ShaderUniforms;
|
|
|
|
|
2020-11-24 17:01:09 +00:00
|
|
|
class TextureCacheData final : public BaseTextureCacheData
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
2020-03-29 18:58:49 +00:00
|
|
|
public:
|
2018-12-30 17:42:55 +00:00
|
|
|
GLuint texID; //gl texture
|
2019-10-04 10:22:18 +00:00
|
|
|
virtual std::string GetId() override { return std::to_string(texID); }
|
2020-03-09 21:49:05 +00:00
|
|
|
virtual void UploadToGPU(int width, int height, u8 *temp_tex_buffer, bool mipmapped, bool mipmapsIncluded = false) override;
|
2019-10-04 10:22:18 +00:00
|
|
|
virtual bool Delete() override;
|
2018-12-30 17:42:55 +00:00
|
|
|
};
|
2019-09-17 11:35:23 +00:00
|
|
|
|
2021-01-15 09:14:24 +00:00
|
|
|
class GlTextureCache final : public BaseTextureCache<TextureCacheData>
|
2019-10-21 14:39:16 +00:00
|
|
|
{
|
2020-12-27 07:58:48 +00:00
|
|
|
public:
|
|
|
|
void Cleanup()
|
|
|
|
{
|
|
|
|
if (!texturesToDelete.empty())
|
|
|
|
{
|
|
|
|
glcache.DeleteTextures((GLsizei)texturesToDelete.size(), &texturesToDelete[0]);
|
|
|
|
texturesToDelete.clear();
|
|
|
|
}
|
|
|
|
CollectCleanup();
|
|
|
|
}
|
|
|
|
void DeleteLater(GLuint texId) { texturesToDelete.push_back(texId); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<GLuint> texturesToDelete;
|
2019-10-21 14:39:16 +00:00
|
|
|
};
|
2021-01-15 09:14:24 +00:00
|
|
|
extern GlTextureCache TexCache;
|
2019-10-21 14:39:16 +00:00
|
|
|
|
2019-09-17 11:35:23 +00:00
|
|
|
extern const u32 Zfunction[8];
|
2020-01-05 19:44:39 +00:00
|
|
|
extern const u32 SrcBlendGL[], DstBlendGL[];
|
2020-12-16 16:25:33 +00:00
|
|
|
|
|
|
|
struct OpenGLRenderer : Renderer
|
|
|
|
{
|
|
|
|
bool Init() override;
|
|
|
|
void Resize(int w, int h) override { screen_width = w; screen_height = h; }
|
|
|
|
void Term() override;
|
|
|
|
|
|
|
|
bool Process(TA_context* ctx) override { return ProcessFrame(ctx); }
|
|
|
|
|
|
|
|
bool Render() override;
|
|
|
|
|
|
|
|
bool RenderLastFrame() override;
|
|
|
|
|
|
|
|
void DrawOSD(bool clear_screen) override { OSD_DRAW(clear_screen); }
|
|
|
|
|
|
|
|
virtual u64 GetTexture(TSP tsp, TCW tcw) override
|
|
|
|
{
|
|
|
|
return gl_GetTexture(tsp, tcw);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Present() override
|
|
|
|
{
|
|
|
|
if (!frameRendered)
|
|
|
|
return false;
|
|
|
|
frameRendered = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool frameRendered = false;
|
|
|
|
};
|