Skip to content

Commit

Permalink
Merge branch 'master' into playthrough_bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pskelton authored Jul 26, 2024
2 parents e0844a8 + 2354330 commit 19c7349
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 331 deletions.
53 changes: 18 additions & 35 deletions src/Engine/Graphics/Renderer/NuklearOverlayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Library/Logger/Logger.h"
#include "Engine/Graphics/Image.h"
#include "Utility/DataPath.h"

struct nk_vertex {
float position[2]{};
Expand All @@ -22,11 +23,6 @@ struct nk_device {
struct nk_draw_null_texture null;
struct nk_font_atlas atlas;
uint32_t vbo{}, vao{}, ebo{};
int32_t attrib_pos{};
int32_t attrib_uv{};
int32_t attrib_col{};
int32_t uniform_tex{};
int32_t uniform_proj{};
};

struct nk_state {
Expand Down Expand Up @@ -72,18 +68,11 @@ void NuklearOverlayRenderer::_initialize(nk_context *context) {
}

bool NuklearOverlayRenderer::_createDevice() {
_shader.build("nuklear", "glnuklear", _useOGLES);
if (!_shader.isValid()) {
logger->warning("Nuklear shader failed to compile!");
reloadShaders(_useOGLES);
if (!_shader.isValid())
return false;
}

nk_buffer_init_default(&_state->dev.cmds);
_state->dev.uniform_tex = _shader.uniformLocation("Texture");
_state->dev.uniform_proj = _shader.uniformLocation("ProjMtx");
_state->dev.attrib_pos = _shader.attribLocation("Position");
_state->dev.attrib_uv = _shader.attribLocation("TexCoord");
_state->dev.attrib_col = _shader.attribLocation("Color");
{
GLsizei vs = sizeof(struct nk_vertex);
size_t vp = offsetof(struct nk_vertex, position);
Expand All @@ -98,13 +87,13 @@ bool NuklearOverlayRenderer::_createDevice() {
glBindBuffer(GL_ARRAY_BUFFER, _state->dev.vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _state->dev.ebo);

glEnableVertexAttribArray((GLuint)_state->dev.attrib_pos);
glEnableVertexAttribArray((GLuint)_state->dev.attrib_uv);
glEnableVertexAttribArray((GLuint)_state->dev.attrib_col);
glEnableVertexAttribArray((GLuint)_attribPos);
glEnableVertexAttribArray((GLuint)_attribUv);
glEnableVertexAttribArray((GLuint)_attribCol);

glVertexAttribPointer((GLuint)_state->dev.attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void *)vp);
glVertexAttribPointer((GLuint)_state->dev.attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void *)vt);
glVertexAttribPointer((GLuint)_state->dev.attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void *)vc);
glVertexAttribPointer((GLuint)_attribPos, 2, GL_FLOAT, GL_FALSE, vs, (void *)vp);
glVertexAttribPointer((GLuint)_attribUv, 2, GL_FLOAT, GL_FALSE, vs, (void *)vt);
glVertexAttribPointer((GLuint)_attribCol, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void *)vc);
}

glBindTexture(GL_TEXTURE_2D, 0);
Expand Down Expand Up @@ -165,7 +154,7 @@ void NuklearOverlayRenderer::_cleanup() {

nk_font_atlas_clear(&_state->dev.atlas);

_shader.reset();
_shader.release();
glDeleteBuffers(1, &_state->dev.vbo);
glDeleteBuffers(1, &_state->dev.ebo);
glDeleteVertexArrays(1, &_state->dev.vao);
Expand Down Expand Up @@ -215,8 +204,8 @@ void NuklearOverlayRenderer::render(nk_context *context, const Sizei &outputPres

/* setup program */
_shader.use();
glUniform1i(_state->dev.uniform_tex, 0);
glUniformMatrix4fv(_state->dev.uniform_proj, 1, GL_FALSE, &ortho[0][0]);
glUniform1i(_uniformTex, 0);
glUniformMatrix4fv(_uniformProj, 1, GL_FALSE, &ortho[0][0]);
{
/* convert from command queue into draw list and draw to screen */
const struct nk_draw_command *cmd;
Expand Down Expand Up @@ -293,17 +282,11 @@ void NuklearOverlayRenderer::render(nk_context *context, const Sizei &outputPres
}

void NuklearOverlayRenderer::reloadShaders(bool useOGLES) {
if (_shader.isValid()) {
std::string name = "Nuklear";
std::string message = "shader failed to reload!\nPlease consult the log and issue a bug report!";
if (!_shader.reload(name, useOGLES)) {
logger->warning("{} {}", name, message);
} else {
_state->dev.uniform_tex = _shader.uniformLocation("Texture");
_state->dev.uniform_proj = _shader.uniformLocation("ProjMtx");
_state->dev.attrib_pos = _shader.attribLocation("Position");
_state->dev.attrib_uv = _shader.attribLocation("TexCoord");
_state->dev.attrib_col = _shader.attribLocation("Color");
}
if (_shader.load(makeDataPath("shaders", "glnuklear.vert"), makeDataPath("shaders", "glnuklear.frag"), useOGLES)) {
_uniformTex = _shader.uniformLocation("Texture");
_uniformProj = _shader.uniformLocation("ProjMtx");
_attribPos = _shader.attribLocation("Position");
_attribUv = _shader.attribLocation("TexCoord");
_attribCol = _shader.attribLocation("Color");
}
}
5 changes: 5 additions & 0 deletions src/Engine/Graphics/Renderer/NuklearOverlayRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ class NuklearOverlayRenderer {
std::unique_ptr<nk_state> _state;
bool _useOGLES = false;
OpenGLShader _shader;
int32_t _attribPos = 0;
int32_t _attribUv = 0;
int32_t _attribCol = 0;
int32_t _uniformTex = 0;
int32_t _uniformProj = 0;
nk_tex_font *_defaultFont = nullptr;
};
2 changes: 1 addition & 1 deletion src/Engine/Graphics/Renderer/NullRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void NullRenderer::ReleaseBSP() {}

void NullRenderer::DrawTwodVerts() {}

void NullRenderer::ReloadShaders() {}
bool NullRenderer::ReloadShaders() { return true; }

void NullRenderer::DoRenderBillboards_D3D() {}

Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Graphics/Renderer/NullRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class NullRenderer : public BaseRenderer {

virtual void DrawTwodVerts() override;

virtual void ReloadShaders() override;
virtual bool ReloadShaders() override;

virtual void DoRenderBillboards_D3D() override;

Expand Down
Loading

0 comments on commit 19c7349

Please sign in to comment.