Skip to content

Commit

Permalink
GLES: Add OpenGL 3.x core profile support.
Browse files Browse the repository at this point in the history
Need to define #version everywhere and avoid varying/etc.
  • Loading branch information
unknownbrackets committed Sep 20, 2017
1 parent 799c85e commit e754cca
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
15 changes: 14 additions & 1 deletion GPU/GLES/FramebufferManagerGLES.cpp
Expand Up @@ -49,6 +49,12 @@
// #define DEBUG_READ_PIXELS 1 // #define DEBUG_READ_PIXELS 1


static const char tex_fs[] = static const char tex_fs[] =
"#if __VERSION__ >= 130\n"
"#define varying in\n"
"#define texture2D texture\n"
"#define gl_FragColor fragColor0\n"
"out vec4 fragColor0;\n"
"#endif\n"
#ifdef USING_GLES2 #ifdef USING_GLES2
"precision mediump float;\n" "precision mediump float;\n"
#endif #endif
Expand All @@ -59,6 +65,10 @@ static const char tex_fs[] =
"}\n"; "}\n";


static const char basic_vs[] = static const char basic_vs[] =
"#if __VERSION__ >= 130\n"
"#define attribute in\n"
"#define varying out\n"
"#endif\n"
"attribute vec4 a_position;\n" "attribute vec4 a_position;\n"
"attribute vec2 a_texcoord0;\n" "attribute vec2 a_texcoord0;\n"
"varying vec2 v_texcoord0;\n" "varying vec2 v_texcoord0;\n"
Expand Down Expand Up @@ -87,7 +97,10 @@ void FramebufferManagerGLES::DisableState() {
void FramebufferManagerGLES::CompileDraw2DProgram() { void FramebufferManagerGLES::CompileDraw2DProgram() {
if (!draw2dprogram_) { if (!draw2dprogram_) {
std::string errorString; std::string errorString;
draw2dprogram_ = glsl_create_source(basic_vs, tex_fs, &errorString); static std::string vs_code, fs_code;
vs_code = ApplyGLSLPrelude(basic_vs, GL_VERTEX_SHADER);
fs_code = ApplyGLSLPrelude(tex_fs, GL_FRAGMENT_SHADER);
draw2dprogram_ = glsl_create_source(vs_code.c_str(), fs_code.c_str(), &errorString);
if (!draw2dprogram_) { if (!draw2dprogram_) {
ERROR_LOG_REPORT(G3D, "Failed to compile draw2dprogram! This shouldn't happen.\n%s", errorString.c_str()); ERROR_LOG_REPORT(G3D, "Failed to compile draw2dprogram! This shouldn't happen.\n%s", errorString.c_str());
} else { } else {
Expand Down
33 changes: 20 additions & 13 deletions GPU/GLES/StencilBufferGLES.cpp
Expand Up @@ -22,11 +22,16 @@
#include "GPU/GLES/ShaderManagerGLES.h" #include "GPU/GLES/ShaderManagerGLES.h"
#include "GPU/GLES/TextureCacheGLES.h" #include "GPU/GLES/TextureCacheGLES.h"


static const char *gles_prefix =
"#version 100\n"
"precision highp float;\n";

static const char *stencil_fs = static const char *stencil_fs =
"#ifdef GL_ES\n"
"precision highp float;\n"
"#endif\n"
"#if __VERSION__ >= 130\n"
"#define varying in\n"
"#define texture2D texture\n"
"#define gl_FragColor fragColor0\n"
"out vec4 fragColor0;\n"
"#endif\n"
"varying vec2 v_texcoord0;\n" "varying vec2 v_texcoord0;\n"
"uniform float u_stencilValue;\n" "uniform float u_stencilValue;\n"
"uniform sampler2D tex;\n" "uniform sampler2D tex;\n"
Expand All @@ -39,6 +44,13 @@ static const char *stencil_fs =
"}\n"; "}\n";


static const char *stencil_vs = static const char *stencil_vs =
"#ifdef GL_ES\n"
"precision highp float;\n"
"#endif\n"
"#if __VERSION__ >= 130\n"
"#define attribute in\n"
"#define varying out\n"
"#endif\n"
"attribute vec4 a_position;\n" "attribute vec4 a_position;\n"
"attribute vec2 a_texcoord0;\n" "attribute vec2 a_texcoord0;\n"
"varying vec2 v_texcoord0;\n" "varying vec2 v_texcoord0;\n"
Expand All @@ -47,14 +59,6 @@ static const char *stencil_vs =
" gl_Position = a_position;\n" " gl_Position = a_position;\n"
"}\n"; "}\n";


std::string GLSLES100PrefixProgram(std::string code) {
if (gl_extensions.IsGLES) {
return std::string(gles_prefix) + code;
} else {
return code;
}
}

static u8 StencilBits5551(const u8 *ptr8, u32 numPixels) { static u8 StencilBits5551(const u8 *ptr8, u32 numPixels) {
const u32 *ptr = (const u32 *)ptr8; const u32 *ptr = (const u32 *)ptr8;


Expand Down Expand Up @@ -152,7 +156,10 @@ bool FramebufferManagerGLES::NotifyStencilUpload(u32 addr, int size, bool skipZe


if (!stencilUploadProgram_) { if (!stencilUploadProgram_) {
std::string errorString; std::string errorString;
stencilUploadProgram_ = glsl_create_source(GLSLES100PrefixProgram(stencil_vs).c_str(), GLSLES100PrefixProgram(stencil_fs).c_str(), &errorString); static std::string vs_code, fs_code;
vs_code = ApplyGLSLPrelude(stencil_vs, GL_VERTEX_SHADER);
fs_code = ApplyGLSLPrelude(stencil_fs, GL_FRAGMENT_SHADER);
stencilUploadProgram_ = glsl_create_source(vs_code.c_str(), fs_code.c_str(), &errorString);
if (!stencilUploadProgram_) { if (!stencilUploadProgram_) {
ERROR_LOG_REPORT(G3D, "Failed to compile stencilUploadProgram! This shouldn't happen.\n%s", errorString.c_str()); ERROR_LOG_REPORT(G3D, "Failed to compile stencilUploadProgram! This shouldn't happen.\n%s", errorString.c_str());
} else { } else {
Expand Down
28 changes: 28 additions & 0 deletions ext/native/gfx_es2/gpu_features.cpp
Expand Up @@ -444,3 +444,31 @@ void SetGLCoreContext(bool flag) {
// For convenience, it'll get reset later. // For convenience, it'll get reset later.
gl_extensions.IsCoreContext = useCoreContext; gl_extensions.IsCoreContext = useCoreContext;
} }

static const char *glsl_fragment_prelude =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n";

std::string ApplyGLSLPrelude(const std::string &source, uint32_t stage) {
std::string temp;
std::string version = "";
if (!gl_extensions.IsGLES && gl_extensions.IsCoreContext) {
// We need to add a corresponding #version. Apple drives fail without an exact match.
if (gl_extensions.VersionGEThan(3, 3)) {
version = StringFromFormat("#version %d%d0\n", gl_extensions.ver[0], gl_extensions.ver[1]);
} else if (gl_extensions.VersionGEThan(3, 2)) {
version = "#version 150\n";
} else if (gl_extensions.VersionGEThan(3, 1)) {
version = "#version 140\n";
} else {
version = "#version 130\n";
}
}
if (stage == GL_FRAGMENT_SHADER) {
temp = version + glsl_fragment_prelude + source;
} else if (stage == GL_VERTEX_SHADER) {
temp = version + source;
}
return temp;
}
3 changes: 3 additions & 0 deletions ext/native/gfx_es2/gpu_features.h
Expand Up @@ -4,6 +4,7 @@


#pragma once #pragma once


#include <string>
#include "base/NativeApp.h" #include "base/NativeApp.h"


enum { enum {
Expand Down Expand Up @@ -110,3 +111,5 @@ extern std::string g_all_egl_extensions;


void CheckGLExtensions(); void CheckGLExtensions();
void SetGLCoreContext(bool flag); void SetGLCoreContext(bool flag);

std::string ApplyGLSLPrelude(const std::string &source, uint32_t stage);
21 changes: 20 additions & 1 deletion ext/native/thin3d/thin3d.cpp
Expand Up @@ -69,6 +69,12 @@ static const std::vector<ShaderSource> fsTexCol = {
"#ifdef GL_ES\n" "#ifdef GL_ES\n"
"precision lowp float;\n" "precision lowp float;\n"
"#endif\n" "#endif\n"
"#if __VERSION__ >= 130\n"
"#define varying in\n"
"#define texture2D texture\n"
"#define gl_FragColor fragColor0\n"
"out vec4 fragColor0;\n"
"#endif\n"
"varying vec4 oColor0;\n" "varying vec4 oColor0;\n"
"varying vec2 oTexCoord0;\n" "varying vec2 oTexCoord0;\n"
"uniform sampler2D Sampler0;\n" "uniform sampler2D Sampler0;\n"
Expand Down Expand Up @@ -107,6 +113,11 @@ static const std::vector<ShaderSource> fsCol = {
"#ifdef GL_ES\n" "#ifdef GL_ES\n"
"precision lowp float;\n" "precision lowp float;\n"
"#endif\n" "#endif\n"
"#if __VERSION__ >= 130\n"
"#define varying in\n"
"#define gl_FragColor fragColor0\n"
"out vec4 fragColor0;\n"
"#endif\n"
"varying vec4 oColor0;\n" "varying vec4 oColor0;\n"
"void main() { gl_FragColor = oColor0; }\n" "void main() { gl_FragColor = oColor0; }\n"
}, },
Expand Down Expand Up @@ -136,6 +147,10 @@ static const std::vector<ShaderSource> fsCol = {


static const std::vector<ShaderSource> vsCol = { static const std::vector<ShaderSource> vsCol = {
{ ShaderLanguage::GLSL_ES_200, { ShaderLanguage::GLSL_ES_200,
"#if __VERSION__ >= 130\n"
"#define attribute in\n"
"#define varying out\n"
"#endif\n"
"attribute vec3 Position;\n" "attribute vec3 Position;\n"
"attribute vec4 Color0;\n" "attribute vec4 Color0;\n"
"varying vec4 oColor0;\n" "varying vec4 oColor0;\n"
Expand Down Expand Up @@ -193,6 +208,10 @@ const UniformBufferDesc vsColBufDesc { sizeof(VsColUB), {


static const std::vector<ShaderSource> vsTexCol = { static const std::vector<ShaderSource> vsTexCol = {
{ ShaderLanguage::GLSL_ES_200, { ShaderLanguage::GLSL_ES_200,
"#if __VERSION__ >= 130\n"
"#define attribute in\n"
"#define varying out\n"
"#endif\n"
"attribute vec3 Position;\n" "attribute vec3 Position;\n"
"attribute vec4 Color0;\n" "attribute vec4 Color0;\n"
"attribute vec2 TexCoord0;\n" "attribute vec2 TexCoord0;\n"
Expand Down Expand Up @@ -285,4 +304,4 @@ DrawContext::~DrawContext() {
} }
} }


} // namespace Draw } // namespace Draw
15 changes: 7 additions & 8 deletions ext/native/thin3d/thin3d_gl.cpp
Expand Up @@ -163,11 +163,6 @@ static const unsigned short primToGL[] = {


class OpenGLBuffer; class OpenGLBuffer;


static const char *glsl_fragment_prelude =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n";

class OpenGLBlendState : public BlendState { class OpenGLBlendState : public BlendState {
public: public:
bool enabled; bool enabled;
Expand Down Expand Up @@ -332,9 +327,9 @@ bool OpenGLShaderModule::Compile(ShaderLanguage language, const uint8_t *data, s
language_ = language; language_ = language;


std::string temp; std::string temp;
// Add the prelude on automatically for fragment shaders. // Add the prelude on automatically.
if (glstage_ == GL_FRAGMENT_SHADER) { if (glstage_ == GL_FRAGMENT_SHADER || glstage_ == GL_VERTEX_SHADER) {
temp = std::string(glsl_fragment_prelude) + source_; temp = ApplyGLSLPrelude(source_, glstage_);
source_ = temp.c_str(); source_ = temp.c_str();
} }


Expand Down Expand Up @@ -1047,6 +1042,10 @@ bool OpenGLPipeline::LinkShaders() {
glBindAttribLocation(program_, SEM_NORMAL, "Normal"); glBindAttribLocation(program_, SEM_NORMAL, "Normal");
glBindAttribLocation(program_, SEM_TANGENT, "Tangent"); glBindAttribLocation(program_, SEM_TANGENT, "Tangent");
glBindAttribLocation(program_, SEM_BINORMAL, "Binormal"); glBindAttribLocation(program_, SEM_BINORMAL, "Binormal");

if (gl_extensions.VersionGEThan(3, 3, 0)) {
glBindFragDataLocation(program_, 0, "fragColor0");
}
glLinkProgram(program_); glLinkProgram(program_);


GLint linkStatus = GL_FALSE; GLint linkStatus = GL_FALSE;
Expand Down

0 comments on commit e754cca

Please sign in to comment.