Skip to content

Commit a6553e1

Browse files
author
drops
committed
Combine OpenGL uniforms into batches
1 parent 112dbe3 commit a6553e1

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

src/Layers/xrRender/R_Backend.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#endif
2424
#ifdef USE_OGL
2525
#include <glm/glm.hpp>
26+
#include "gl_backend_uniforms.h"
2627
#endif
2728

2829
#include "FVF.h"
@@ -63,6 +64,9 @@ struct R_statistics
6364
#pragma warning(push)
6465
#pragma warning(disable:4324)
6566
class ECORE_API CBackend
67+
#ifdef USE_OGL
68+
: public CBackendUniforms
69+
#endif
6670
{
6771
public:
6872
enum
@@ -103,13 +107,6 @@ class ECORE_API CBackend
103107
GLuint pFB;
104108
GLuint pRT[4];
105109
GLuint pZB;
106-
107-
template<typename TYPE>
108-
void _set_uniforms(GLuint program, GLint location, xr_vector<TYPE>& uniforms);
109-
110-
public:
111-
void set_uniforms(GLuint program, GLint location, xr_vector<glm::vec4>& uniforms);
112-
private:
113110
#else
114111
# error No graphics API selected or enabled!
115112
#endif

src/Layers/xrRender/R_Backend_Runtime.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ void CBackend::OnDeviceCreate()
470470

471471
// invalidate caching
472472
Invalidate();
473+
474+
#ifdef USE_OGL
475+
initializeUniforms();
476+
#endif
473477
}
474478

475479
void CBackend::OnDeviceDestroy()

src/Layers/xrRenderPC_GL/gl_backend_uniforms.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <glm/glm.hpp>
55

6-
76
namespace xray::render::RENDER_NAMESPACE
87
{
98
void CBackendUniforms::uniformBufferObjectGenerate(UniformBufferObject& info)
@@ -21,9 +20,9 @@ void CBackendUniforms::uniformBufferObjectRegisterWithProgram(Program& program,
2120
{
2221
// Only single bit is 1
2322
VERIFY(blockBinding != 0 && (blockBinding & (blockBinding - 1)) == 0);
24-
//VERIFY(program.blockBindingSlots & 1 << blockBinding == 0);
23+
//VERIFY(program.bindingSlots & 1 << blockBinding == 0);
2524

26-
if (program.blockBindingSlots & 1 << blockBinding)
25+
if (program.bindingSlots & 1 << blockBinding)
2726
{
2827
// UBO already registered with this program
2928
return;
@@ -34,15 +33,15 @@ void CBackendUniforms::uniformBufferObjectRegisterWithProgram(Program& program,
3433
CHK_GL(glUniformBlockBinding(program.id, blockIndex, blockBinding));
3534
CHK_GL(glBindBufferRange(GL_UNIFORM_BUFFER, blockBinding, ubo.id, 0,ubo.size));
3635

37-
program.blockBindingSlots |= 1 << blockBinding;
36+
program.bindingSlots |= 1 << blockBinding;
3837

3938
CHK_GL(glBindBuffer(GL_UNIFORM_BUFFER, ubo.id));
4039
}
4140

42-
void CBackendUniforms::uniformBufferObjectPushToDevice(const UniformBufferObject& ubo)
41+
void CBackendUniforms::uniformBufferObjectPushToDevice(const UniformBufferObject& ubo, GLsizeiptr size, void* data)
4342
{
4443
CHK_GL(glBindBuffer(GL_UNIFORM_BUFFER, ubo.id));
45-
CHK_GL(glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo.size, ubo.data));
44+
CHK_GL(glBufferSubData(GL_UNIFORM_BUFFER, 0, size, data));
4645
//CHK_GL(glBindBuffer(GL_UNIFORM_BUFFER, 0));
4746
}
4847

@@ -59,4 +58,10 @@ void CBackendUniforms::_set_uniforms(GLuint program, GLint location, xr_vector<T
5958
CHK_GL(glProgramUniform4fv(program, location, uniforms.size(), reinterpret_cast<GLfloat*>(uniforms.data())));
6059
}
6160
}
61+
62+
void CBackendUniforms::initializeUniforms()
63+
{
64+
CHK_GL(glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &maxUniformVertexSize));
65+
CHK_GL(glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &maxUniformBlockSize));
66+
}
6267
}

src/Layers/xrRenderPC_GL/gl_backend_uniforms.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "xrCommon/xr_vector.h"
44
#include <glm/glm.hpp>
55
#include <glad/gl.h>
6+
#include <string_view>
7+
8+
#include "gl_program.h"
69

710
namespace xray::render::RENDER_NAMESPACE
811
{
@@ -16,26 +19,35 @@ struct UniformBufferObject
1619
void* data = nullptr;
1720
};
1821

19-
struct Program
20-
{
21-
GLuint id = GL_NONE;
22-
uint32_t blockBindingSlots = 0;
23-
};
2422

2523
class CBackendUniforms
2624
{
25+
protected:
26+
void initializeUniforms();
2727
public:
28+
2829
void set_uniforms(GLuint program, GLint location, xr_vector<glm::vec4>& uniforms);
2930

3031
static void uniformBufferObjectGenerate(UniformBufferObject& ubo);
3132
static void uniformBufferObjectRegisterWithProgram(Program& program, std::string_view location, uint32_t blockBinding, const UniformBufferObject& ubo);
3233

33-
static void uniformBufferObjectPushToDevice(const UniformBufferObject& ubo);
34+
static void uniformBufferObjectPushToDevice(const UniformBufferObject& ubo)
35+
{
36+
uniformBufferObjectPushToDevice(ubo, ubo.size, ubo.data);
37+
}
38+
static void uniformBufferObjectPushToDevice(const UniformBufferObject& ubo, GLsizeiptr size, void* data);
39+
40+
[[nodiscard]] uint calculateMaxUniformVertexSize(size_t sizeOfElement, uint max) const
41+
{
42+
return std::min(static_cast<uint>(maxUniformVertexSize / std::max(sizeOfElement, sizeof(float))), max);
43+
}
3444

3545
private:
46+
GLint maxUniformVertexSize = GL_NONE;
47+
GLint maxUniformBlockSize = GL_NONE;
48+
3649
template<typename TYPE>
3750
void _set_uniforms(GLuint program, GLint location, xr_vector<TYPE>& uniforms);
38-
3951
};
4052

4153
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include "../../../sdk/include/glad/gl.h"
3+
#include "xrCommon/xr_string.h"
4+
5+
struct Program
6+
{
7+
GLuint id = GL_NONE;
8+
uint32_t bindingSlots = 0;
9+
10+
#ifdef DEBUG
11+
xr_string name;
12+
#endif
13+
};

0 commit comments

Comments
 (0)