osb/source/application/StarRenderer_opengl.cpp

1069 lines
37 KiB
C++
Raw Normal View History

#include "StarRenderer_opengl.hpp"
2023-06-20 04:33:09 +00:00
#include "StarJsonExtra.hpp"
#include "StarCasting.hpp"
#include "StarLogging.hpp"
namespace Star {
size_t const MultiTextureCount = 4;
char const* DefaultVertexShader = R"SHADER(
2024-07-06 21:32:06 +00:00
#version 140
uniform vec2 textureSize0;
uniform vec2 textureSize1;
uniform vec2 textureSize2;
uniform vec2 textureSize3;
uniform vec2 screenSize;
uniform mat3 vertexTransform;
in vec2 vertexPosition;
in vec4 vertexColor;
in vec2 vertexTextureCoordinate;
in int vertexData;
out vec2 fragmentTextureCoordinate;
flat out int fragmentTextureIndex;
out vec4 fragmentColor;
void main() {
vec2 screenPosition = (vertexTransform * vec3(vertexPosition, 1.0)).xy;
gl_Position = vec4(screenPosition / screenSize * 2.0 - 1.0, 0.0, 1.0);
if (((vertexData >> 3) & 0x1) == 1)
screenPosition.x = round(screenPosition.x);
if (((vertexData >> 4) & 0x1) == 1)
screenPosition.y = round(screenPosition.y);
int vertexTextureIndex = vertexData & 0x3;
if (vertexTextureIndex == 3)
fragmentTextureCoordinate = vertexTextureCoordinate / textureSize3;
else if (vertexTextureIndex == 2)
fragmentTextureCoordinate = vertexTextureCoordinate / textureSize2;
else if (vertexTextureIndex == 1)
fragmentTextureCoordinate = vertexTextureCoordinate / textureSize1;
else
fragmentTextureCoordinate = vertexTextureCoordinate / textureSize0;
fragmentTextureIndex = vertexTextureIndex;
fragmentColor = vertexColor;
}
)SHADER";
char const* DefaultFragmentShader = R"SHADER(
2024-07-06 21:32:06 +00:00
#version 140
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
in vec2 fragmentTextureCoordinate;
flat in int fragmentTextureIndex;
in vec4 fragmentColor;
out vec4 outColor;
void main() {
vec4 texColor;
if (fragmentTextureIndex == 3)
2024-07-06 21:32:06 +00:00
texColor = texture(texture3, fragmentTextureCoordinate);
else if (fragmentTextureIndex == 2)
2024-07-06 21:32:06 +00:00
texColor = texture(texture2, fragmentTextureCoordinate);
else if (fragmentTextureIndex == 1)
2024-07-06 21:32:06 +00:00
texColor = texture(texture1, fragmentTextureCoordinate);
else
2024-07-06 21:32:06 +00:00
texColor = texture(texture0, fragmentTextureCoordinate);
if (texColor.a <= 0.0)
discard;
outColor = texColor * fragmentColor;
}
)SHADER";
2023-06-20 04:33:09 +00:00
/*
static void GLAPIENTRY GlMessageCallback(GLenum, GLenum type, GLuint, GLenum, GLsizei, const GLchar* message, const void* renderer) {
if (type == GL_DEBUG_TYPE_ERROR) {
Logger::error("GL ERROR: {}", message);
__debugbreak();
}
}
*/
OpenGlRenderer::OpenGlRenderer() {
2023-06-20 04:33:09 +00:00
if (glewInit() != GLEW_OK)
throw RendererException("Could not initialize GLEW");
if (!GLEW_VERSION_2_0)
throw RendererException("OpenGL 2.0 not available!");
2023-06-27 10:23:44 +00:00
Logger::info("OpenGL version: '{}' vendor: '{}' renderer: '{}' shader: '{}'",
(const char*)glGetString(GL_VERSION),
(const char*)glGetString(GL_VENDOR),
(const char*)glGetString(GL_RENDERER),
(const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
2023-06-20 04:33:09 +00:00
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
//glEnable(GL_DEBUG_OUTPUT);
//glDebugMessageCallback(GlMessageCallback, this);
2023-06-20 04:33:09 +00:00
m_whiteTexture = createGlTexture(Image::filled({1, 1}, Vec4B(255, 255, 255, 255), PixelFormat::RGBA32),
TextureAddressing::Clamp,
TextureFiltering::Nearest);
m_immediateRenderBuffer = createGlRenderBuffer();
2023-06-29 19:55:41 +00:00
loadEffectConfig("internal", JsonObject(), {{"vertex", DefaultVertexShader}, {"fragment", DefaultFragmentShader}});
2023-06-20 04:33:09 +00:00
m_limitTextureGroupSize = false;
m_useMultiTexturing = true;
m_multiSampling = false;
2023-06-20 04:33:09 +00:00
logGlErrorSummary("OpenGL errors during renderer initialization");
}
OpenGlRenderer::~OpenGlRenderer() {
2023-06-29 19:55:41 +00:00
for (auto& effect : m_effects)
glDeleteProgram(effect.second.program);
2023-06-20 04:33:09 +00:00
m_frameBuffers.clear();
2023-06-20 04:33:09 +00:00
logGlErrorSummary("OpenGL errors during shutdown");
}
String OpenGlRenderer::rendererId() const {
2023-06-20 04:33:09 +00:00
return "OpenGL20";
}
Vec2U OpenGlRenderer::screenSize() const {
2023-06-20 04:33:09 +00:00
return m_screenSize;
}
OpenGlRenderer::GlFrameBuffer::GlFrameBuffer(Json const& fbConfig) : config(fbConfig) {
texture = make_ref<GlLoneTexture>();
texture->textureFiltering = TextureFiltering::Nearest;
texture->textureAddressing = TextureAddressing::Clamp;
texture->textureSize = {0, 0};
glGenTextures(1, &texture->textureId);
if (texture->textureId == 0)
throw RendererException("Could not generate OpenGL texture for framebuffer");
multisample = GLEW_VERSION_4_0 ? config.getUInt("multisample", 0) : 0;
GLenum target = multisample ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
glBindTexture(target, texture->glTextureId());
Vec2U size = jsonToVec2U(config.getArray("size", { 256, 256 }));
if (multisample)
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, multisample, GL_RGBA8, size[0], size[1], GL_TRUE);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size[0], size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glGenFramebuffers(1, &id);
if (!id)
throw RendererException("Failed to create OpenGL framebuffer");
glBindFramebuffer(GL_FRAMEBUFFER, id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, texture->glTextureId(), 0);
auto framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE)
throw RendererException("OpenGL framebuffer is not complete!");
}
OpenGlRenderer::GlFrameBuffer::~GlFrameBuffer() {
glDeleteFramebuffers(1, &id);
texture.reset();
}
void OpenGlRenderer::loadConfig(Json const& config) {
m_frameBuffers.clear();
for (auto& pair : config.getObject("frameBuffers", {})) {
Json config = pair.second;
config = config.set("multisample", m_multiSampling);
m_frameBuffers[pair.first] = make_ref<GlFrameBuffer>(config);
2024-03-19 14:53:34 +00:00
}
2024-03-19 14:53:34 +00:00
setScreenSize(m_screenSize);
m_config = config;
}
void OpenGlRenderer::loadEffectConfig(String const& name, Json const& effectConfig, StringMap<String> const& shaders) {
2024-03-19 14:53:34 +00:00
if (auto effect = m_effects.ptr(name)) {
Logger::info("Reloading OpenGL effect {}", name);
glDeleteProgram(effect->program);
m_effects.erase(name);
2023-06-29 19:55:41 +00:00
}
2023-06-20 04:33:09 +00:00
GLint status = 0;
char logBuffer[1024];
auto compileShader = [&](GLenum type, String const& name) -> GLuint {
GLuint shader = glCreateShader(type);
auto* source = shaders.ptr(name);
if (!source)
return 0;
char const* sourcePtr = source->utf8Ptr();
glShaderSource(shader, 1, &sourcePtr, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!status) {
glGetShaderInfoLog(shader, sizeof(logBuffer), NULL, logBuffer);
2023-06-27 10:23:44 +00:00
throw RendererException(strf("Failed to compile {} shader: {}\n", name, logBuffer));
}
2023-06-20 04:33:09 +00:00
return shader;
};
2023-06-20 04:33:09 +00:00
2024-03-19 14:53:34 +00:00
GLuint vertexShader = 0, fragmentShader = 0;
try {
vertexShader = compileShader(GL_VERTEX_SHADER, "vertex");
fragmentShader = compileShader(GL_FRAGMENT_SHADER, "fragment");
}
catch (RendererException const& e) {
Logger::error("Shader compile error, using default: {}", e.what());
if (vertexShader) glDeleteShader(vertexShader);
if (fragmentShader) glDeleteShader(fragmentShader);
vertexShader = compileShader(GL_VERTEX_SHADER, DefaultVertexShader);
fragmentShader = compileShader(GL_FRAGMENT_SHADER, DefaultFragmentShader);
}
2023-06-20 04:33:09 +00:00
GLuint program = glCreateProgram();
if (vertexShader)
glAttachShader(program, vertexShader);
if (fragmentShader)
glAttachShader(program, fragmentShader);
2023-06-20 04:33:09 +00:00
glLinkProgram(program);
if (vertexShader)
glDeleteShader(vertexShader);
if (fragmentShader)
glDeleteShader(fragmentShader);
2023-06-20 04:33:09 +00:00
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
glGetProgramInfoLog(program, sizeof(logBuffer), NULL, logBuffer);
glDeleteProgram(program);
2023-06-27 10:23:44 +00:00
throw RendererException(strf("Failed to link program: {}\n", logBuffer));
2023-06-20 04:33:09 +00:00
}
2023-06-29 19:55:41 +00:00
glUseProgram(m_program = program);
2023-06-20 04:33:09 +00:00
auto& effect = m_effects.emplace(name, Effect()).first->second;
effect.program = m_program;
effect.config = effectConfig;
2023-06-29 19:55:41 +00:00
m_currentEffect = &effect;
setupGlUniforms(effect);
2023-06-20 04:33:09 +00:00
for (auto const& p : effectConfig.getObject("effectParameters", {})) {
EffectParameter effectParameter;
effectParameter.parameterUniform = glGetUniformLocation(m_program, p.second.getString("uniform").utf8Ptr());
if (effectParameter.parameterUniform == -1) {
Logger::warn("OpenGL20 effect parameter '{}' in effect '{}' has no associated uniform, skipping", p.first, name);
2023-06-20 04:33:09 +00:00
} else {
String type = p.second.getString("type");
if (type == "bool") {
effectParameter.parameterType = RenderEffectParameter::typeIndexOf<bool>();
} else if (type == "int") {
effectParameter.parameterType = RenderEffectParameter::typeIndexOf<int>();
} else if (type == "float") {
effectParameter.parameterType = RenderEffectParameter::typeIndexOf<float>();
} else if (type == "vec2") {
effectParameter.parameterType = RenderEffectParameter::typeIndexOf<Vec2F>();
} else if (type == "vec3") {
effectParameter.parameterType = RenderEffectParameter::typeIndexOf<Vec3F>();
} else if (type == "vec4") {
effectParameter.parameterType = RenderEffectParameter::typeIndexOf<Vec4F>();
} else {
2023-06-27 10:23:44 +00:00
throw RendererException::format("Unrecognized effect parameter type '{}'", type);
2023-06-20 04:33:09 +00:00
}
2023-06-29 19:55:41 +00:00
effect.parameters[p.first] = effectParameter;
2023-06-20 04:33:09 +00:00
if (Json def = p.second.get("default", {})) {
if (type == "bool") {
setEffectParameter(p.first, def.toBool());
} else if (type == "int") {
setEffectParameter(p.first, (int)def.toInt());
} else if (type == "float") {
setEffectParameter(p.first, def.toFloat());
} else if (type == "vec2") {
setEffectParameter(p.first, jsonToVec2F(def));
} else if (type == "vec3") {
setEffectParameter(p.first, jsonToVec3F(def));
} else if (type == "vec4") {
setEffectParameter(p.first, jsonToVec4F(def));
}
}
}
}
// Assign each texture parameter a texture unit starting with MultiTextureCount, the first
// few texture units are used by the primary textures being drawn. Currently,
// maximum texture units are not checked.
unsigned parameterTextureUnit = MultiTextureCount;
for (auto const& p : effectConfig.getObject("effectTextures", {})) {
EffectTexture effectTexture;
effectTexture.textureUniform = glGetUniformLocation(m_program, p.second.getString("textureUniform").utf8Ptr());
if (effectTexture.textureUniform == -1) {
2023-06-27 10:23:44 +00:00
Logger::warn("OpenGL20 effect parameter '{}' has no associated uniform, skipping", p.first);
2023-06-20 04:33:09 +00:00
} else {
effectTexture.textureUnit = parameterTextureUnit++;
glUniform1i(effectTexture.textureUniform, effectTexture.textureUnit);
effectTexture.textureAddressing = TextureAddressingNames.getLeft(p.second.getString("textureAddressing", "clamp"));
effectTexture.textureFiltering = TextureFilteringNames.getLeft(p.second.getString("textureFiltering", "nearest"));
if (auto tsu = p.second.optString("textureSizeUniform")) {
effectTexture.textureSizeUniform = glGetUniformLocation(m_program, tsu->utf8Ptr());
if (effectTexture.textureSizeUniform == -1)
2023-06-27 10:23:44 +00:00
Logger::warn("OpenGL20 effect parameter '{}' has textureSizeUniform '{}' with no associated uniform", p.first, *tsu);
2023-06-20 04:33:09 +00:00
}
2023-06-29 19:55:41 +00:00
effect.textures[p.first] = effectTexture;
2023-06-20 04:33:09 +00:00
}
}
if (DebugEnabled)
logGlErrorSummary("OpenGL errors setting effect config");
}
void OpenGlRenderer::setEffectParameter(String const& parameterName, RenderEffectParameter const& value) {
2023-06-29 19:55:41 +00:00
auto ptr = m_currentEffect->parameters.ptr(parameterName);
2023-06-20 04:33:09 +00:00
if (!ptr || (ptr->parameterValue && *ptr->parameterValue == value))
return;
if (ptr->parameterType != value.typeIndex())
throw RendererException::format("OpenGlRenderer::setEffectParameter '{}' parameter type mismatch", parameterName);
2023-06-20 04:33:09 +00:00
flushImmediatePrimitives();
if (auto v = value.ptr<bool>())
glUniform1i(ptr->parameterUniform, *v);
else if (auto v = value.ptr<int>())
glUniform1i(ptr->parameterUniform, *v);
else if (auto v = value.ptr<float>())
glUniform1f(ptr->parameterUniform, *v);
else if (auto v = value.ptr<Vec2F>())
glUniform2f(ptr->parameterUniform, (*v)[0], (*v)[1]);
else if (auto v = value.ptr<Vec3F>())
glUniform3f(ptr->parameterUniform, (*v)[0], (*v)[1], (*v)[2]);
else if (auto v = value.ptr<Vec4F>())
glUniform4f(ptr->parameterUniform, (*v)[0], (*v)[1], (*v)[2], (*v)[3]);
ptr->parameterValue = value;
}
void OpenGlRenderer::setEffectTexture(String const& textureName, ImageView const& image) {
2023-06-29 19:55:41 +00:00
auto ptr = m_currentEffect->textures.ptr(textureName);
2023-06-20 04:33:09 +00:00
if (!ptr)
return;
flushImmediatePrimitives();
if (!ptr->textureValue || ptr->textureValue->textureId == 0) {
ptr->textureValue = createGlTexture(image, ptr->textureAddressing, ptr->textureFiltering);
} else {
glBindTexture(GL_TEXTURE_2D, ptr->textureValue->textureId);
2024-03-19 14:53:34 +00:00
ptr->textureValue->textureSize = image.size;
uploadTextureImage(image.format, image.size, image.data);
2023-06-20 04:33:09 +00:00
}
if (ptr->textureSizeUniform != -1) {
auto textureSize = ptr->textureValue->glTextureSize();
glUniform2f(ptr->textureSizeUniform, textureSize[0], textureSize[1]);
}
}
bool OpenGlRenderer::switchEffectConfig(String const& name) {
2023-06-29 19:55:41 +00:00
flushImmediatePrimitives();
auto find = m_effects.find(name);
if (find == m_effects.end())
return false;
Effect& effect = find->second;
if (m_currentEffect == &effect)
return true;
if (auto blitFrameBufferId = effect.config.optString("blitFrameBuffer"))
blitGlFrameBuffer(getGlFrameBuffer(*blitFrameBufferId));
if (auto frameBufferId = effect.config.optString("frameBuffer"))
switchGlFrameBuffer(getGlFrameBuffer(*frameBufferId));
else {
m_currentFrameBuffer.reset();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
2023-06-29 19:55:41 +00:00
glUseProgram(m_program = effect.program);
setupGlUniforms(effect);
2023-06-29 19:55:41 +00:00
m_currentEffect = &effect;
setEffectParameter("vertexRounding", m_multiSampling > 0);
2023-06-29 19:55:41 +00:00
return true;
}
void OpenGlRenderer::setScissorRect(Maybe<RectI> const& scissorRect) {
2023-06-20 04:33:09 +00:00
if (scissorRect == m_scissorRect)
return;
flushImmediatePrimitives();
m_scissorRect = scissorRect;
if (m_scissorRect) {
glEnable(GL_SCISSOR_TEST);
glScissor(m_scissorRect->xMin(), m_scissorRect->yMin(), m_scissorRect->width(), m_scissorRect->height());
} else {
glDisable(GL_SCISSOR_TEST);
}
}
TexturePtr OpenGlRenderer::createTexture(Image const& texture, TextureAddressing addressing, TextureFiltering filtering) {
2023-06-20 04:33:09 +00:00
return createGlTexture(texture, addressing, filtering);
}
void OpenGlRenderer::setSizeLimitEnabled(bool enabled) {
2023-06-20 04:33:09 +00:00
m_limitTextureGroupSize = enabled;
}
void OpenGlRenderer::setMultiTexturingEnabled(bool enabled) {
2023-06-20 04:33:09 +00:00
m_useMultiTexturing = enabled;
}
void OpenGlRenderer::setMultiSampling(unsigned multiSampling) {
if (m_multiSampling == multiSampling)
return;
m_multiSampling = multiSampling;
if (m_multiSampling) {
glEnable(GL_MULTISAMPLE);
glEnable(GL_SAMPLE_SHADING);
glMinSampleShading(1.f);
} else {
glMinSampleShading(0.f);
glDisable(GL_SAMPLE_SHADING);
glDisable(GL_MULTISAMPLE);
}
loadConfig(m_config);
}
TextureGroupPtr OpenGlRenderer::createTextureGroup(TextureGroupSize textureSize, TextureFiltering filtering) {
2023-06-20 04:33:09 +00:00
int maxTextureSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
maxTextureSize = min(maxTextureSize, (2 << 14));
2023-06-20 04:33:09 +00:00
// Large texture sizes are not always supported
if (textureSize == TextureGroupSize::Large && (m_limitTextureGroupSize || maxTextureSize < 4096))
textureSize = TextureGroupSize::Medium;
unsigned atlasNumCells;
if (textureSize == TextureGroupSize::Large)
atlasNumCells = 256;
else if (textureSize == TextureGroupSize::Medium)
atlasNumCells = 128;
else // TextureGroupSize::Small
atlasNumCells = 64;
2023-06-27 10:23:44 +00:00
Logger::info("detected supported OpenGL texture size {}, using atlasNumCells {}", maxTextureSize, atlasNumCells);
2023-06-20 04:33:09 +00:00
auto glTextureGroup = make_shared<GlTextureGroup>(atlasNumCells);
glTextureGroup->textureAtlasSet.textureFiltering = filtering;
m_liveTextureGroups.append(glTextureGroup);
return glTextureGroup;
}
RenderBufferPtr OpenGlRenderer::createRenderBuffer() {
2023-06-20 04:33:09 +00:00
return createGlRenderBuffer();
}
List<RenderPrimitive>& OpenGlRenderer::immediatePrimitives() {
2023-06-29 18:34:10 +00:00
return m_immediatePrimitives;
}
void OpenGlRenderer::render(RenderPrimitive primitive) {
m_immediatePrimitives.append(std::move(primitive));
2023-06-20 04:33:09 +00:00
}
void OpenGlRenderer::renderBuffer(RenderBufferPtr const& renderBuffer, Mat3F const& transformation) {
2023-06-20 04:33:09 +00:00
flushImmediatePrimitives();
renderGlBuffer(*convert<GlRenderBuffer>(renderBuffer.get()), transformation);
}
void OpenGlRenderer::flush(Mat3F const& transformation) {
flushImmediatePrimitives(transformation);
2023-06-20 04:33:09 +00:00
}
void OpenGlRenderer::setScreenSize(Vec2U screenSize) {
2023-06-20 04:33:09 +00:00
m_screenSize = screenSize;
glViewport(0, 0, m_screenSize[0], m_screenSize[1]);
glUniform2f(m_screenSizeUniform, m_screenSize[0], m_screenSize[1]);
for (auto& frameBuffer : m_frameBuffers) {
if (unsigned multisample = frameBuffer.second->multisample) {
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, frameBuffer.second->texture->glTextureId());
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, multisample, GL_RGBA8, m_screenSize[0], m_screenSize[1], GL_TRUE);
} else {
glBindTexture(GL_TEXTURE_2D, frameBuffer.second->texture->glTextureId());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_screenSize[0], m_screenSize[1], 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
}
}
2023-06-20 04:33:09 +00:00
}
void OpenGlRenderer::startFrame() {
2023-06-20 04:33:09 +00:00
if (m_scissorRect)
glDisable(GL_SCISSOR_TEST);
for (auto& frameBuffer : m_frameBuffers) {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer.second->id);
glClear(GL_COLOR_BUFFER_BIT);
frameBuffer.second->blitted = false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2023-06-20 04:33:09 +00:00
glClear(GL_COLOR_BUFFER_BIT);
if (m_scissorRect)
glEnable(GL_SCISSOR_TEST);
}
void OpenGlRenderer::finishFrame() {
2023-06-20 04:33:09 +00:00
flushImmediatePrimitives();
// Make sure that the immediate render buffer doesn't needlessly lock texutres
// from being compressed.
2023-06-29 18:34:10 +00:00
List<RenderPrimitive> empty;
m_immediateRenderBuffer->set(empty);
2023-06-20 04:33:09 +00:00
filter(m_liveTextureGroups, [](auto const& p) {
unsigned const CompressionsPerFrame = 1;
if (!p.unique() || p->textureAtlasSet.totalTextures() > 0) {
p->textureAtlasSet.compressionPass(CompressionsPerFrame);
return true;
}
return false;
});
// Blit if another shader hasn't
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2023-06-20 04:33:09 +00:00
if (DebugEnabled)
logGlErrorSummary("OpenGL errors this frame");
}
OpenGlRenderer::GlTextureAtlasSet::GlTextureAtlasSet(unsigned atlasNumCells)
2023-06-20 04:33:09 +00:00
: TextureAtlasSet(16, atlasNumCells) {}
GLuint OpenGlRenderer::GlTextureAtlasSet::createAtlasTexture(Vec2U const& size, PixelFormat pixelFormat) {
2023-06-20 04:33:09 +00:00
GLuint glTextureId;
glGenTextures(1, &glTextureId);
if (glTextureId == 0)
throw RendererException("Could not generate texture in OpenGlRenderer::TextureGroup::createAtlasTexture()");
2023-06-20 04:33:09 +00:00
glBindTexture(GL_TEXTURE_2D, glTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (textureFiltering == TextureFiltering::Nearest) {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
uploadTextureImage(pixelFormat, size, nullptr);
return glTextureId;
}
void OpenGlRenderer::GlTextureAtlasSet::destroyAtlasTexture(GLuint const& glTexture) {
2023-06-20 04:33:09 +00:00
glDeleteTextures(1, &glTexture);
}
void OpenGlRenderer::GlTextureAtlasSet::copyAtlasPixels(
2023-06-20 04:33:09 +00:00
GLuint const& glTexture, Vec2U const& bottomLeft, Image const& image) {
glBindTexture(GL_TEXTURE_2D, glTexture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format;
auto pixelFormat = image.pixelFormat();
if (pixelFormat == PixelFormat::RGB24)
format = GL_RGB;
else if (pixelFormat == PixelFormat::RGBA32)
format = GL_RGBA;
else if (pixelFormat == PixelFormat::BGR24)
format = GL_BGR;
else if (pixelFormat == PixelFormat::BGRA32)
format = GL_BGRA;
else
throw RendererException("Unsupported texture format in OpenGlRenderer::TextureGroup::copyAtlasPixels");
glTexSubImage2D(GL_TEXTURE_2D, 0, bottomLeft[0], bottomLeft[1], image.width(), image.height(), format, GL_UNSIGNED_BYTE, image.data());
2023-06-20 04:33:09 +00:00
}
OpenGlRenderer::GlTextureGroup::GlTextureGroup(unsigned atlasNumCells)
2023-06-20 04:33:09 +00:00
: textureAtlasSet(atlasNumCells) {}
OpenGlRenderer::GlTextureGroup::~GlTextureGroup() {
2023-06-20 04:33:09 +00:00
textureAtlasSet.reset();
}
TextureFiltering OpenGlRenderer::GlTextureGroup::filtering() const {
2023-06-20 04:33:09 +00:00
return textureAtlasSet.textureFiltering;
}
TexturePtr OpenGlRenderer::GlTextureGroup::create(Image const& texture) {
2023-06-20 04:33:09 +00:00
// If the image is empty, or would not fit in the texture atlas with border
// pixels, just create a regular texture
Vec2U atlasTextureSize = textureAtlasSet.atlasTextureSize();
if (texture.empty() || texture.width() + 2 > atlasTextureSize[0] || texture.height() + 2 > atlasTextureSize[1])
return createGlTexture(texture, TextureAddressing::Clamp, textureAtlasSet.textureFiltering);
auto glGroupedTexture = make_ref<GlGroupedTexture>();
glGroupedTexture->parentGroup = shared_from_this();
glGroupedTexture->parentAtlasTexture = textureAtlasSet.addTexture(texture);
return glGroupedTexture;
}
OpenGlRenderer::GlGroupedTexture::~GlGroupedTexture() {
2023-06-20 04:33:09 +00:00
if (parentAtlasTexture)
parentGroup->textureAtlasSet.freeTexture(parentAtlasTexture);
}
Vec2U OpenGlRenderer::GlGroupedTexture::size() const {
2023-06-20 04:33:09 +00:00
return parentAtlasTexture->imageSize();
}
TextureFiltering OpenGlRenderer::GlGroupedTexture::filtering() const {
2023-06-20 04:33:09 +00:00
return parentGroup->filtering();
}
TextureAddressing OpenGlRenderer::GlGroupedTexture::addressing() const {
2023-06-20 04:33:09 +00:00
return TextureAddressing::Clamp;
}
GLuint OpenGlRenderer::GlGroupedTexture::glTextureId() const {
2023-06-20 04:33:09 +00:00
return parentAtlasTexture->atlasTexture();
}
Vec2U OpenGlRenderer::GlGroupedTexture::glTextureSize() const {
2023-06-20 04:33:09 +00:00
return parentGroup->textureAtlasSet.atlasTextureSize();
}
Vec2U OpenGlRenderer::GlGroupedTexture::glTextureCoordinateOffset() const {
2023-06-20 04:33:09 +00:00
return parentAtlasTexture->atlasTextureCoordinates().min();
}
void OpenGlRenderer::GlGroupedTexture::incrementBufferUseCount() {
2023-06-20 04:33:09 +00:00
if (bufferUseCount == 0)
parentAtlasTexture->setLocked(true);
++bufferUseCount;
}
void OpenGlRenderer::GlGroupedTexture::decrementBufferUseCount() {
2023-06-20 04:33:09 +00:00
starAssert(bufferUseCount != 0);
if (bufferUseCount == 1)
parentAtlasTexture->setLocked(false);
--bufferUseCount;
}
OpenGlRenderer::GlLoneTexture::~GlLoneTexture() {
2023-06-20 04:33:09 +00:00
if (textureId != 0)
glDeleteTextures(1, &textureId);
}
Vec2U OpenGlRenderer::GlLoneTexture::size() const {
2023-06-20 04:33:09 +00:00
return textureSize;
}
TextureFiltering OpenGlRenderer::GlLoneTexture::filtering() const {
2023-06-20 04:33:09 +00:00
return textureFiltering;
}
TextureAddressing OpenGlRenderer::GlLoneTexture::addressing() const {
2023-06-20 04:33:09 +00:00
return textureAddressing;
}
GLuint OpenGlRenderer::GlLoneTexture::glTextureId() const {
2023-06-20 04:33:09 +00:00
return textureId;
}
Vec2U OpenGlRenderer::GlLoneTexture::glTextureSize() const {
2023-06-20 04:33:09 +00:00
return textureSize;
}
Vec2U OpenGlRenderer::GlLoneTexture::glTextureCoordinateOffset() const {
2023-06-20 04:33:09 +00:00
return Vec2U();
}
OpenGlRenderer::GlRenderBuffer::GlRenderBuffer() {
glGenVertexArrays(1, &vertexArray);
}
OpenGlRenderer::GlRenderBuffer::~GlRenderBuffer() {
2023-06-20 04:33:09 +00:00
for (auto const& texture : usedTextures) {
if (auto gt = as<GlGroupedTexture>(texture.get()))
gt->decrementBufferUseCount();
}
for (auto const& vb : vertexBuffers)
glDeleteBuffers(1, &vb.vertexBuffer);
glDeleteVertexArrays(1, &vertexArray);
2023-06-20 04:33:09 +00:00
}
void OpenGlRenderer::GlRenderBuffer::set(List<RenderPrimitive>& primitives) {
2023-06-20 04:33:09 +00:00
for (auto const& texture : usedTextures) {
if (auto gt = as<GlGroupedTexture>(texture.get()))
gt->decrementBufferUseCount();
}
usedTextures.clear();
auto oldVertexBuffers = take(vertexBuffers);
List<GLuint> currentTextures;
List<Vec2U> currentTextureSizes;
size_t currentVertexCount = 0;
glBindVertexArray(vertexArray);
2023-06-20 04:33:09 +00:00
auto finishCurrentBuffer = [&]() {
if (currentVertexCount > 0) {
GlVertexBuffer vb;
for (size_t i = 0; i < currentTextures.size(); ++i) {
vb.textures.append(GlVertexBufferTexture{currentTextures[i], currentTextureSizes[i]});
}
vb.vertexCount = currentVertexCount;
if (!oldVertexBuffers.empty()) {
auto oldVb = oldVertexBuffers.takeLast();
vb.vertexBuffer = oldVb.vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vb.vertexBuffer);
if (oldVb.vertexCount >= vb.vertexCount)
glBufferSubData(GL_ARRAY_BUFFER, 0, accumulationBuffer.size(), accumulationBuffer.ptr());
else
glBufferData(GL_ARRAY_BUFFER, accumulationBuffer.size(), accumulationBuffer.ptr(), GL_STREAM_DRAW);
} else {
glGenBuffers(1, &vb.vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vb.vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, accumulationBuffer.size(), accumulationBuffer.ptr(), GL_STREAM_DRAW);
}
vertexBuffers.emplace_back(std::move(vb));
2023-06-20 04:33:09 +00:00
currentTextures.clear();
currentTextureSizes.clear();
accumulationBuffer.clear();
currentVertexCount = 0;
}
};
auto textureCount = useMultiTexturing ? MultiTextureCount : 1;
auto addCurrentTexture = [&](TexturePtr texture) -> pair<uint8_t, Vec2F> {
if (!texture)
texture = whiteTexture;
auto glTexture = as<GlTexture>(texture.get());
GLuint glTextureId = glTexture->glTextureId();
auto textureIndex = currentTextures.indexOf(glTextureId);
if (textureIndex == NPos) {
if (currentTextures.size() >= textureCount)
finishCurrentBuffer();
textureIndex = currentTextures.size();
currentTextures.append(glTextureId);
currentTextureSizes.append(glTexture->glTextureSize());
}
if (auto gt = as<GlGroupedTexture>(texture.get()))
gt->incrementBufferUseCount();
usedTextures.add(std::move(texture));
2023-06-20 04:33:09 +00:00
return {float(textureIndex), Vec2F(glTexture->glTextureCoordinateOffset())};
};
auto appendBufferVertex = [&](RenderVertex const& v, uint8_t textureIndex, Vec2F textureCoordinateOffset, RenderVertex const& prev, RenderVertex const& next) {
size_t off = accumulationBuffer.size();
accumulationBuffer.resize(accumulationBuffer.size() + sizeof(GlRenderVertex));
GlRenderVertex& glv = *(GlRenderVertex*)(accumulationBuffer.ptr() + off);
glv.pos = v.screenCoordinate;
glv.uv = v.textureCoordinate + textureCoordinateOffset;
glv.color = v.color;
glv.pack.vars.textureIndex = textureIndex;
glv.pack.vars.fullbright = v.param1 > 0.0f;
// Tell the vertex shader to round to the nearest pixel if the vertices form a straight
// edge, to ensure sharpness with supersampling. If we rounded *all* vertex positions,
// it'd cause slight visual issues with sprites rotating around a point.
glv.pack.vars.rX = min(abs(glv.pos.x() - prev.screenCoordinate.x()), abs(glv.pos.x() - next.screenCoordinate.x())) < 0.001f;
glv.pack.vars.rY = min(abs(glv.pos.y() - prev.screenCoordinate.y()), abs(glv.pos.y() - next.screenCoordinate.y())) < 0.001f;
glv.pack.vars.unused = 0;
2023-06-20 04:33:09 +00:00
++currentVertexCount;
return glv;
2023-06-20 04:33:09 +00:00
};
uint8_t textureIndex = 0;
Vec2F textureOffset = {};
2023-06-20 04:33:09 +00:00
for (auto& primitive : primitives) {
if (auto tri = primitive.ptr<RenderTriangle>()) {
tie(textureIndex, textureOffset) = addCurrentTexture(std::move(tri->texture));
2023-06-20 04:33:09 +00:00
appendBufferVertex(tri->a, textureIndex, textureOffset, tri->c, tri->b);
appendBufferVertex(tri->b, textureIndex, textureOffset, tri->a, tri->c);
appendBufferVertex(tri->c, textureIndex, textureOffset, tri->b, tri->a);
2023-06-20 04:33:09 +00:00
} else if (auto quad = primitive.ptr<RenderQuad>()) {
tie(textureIndex, textureOffset) = addCurrentTexture(std::move(quad->texture));
2023-06-20 04:33:09 +00:00
// = prev and next are altered - the diagonal across the quad is bad for the rounding check
appendBufferVertex(quad->a, textureIndex, textureOffset, quad->d, quad->b);
appendBufferVertex(quad->b, textureIndex, textureOffset, quad->a, quad->c); //
appendBufferVertex(quad->c, textureIndex, textureOffset, quad->b, quad->d);
2023-06-20 04:33:09 +00:00
appendBufferVertex(quad->a, textureIndex, textureOffset, quad->d, quad->b);
appendBufferVertex(quad->c, textureIndex, textureOffset, quad->b, quad->d); //
appendBufferVertex(quad->d, textureIndex, textureOffset, quad->c, quad->a);
2023-06-20 04:33:09 +00:00
} else if (auto poly = primitive.ptr<RenderPoly>()) {
if (poly->vertexes.size() > 2) {
tie(textureIndex, textureOffset) = addCurrentTexture(std::move(poly->texture));
2023-06-20 04:33:09 +00:00
for (size_t i = 1; i < poly->vertexes.size() - 1; ++i) {
RenderVertex const& a = poly->vertexes[0],
b = poly->vertexes[i],
c = poly->vertexes[i + 1];
appendBufferVertex(a, textureIndex, textureOffset, c, b);
appendBufferVertex(b, textureIndex, textureOffset, a, c);
appendBufferVertex(c, textureIndex, textureOffset, b, a);
2023-06-20 04:33:09 +00:00
}
}
}
}
2023-06-29 18:34:10 +00:00
vertexBuffers.reserve(primitives.size() * 6);
2023-06-20 04:33:09 +00:00
finishCurrentBuffer();
for (auto const& vb : oldVertexBuffers)
glDeleteBuffers(1, &vb.vertexBuffer);
}
bool OpenGlRenderer::logGlErrorSummary(String prefix) {
if (GLenum error = glGetError()) {
2023-06-30 17:58:02 +00:00
Logger::error("{}: ", prefix);
do {
2023-06-20 04:33:09 +00:00
if (error == GL_INVALID_ENUM) {
Logger::error("GL_INVALID_ENUM");
2023-06-20 04:33:09 +00:00
} else if (error == GL_INVALID_VALUE) {
Logger::error("GL_INVALID_VALUE");
2023-06-20 04:33:09 +00:00
} else if (error == GL_INVALID_OPERATION) {
Logger::error("GL_INVALID_OPERATION");
2023-06-20 04:33:09 +00:00
} else if (error == GL_INVALID_FRAMEBUFFER_OPERATION) {
Logger::error("GL_INVALID_FRAMEBUFFER_OPERATION");
2023-06-20 04:33:09 +00:00
} else if (error == GL_OUT_OF_MEMORY) {
Logger::error("GL_OUT_OF_MEMORY");
2023-06-20 04:33:09 +00:00
} else if (error == GL_STACK_UNDERFLOW) {
Logger::error("GL_STACK_UNDERFLOW");
2023-06-20 04:33:09 +00:00
} else if (error == GL_STACK_OVERFLOW) {
Logger::error("GL_STACK_OVERFLOW");
2023-06-20 04:33:09 +00:00
} else {
Logger::error("<UNRECOGNIZED GL ERROR>");
2023-06-20 04:33:09 +00:00
}
} while ((error = glGetError()));
2023-06-30 17:58:02 +00:00
return true;
2023-06-20 04:33:09 +00:00
}
2023-06-30 17:58:02 +00:00
return false;
2023-06-20 04:33:09 +00:00
}
void OpenGlRenderer::uploadTextureImage(PixelFormat pixelFormat, Vec2U size, uint8_t const* data) {
2023-06-20 04:33:09 +00:00
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2024-03-19 14:53:34 +00:00
Maybe<GLenum> internalFormat;
GLenum format;
2024-03-19 14:53:34 +00:00
GLenum type = GL_UNSIGNED_BYTE;
if (pixelFormat == PixelFormat::RGB24)
format = GL_RGB;
else if (pixelFormat == PixelFormat::RGBA32)
format = GL_RGBA;
else if (pixelFormat == PixelFormat::BGR24)
format = GL_BGR;
else if (pixelFormat == PixelFormat::BGRA32)
format = GL_BGRA;
2024-03-19 14:53:34 +00:00
else {
type = GL_FLOAT;
if (pixelFormat == PixelFormat::RGB_F) {
internalFormat = GL_RGB32F;
format = GL_RGB;
} else if (pixelFormat == PixelFormat::RGBA_F) {
internalFormat = GL_RGBA32F;
format = GL_RGBA;
} else
throw RendererException("Unsupported texture format in OpenGlRenderer::uploadTextureImage");
2024-03-19 14:53:34 +00:00
}
2024-03-19 14:53:34 +00:00
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat.value(format), size[0], size[1], 0, format, type, data);
2023-06-20 04:33:09 +00:00
}
void OpenGlRenderer::flushImmediatePrimitives(Mat3F const& transformation) {
2023-06-20 04:33:09 +00:00
if (m_immediatePrimitives.empty())
return;
2023-06-29 18:34:10 +00:00
m_immediateRenderBuffer->set(m_immediatePrimitives);
m_immediatePrimitives.resize(0);
renderGlBuffer(*m_immediateRenderBuffer, transformation);
2023-06-20 04:33:09 +00:00
}
auto OpenGlRenderer::createGlTexture(ImageView const& image, TextureAddressing addressing, TextureFiltering filtering)
2024-03-19 14:53:34 +00:00
->RefPtr<GlLoneTexture> {
2023-06-20 04:33:09 +00:00
auto glLoneTexture = make_ref<GlLoneTexture>();
glLoneTexture->textureFiltering = filtering;
glLoneTexture->textureAddressing = addressing;
2024-03-19 14:53:34 +00:00
glLoneTexture->textureSize = image.size;
2023-06-20 04:33:09 +00:00
glGenTextures(1, &glLoneTexture->textureId);
if (glLoneTexture->textureId == 0)
throw RendererException("Could not generate texture in OpenGlRenderer::createGlTexture");
2023-06-20 04:33:09 +00:00
glBindTexture(GL_TEXTURE_2D, glLoneTexture->textureId);
if (addressing == TextureAddressing::Clamp) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
if (filtering == TextureFiltering::Nearest) {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
if (!image.empty())
2024-03-19 14:53:34 +00:00
uploadTextureImage(image.format, image.size, image.data);
2023-06-20 04:33:09 +00:00
return glLoneTexture;
}
auto OpenGlRenderer::createGlRenderBuffer() -> shared_ptr<GlRenderBuffer> {
2023-06-20 04:33:09 +00:00
auto glrb = make_shared<GlRenderBuffer>();
glrb->whiteTexture = m_whiteTexture;
glrb->useMultiTexturing = m_useMultiTexturing;
return glrb;
}
void OpenGlRenderer::renderGlBuffer(GlRenderBuffer const& renderBuffer, Mat3F const& transformation) {
2023-06-20 04:33:09 +00:00
for (auto const& vb : renderBuffer.vertexBuffers) {
glUniformMatrix3fv(m_vertexTransformUniform, 1, GL_TRUE, transformation.ptr());
for (size_t i = 0; i < vb.textures.size(); ++i) {
glUniform2f(m_textureSizeUniforms[i], vb.textures[i].size[0], vb.textures[i].size[1]);
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, vb.textures[i].texture);
}
2023-06-29 19:55:41 +00:00
for (auto const& p : m_currentEffect->textures) {
2023-06-20 04:33:09 +00:00
if (p.second.textureValue) {
glActiveTexture(GL_TEXTURE0 + p.second.textureUnit);
glBindTexture(GL_TEXTURE_2D, p.second.textureValue->textureId);
}
}
glBindBuffer(GL_ARRAY_BUFFER, vb.vertexBuffer);
glEnableVertexAttribArray(m_positionAttribute);
glEnableVertexAttribArray(m_texCoordAttribute);
glEnableVertexAttribArray(m_colorAttribute);
glEnableVertexAttribArray(m_dataAttribute);
2023-06-20 04:33:09 +00:00
glVertexAttribPointer(m_positionAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(GlRenderVertex), (GLvoid*)offsetof(GlRenderVertex, pos));
glVertexAttribPointer(m_texCoordAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(GlRenderVertex), (GLvoid*)offsetof(GlRenderVertex, uv));
2023-06-20 04:33:09 +00:00
glVertexAttribPointer(m_colorAttribute, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(GlRenderVertex), (GLvoid*)offsetof(GlRenderVertex, color));
glVertexAttribIPointer(m_dataAttribute, 1, GL_INT, sizeof(GlRenderVertex), (GLvoid*)offsetof(GlRenderVertex, pack));
2023-06-30 17:58:02 +00:00
2023-06-20 04:33:09 +00:00
glDrawArrays(GL_TRIANGLES, 0, vb.vertexCount);
}
}
//Assumes the passed effect program is currently in use.
void OpenGlRenderer::setupGlUniforms(Effect& effect) {
m_positionAttribute = effect.getAttribute("vertexPosition");
m_colorAttribute = effect.getAttribute("vertexColor");
m_texCoordAttribute = effect.getAttribute("vertexTextureCoordinate");
m_dataAttribute = effect.getAttribute("vertexData");
2023-06-29 19:55:41 +00:00
m_textureUniforms.clear();
m_textureSizeUniforms.clear();
for (size_t i = 0; i < MultiTextureCount; ++i) {
m_textureUniforms.append(effect.getUniform(strf("texture{}", i).c_str()));
m_textureSizeUniforms.append(effect.getUniform(strf("textureSize{}", i).c_str()));
2023-06-29 19:55:41 +00:00
}
m_screenSizeUniform = effect.getUniform("screenSize");
m_vertexTransformUniform = effect.getUniform("vertexTransform");
2023-06-29 19:55:41 +00:00
for (size_t i = 0; i < MultiTextureCount; ++i)
glUniform1i(m_textureUniforms[i], i);
glUniform2f(m_screenSizeUniform, m_screenSize[0], m_screenSize[1]);
}
RefPtr<OpenGlRenderer::GlFrameBuffer> OpenGlRenderer::getGlFrameBuffer(String const& id) {
if (auto ptr = m_frameBuffers.ptr(id))
return *ptr;
else
throw RendererException::format("Frame buffer '{}' does not exist", id);
}
void OpenGlRenderer::blitGlFrameBuffer(RefPtr<GlFrameBuffer> const& frameBuffer) {
if (frameBuffer->blitted)
return;
auto& size = m_screenSize;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, frameBuffer->id);
glBlitFramebuffer(
0, 0, size[0], size[1],
0, 0, size[0], size[1],
GL_COLOR_BUFFER_BIT, GL_NEAREST
);
frameBuffer->blitted = true;
}
void OpenGlRenderer::switchGlFrameBuffer(RefPtr<GlFrameBuffer> const& frameBuffer) {
if (m_currentFrameBuffer == frameBuffer)
return;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer->id);
m_currentFrameBuffer = frameBuffer;
}
GLuint OpenGlRenderer::Effect::getAttribute(String const& name) {
auto find = attributes.find(name);
if (find == attributes.end()) {
GLuint attrib = glGetAttribLocation(program, name.utf8Ptr());
attributes[name] = attrib;
return attrib;
}
return find->second;
}
GLuint OpenGlRenderer::Effect::getUniform(String const& name) {
auto find = uniforms.find(name);
if (find == uniforms.end()) {
GLuint uniform = glGetUniformLocation(program, name.utf8Ptr());
uniforms[name] = uniform;
return uniform;
}
return find->second;
}
2023-06-20 04:33:09 +00:00
}