GRAPHICS: Moved shader code where it's actually used

This commit is contained in:
Paweł Kołodziejski 2022-01-10 03:34:48 +01:00
parent 4e865e945e
commit 4f0e5ed3c0
9 changed files with 134 additions and 210 deletions

View file

@ -75,12 +75,43 @@ static const GLfloat vertices[] = {
1.0, 1.0,
};
static const char *controlVertex =
"#version 100\n"
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"uniform vec2 offsetXY;\n"
"uniform vec2 sizeWH;\n"
"uniform vec4 clip;\n"
"uniform bool flipY;\n"
"varying vec2 Texcoord;\n"
"void main() {\n"
"Texcoord = clip.xy + texcoord * (clip.zw - clip.xy);\n"
"vec2 pos = offsetXY + position * sizeWH;\n"
"pos.x = pos.x * 2.0 - 1.0;\n"
"pos.y = pos.y * 2.0 - 1.0;\n"
"if (flipY)\n"
"pos.y *= -1.0;\n"
"gl_Position = vec4(pos, 0.0, 1.0);\n"
"}\n";
static const char *controlFragment =
"#version 100\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main() {\n"
"gl_FragColor = texture2D(tex, Texcoord);\n"
"}\n";
void GLESBaseTexture::initGL() {
_npot_supported = OpenGLContext.NPOTSupported;
const char *attributes[] = { "position", "texcoord", NULL };
_box_shader = OpenGL::ShaderGL::fromStrings("control", OpenGL::BuiltinShaders::controlVertex,
OpenGL::BuiltinShaders::controlFragment, attributes);
_box_shader = OpenGL::ShaderGL::fromStrings("control", controlVertex, controlFragment, attributes);
_verticesVBO = OpenGL::ShaderGL::createBuffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
_box_shader->enableVertexAttribute("position", _verticesVBO, 2, GL_FLOAT, GL_TRUE,
2 * sizeof(float), 0);

View file

@ -172,6 +172,38 @@ void FixedSurfaceRenderer::restorePreviousState() {
#if defined(USE_OPENGL_SHADERS)
static const char *boxVertex =
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"uniform vec2 offsetXY;\n"
"uniform vec2 sizeWH;\n"
"uniform vec2 texcrop;\n"
"uniform bool flipY;\n"
"varying vec2 Texcoord;\n"
"void main() {\n"
"Texcoord = texcoord * texcrop;\n"
"vec2 pos = offsetXY + position * sizeWH;\n"
"pos.x = pos.x * 2.0 - 1.0;\n"
"pos.y = pos.y * 2.0 - 1.0;\n"
"if (flipY)\n"
"pos.y *= -1.0;\n"
"gl_Position = vec4(pos, 0.0, 1.0);\n"
"}\n";
static const char *boxFragment =
"#ifdef GL_ES\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"#endif\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main() {\n"
"gl_FragColor = texture2D(tex, Texcoord);\n"
"}\n";
ShaderSurfaceRenderer::ShaderSurfaceRenderer() {
const GLfloat vertices[] = {
0.0, 0.0,
@ -182,7 +214,7 @@ ShaderSurfaceRenderer::ShaderSurfaceRenderer() {
// Setup the box shader used to render the overlay
const char *attributes[] = { "position", "texcoord", nullptr };
_boxShader = ShaderGL::fromStrings("box", BuiltinShaders::boxVertex, BuiltinShaders::boxFragment, attributes);
_boxShader = ShaderGL::fromStrings("box", boxVertex, boxFragment, attributes);
_boxVerticesVBO = ShaderGL::createBuffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
_boxShader->enableVertexAttribute("position", _boxVerticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
_boxShader->enableVertexAttribute("texcoord", _boxVerticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);

View file

@ -76,11 +76,43 @@ const GLfloat vertices[] = {
1.0, 1.0,
};
static const char *controlVertex =
"#version 100\n"
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"uniform vec2 offsetXY;\n"
"uniform vec2 sizeWH;\n"
"uniform vec4 clip;\n"
"uniform bool flipY;\n"
"varying vec2 Texcoord;\n"
"void main() {\n"
"Texcoord = clip.xy + texcoord * (clip.zw - clip.xy);\n"
"vec2 pos = offsetXY + position * sizeWH;\n"
"pos.x = pos.x * 2.0 - 1.0;\n"
"pos.y = pos.y * 2.0 - 1.0;\n"
"if (flipY)\n"
"pos.y *= -1.0;\n"
"gl_Position = vec4(pos, 0.0, 1.0);\n"
"}\n";
static const char *controlFragment =
"#version 100\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main() {\n"
"gl_FragColor = texture2D(tex, Texcoord);\n"
"}\n";
void GLESBaseTexture::initGL() {
npot_supported = OpenGLContext.NPOTSupported;
const char* attributes[] = { "position", "texcoord", NULL };
g_box_shader = OpenGL::ShaderGL::fromStrings("control", OpenGL::BuiltinShaders::controlVertex, OpenGL::BuiltinShaders::controlFragment, attributes);
g_box_shader = OpenGL::ShaderGL::fromStrings("control", controlVertex, controlFragment, attributes);
g_verticesVBO = OpenGL::ShaderGL::createBuffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
g_box_shader->enableVertexAttribute("position", g_verticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
g_box_shader->enableVertexAttribute("texcoord", g_verticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);

View file

@ -29,10 +29,7 @@ MODULE_OBJS := \
macgui/macwindowmanager.o \
managed_surface.o \
nine_patch.o \
opengl/box_shaders.o \
opengl/context.o \
opengl/control_shaders.o \
opengl/compat_shaders.o \
opengl/shader.o \
pixelformat.o \
primitives.o \

View file

@ -1,64 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/scummsys.h"
#if defined(USE_OPENGL_SHADERS)
namespace OpenGL {
namespace BuiltinShaders {
const char *boxVertex =
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"uniform vec2 offsetXY;\n"
"uniform vec2 sizeWH;\n"
"uniform vec2 texcrop;\n"
"uniform bool flipY;\n"
"varying vec2 Texcoord;\n"
"void main() {\n"
"Texcoord = texcoord * texcrop;\n"
"vec2 pos = offsetXY + position * sizeWH;\n"
"pos.x = pos.x * 2.0 - 1.0;\n"
"pos.y = pos.y * 2.0 - 1.0;\n"
"if (flipY)\n"
"pos.y *= -1.0;\n"
"gl_Position = vec4(pos, 0.0, 1.0);\n"
"}\n";
const char *boxFragment =
"#ifdef GL_ES\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"#endif\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main() {\n"
"gl_FragColor = texture2D(tex, Texcoord);\n"
"}\n";
}
} // End of namespace OpenGL
#endif

View file

@ -1,66 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/scummsys.h"
#if defined(USE_OPENGL_SHADERS)
namespace OpenGL {
namespace BuiltinShaders {
const char *compatVertex =
"#if defined(GL_ES)\n"
"#define ROUND(x) (sign(x) * floor(abs(x) + .5))\n"
"#define in attribute\n"
"#define out varying\n"
"#elif __VERSION__ < 130\n"
"#define ROUND(x) (sign(x) * floor(abs(x) + .5))\n"
"#define highp\n"
"#define in attribute\n"
"#define out varying\n"
"#else\n"
"#define ROUND(x) round(x)\n"
"#endif\n";
const char *compatFragment =
"#if defined(GL_ES)\n"
"#define in varying\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"#define OUTPUT\n"
"#define outColor gl_FragColor\n"
"#define texture texture2D\n"
"#elif __VERSION__ < 130\n"
"#define in varying\n"
"#define OUTPUT\n"
"#define outColor gl_FragColor\n"
"#define texture texture2D\n"
"#else\n"
"#define OUTPUT out vec4 outColor;\n"
"#endif\n";
}
} // End of namespace OpenGL
#endif

View file

@ -1,64 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/scummsys.h"
#if defined(USE_OPENGL_SHADERS)
namespace OpenGL {
namespace BuiltinShaders {
const char *controlVertex =
"#version 100\n"
"attribute vec2 position;\n"
"attribute vec2 texcoord;\n"
"uniform vec2 offsetXY;\n"
"uniform vec2 sizeWH;\n"
"uniform vec4 clip;\n"
"uniform bool flipY;\n"
"varying vec2 Texcoord;\n"
"void main() {\n"
"Texcoord = clip.xy + texcoord * (clip.zw - clip.xy);\n"
"vec2 pos = offsetXY + position * sizeWH;\n"
"pos.x = pos.x * 2.0 - 1.0;\n"
"pos.y = pos.y * 2.0 - 1.0;\n"
"if (flipY)\n"
"pos.y *= -1.0;\n"
"gl_Position = vec4(pos, 0.0, 1.0);\n"
"}\n";
const char *controlFragment =
"#version 100\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"varying vec2 Texcoord;\n"
"uniform sampler2D tex;\n"
"void main() {\n"
"gl_FragColor = texture2D(tex, Texcoord);\n"
"}\n";
}
} // End of namespace OpenGL
#endif

View file

@ -30,6 +30,40 @@
namespace OpenGL {
static const char *compatVertex =
"#if defined(GL_ES)\n"
"#define ROUND(x) (sign(x) * floor(abs(x) + .5))\n"
"#define in attribute\n"
"#define out varying\n"
"#elif __VERSION__ < 130\n"
"#define ROUND(x) (sign(x) * floor(abs(x) + .5))\n"
"#define highp\n"
"#define in attribute\n"
"#define out varying\n"
"#else\n"
"#define ROUND(x) round(x)\n"
"#endif\n";
static const char *compatFragment =
"#if defined(GL_ES)\n"
"#define in varying\n"
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
"precision highp float;\n"
"#else\n"
"precision mediump float;\n"
"#endif\n"
"#define OUTPUT\n"
"#define outColor gl_FragColor\n"
"#define texture texture2D\n"
"#elif __VERSION__ < 130\n"
"#define in varying\n"
"#define OUTPUT\n"
"#define outColor gl_FragColor\n"
"#define texture texture2D\n"
"#else\n"
"#define OUTPUT out vec4 outColor;\n"
"#endif\n";
static const GLchar *readFile(const Common::String &filename) {
Common::File file;
@ -83,7 +117,7 @@ static GLuint createDirectShader(const char *shaderSource, GLenum shaderType, co
static GLuint createCompatShader(const char *shaderSource, GLenum shaderType, const Common::String &name) {
const GLchar *versionSource = OpenGLContext.type == kOGLContextGLES2 ? "#version 100\n" : "#version 120\n";
const GLchar *compatSource =
shaderType == GL_VERTEX_SHADER ? OpenGL::BuiltinShaders::compatVertex : OpenGL::BuiltinShaders::compatFragment;
shaderType == GL_VERTEX_SHADER ? compatVertex : compatFragment;
const GLchar *shaderSources[] = {
versionSource,
compatSource,
@ -167,7 +201,6 @@ ShaderGL *ShaderGL::fromStrings(const Common::String &name, const char *vertex,
return new ShaderGL(name, vertexShader, fragmentShader, attributes);
}
ShaderGL *ShaderGL::fromFiles(const char *vertex, const char *fragment, const char **attributes) {
GLuint vertexShader = loadShaderFromFile(vertex, "vertex", GL_VERTEX_SHADER);
GLuint fragmentShader = loadShaderFromFile(fragment, "fragment", GL_FRAGMENT_SHADER);

View file

@ -36,12 +36,6 @@
namespace OpenGL {
namespace BuiltinShaders {
extern const char *boxVertex, *boxFragment;
extern const char *compatVertex, *compatFragment;
extern const char *controlVertex, *controlFragment;
}
struct VertexAttrib {
VertexAttrib(uint32 idx, const char *name) :
_enabled(false), _idx(idx), _name(name), _vbo(0), _size(0),
@ -112,7 +106,6 @@ public:
glUniform1f(pos, f);
}
GLint getUniformLocation(const char *uniform) const {
UniformsMap::iterator kv = _uniforms->find(uniform);
if (kv == _uniforms->end()) {