-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move global shader related variables to new ShaderCache singleton
This fixes crash at exit now that SDL is a Singleton, too.
- Loading branch information
Showing
17 changed files
with
266 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2024 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#include "ShaderCache.hpp" | ||
|
||
#include "jngl/Shader.hpp" | ||
#include "opengl.hpp" | ||
#include "spriteimpl.hpp" | ||
|
||
#include <cassert> | ||
|
||
namespace jngl { | ||
|
||
ShaderCache::ShaderCache() { | ||
Shader vertexShader(R"(#version 300 es | ||
in mediump vec2 position; | ||
uniform highp mat3 modelview; | ||
uniform mediump mat4 projection; | ||
void main() { | ||
vec3 tmp = modelview * vec3(position, 1); | ||
gl_Position = projection * vec4(tmp.x, tmp.y, 0, 1); | ||
})", | ||
Shader::Type::VERTEX, R"(#version 100 | ||
attribute mediump vec2 position; | ||
uniform highp mat3 modelview; | ||
uniform mediump mat4 projection; | ||
void main() { | ||
vec3 tmp = modelview * vec3(position, 1); | ||
gl_Position = projection * vec4(tmp.x, tmp.y, 0, 1); | ||
})"); | ||
Shader fragmentShader(R"(#version 300 es | ||
uniform lowp vec4 color; | ||
out lowp vec4 outColor; | ||
void main() { | ||
outColor = color; | ||
})", | ||
Shader::Type::FRAGMENT, R"(#version 100 | ||
uniform lowp vec4 color; | ||
void main() { | ||
gl_FragColor = color; | ||
})"); | ||
simpleShaderProgram = std::make_unique<ShaderProgram>(vertexShader, fragmentShader); | ||
simpleModelviewUniform = simpleShaderProgram->getUniformLocation("modelview"); | ||
simpleColorUniform = simpleShaderProgram->getUniformLocation("color"); | ||
|
||
{ | ||
textureVertexShader = std::make_unique<Shader>(R"(#version 300 es | ||
in mediump vec2 position; | ||
in mediump vec2 inTexCoord; | ||
uniform highp mat3 modelview; | ||
uniform mediump mat4 projection; | ||
out mediump vec2 texCoord; | ||
void main() { | ||
vec3 tmp = modelview * vec3(position, 1); | ||
gl_Position = projection * vec4(tmp.x, tmp.y, 0, 1); | ||
texCoord = inTexCoord; | ||
})", | ||
Shader::Type::VERTEX, R"(#version 100 | ||
attribute mediump vec2 position; | ||
attribute mediump vec2 inTexCoord; | ||
uniform highp mat3 modelview; | ||
uniform mediump mat4 projection; | ||
varying mediump vec2 texCoord; | ||
void main() { | ||
vec3 tmp = modelview * vec3(position, 1); | ||
gl_Position = projection * vec4(tmp.x, tmp.y, 0, 1); | ||
texCoord = inTexCoord; | ||
})"); | ||
Shader fragmentShader(R"(#version 300 es | ||
uniform sampler2D tex; | ||
uniform lowp vec4 spriteColor; | ||
in mediump vec2 texCoord; | ||
out lowp vec4 outColor; | ||
void main() { | ||
outColor = texture(tex, texCoord) * spriteColor; | ||
})", | ||
Shader::Type::FRAGMENT, R"(#version 100 | ||
uniform sampler2D tex; | ||
uniform lowp vec4 spriteColor; | ||
varying mediump vec2 texCoord; | ||
void main() { | ||
gl_FragColor = texture2D(tex, texCoord) * spriteColor; | ||
})"); | ||
textureShaderProgram = | ||
std::make_unique<ShaderProgram>(*textureVertexShader, fragmentShader); | ||
shaderSpriteColorUniform = textureShaderProgram->getUniformLocation("spriteColor"); | ||
modelviewUniform = textureShaderProgram->getUniformLocation("modelview"); | ||
} | ||
} | ||
|
||
ShaderCache::~ShaderCache() = default; | ||
|
||
ShaderProgram::Context ShaderCache::useSimpleShaderProgram() { | ||
return useSimpleShaderProgram(opengl::modelview, gShapeColor); | ||
} | ||
|
||
ShaderProgram::Context ShaderCache::useSimpleShaderProgram(const Mat3& modelview, Rgba color) { | ||
auto context = simpleShaderProgram->use(); | ||
glUniform4f(simpleColorUniform, color.getRed(), color.getGreen(), color.getBlue(), | ||
color.getAlpha()); | ||
glUniformMatrix3fv(simpleModelviewUniform, 1, GL_FALSE, modelview.data); | ||
|
||
assert(simpleShaderProgram->getAttribLocation("position") == 0); | ||
glEnableVertexAttribArray(0); | ||
|
||
return context; | ||
} | ||
|
||
} // namespace jngl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#pragma once | ||
|
||
#include "jngl/Rgba.hpp" | ||
#include "jngl/ShaderProgram.hpp" | ||
#include "jngl/Singleton.hpp" | ||
|
||
namespace jngl { | ||
|
||
class Mat3; | ||
|
||
class ShaderCache : public Singleton<ShaderCache> { | ||
public: | ||
ShaderCache(); | ||
~ShaderCache(); | ||
|
||
ShaderProgram::Context useSimpleShaderProgram(); | ||
ShaderProgram::Context useSimpleShaderProgram(const Mat3& modelview, Rgba color); | ||
|
||
std::unique_ptr<Shader> textureVertexShader; | ||
std::unique_ptr<ShaderProgram> textureShaderProgram; | ||
int shaderSpriteColorUniform; | ||
int modelviewUniform; | ||
|
||
private: | ||
std::unique_ptr<ShaderProgram> simpleShaderProgram; | ||
int simpleModelviewUniform; | ||
int simpleColorUniform; | ||
}; | ||
|
||
} // namespace jngl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
// Copyright 2007-2024 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
|
||
#pragma once | ||
|
||
#include "jngl/Pixels.hpp" | ||
|
@@ -12,9 +11,12 @@ | |
#include FT_STROKER_H | ||
|
||
#include <map> | ||
#include <memory> | ||
|
||
namespace jngl { | ||
|
||
class Finally; | ||
|
||
constexpr double LINE_HEIGHT_FACOTR = 1. / .63; | ||
extern Rgba gFontColor; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
// Copyright 2021-2023 Jan Niklas Hasse <[email protected]> | ||
// Copyright 2021-2024 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#include "ImageData.hpp" | ||
|
||
#include "../helper.hpp" | ||
#include "../jngl/debug.hpp" | ||
#include "../main.hpp" | ||
#include "Finally.hpp" | ||
#include "debug.hpp" | ||
|
||
#ifdef ANDROID | ||
#include "../android/fopen.hpp" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
// Copyright 2018-2024 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
|
||
#include "Video.hpp" | ||
|
||
#include <stdexcept> | ||
|
@@ -16,6 +15,7 @@ | |
#include "../theoraplay/theoraplay.h" | ||
#include "Channel.hpp" | ||
#include "Shader.hpp" | ||
#include "ShaderProgram.hpp" | ||
#include "screen.hpp" | ||
#include "time.hpp" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
|
||
#include "shapes.hpp" | ||
|
||
#include "../main.hpp" | ||
#include "../ShaderCache.hpp" | ||
#include "../opengl.hpp" | ||
#include "../spriteimpl.hpp" | ||
#include "Alpha.hpp" | ||
|
@@ -65,8 +64,8 @@ void drawEllipse(const Vec2 position, const float width, const float height, | |
|
||
void drawEllipse(Mat3 modelview, float width, float height, float startAngle) { | ||
glBindVertexArray(opengl::vaoStream); | ||
auto tmp = | ||
useSimpleShaderProgram(modelview.scale(static_cast<float>(getScaleFactor())), gShapeColor); | ||
auto tmp = ShaderCache::handle().useSimpleShaderProgram( | ||
modelview.scale(static_cast<float>(getScaleFactor())), gShapeColor); | ||
std::vector<float> vertexes; | ||
vertexes.push_back(0.f); | ||
vertexes.push_back(0.f); | ||
|
@@ -101,7 +100,8 @@ void drawCircle(Mat3 modelview, const float radius, const Rgba color) { | |
|
||
void drawCircle(Mat3 modelview, const Rgba color) { | ||
glBindVertexArray(opengl::vaoStream); | ||
auto tmp = useSimpleShaderProgram(modelview.scale(static_cast<float>(getScaleFactor())), color); | ||
auto tmp = ShaderCache::handle().useSimpleShaderProgram( | ||
modelview.scale(static_cast<float>(getScaleFactor())), color); | ||
// clang-format off | ||
const static float vertexes[] = { | ||
1.f, 0.f, 0.9951847f, 0.09801714f, 0.9807853f, 0.1950903f, 0.9569403f, 0.2902847f, | ||
|
Oops, something went wrong.