Skip to content

Commit

Permalink
Add global shader params UBOs that will be shared between all programs
Browse files Browse the repository at this point in the history
  • Loading branch information
kavika13 committed Mar 18, 2024
1 parent 1e51cce commit 6b71e47
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 14 deletions.
19 changes: 5 additions & 14 deletions Data/GLSL/envobject.frag
Expand Up @@ -19,19 +19,16 @@
#og_version_major 1
#og_version_minor 5

/*#if defined(WATER)
#define NO_DECALS
#endif
*/
#define FIRE_DECAL_ENABLED
//#define RAINY

uniform float time;
uniform vec3 cam_pos;

//#define VOLCANO
//#define MISTY
//#define WATER_HORIZON
#include "params_global.glsl"
#include "params_instance.glsl"
#include "object_frag150.glsl"
#include "object_shared150.glsl"
#include "ambient_tet_mesh.glsl"

#if defined(SWAMP)
const float kBeachLevel = 17.1;
Expand Down Expand Up @@ -65,12 +62,6 @@ uniform vec3 cam_pos;
const float kBeachLevel = 75.4;
#endif

//#define VOLUME_SHADOWS

#include "object_frag150.glsl"
#include "object_shared150.glsl"
#include "ambient_tet_mesh.glsl"

#if defined(PARTICLE)
uniform sampler2D tex0; // Colormap

Expand Down
2 changes: 2 additions & 0 deletions Data/GLSL/envobject.vert
Expand Up @@ -19,6 +19,8 @@
#og_version_major 1
#og_version_minor 5

#include "params_global.glsl"
#include "params_instance.glsl"
#include "lighting150.glsl"

in vec3 vertex_attrib;
Expand Down
28 changes: 28 additions & 0 deletions Data/GLSL/params_global.glsl
@@ -0,0 +1,28 @@
// Copyright 2022 Wolfire Games LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-----------------------------------------------------------------------------
#version 150
#extension GL_ARB_shading_language_420pack : enable

#og_version_major 1
#og_version_minor 5

layout(std140) uniform GlobalVec4Params {
vec4 global_vec4_params[1024];
};

layout(std140) uniform GlobalIVec4Params {
ivec4 global_ivec4_params[1024];
};
28 changes: 28 additions & 0 deletions Data/GLSL/params_instance.glsl
@@ -0,0 +1,28 @@
// Copyright 2022 Wolfire Games LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-----------------------------------------------------------------------------
#version 150
#extension GL_ARB_shading_language_420pack : enable

#og_version_major 1
#og_version_minor 5

layout(std140) uniform InstanceVec4Params {
vec4 instance_vec4_params[1024];
};

layout(std140) uniform InstanceIVec4Params {
ivec4 instance_ivec4_params[1024];
};
31 changes: 31 additions & 0 deletions Source/Main/scenegraph.cpp
Expand Up @@ -580,6 +580,36 @@ static void UpdateShaderSuffix(SceneGraph* scenegraph, Object::DrawType object_d
FormatString(global_shader_suffix_storage, kGlobalShaderSuffixLen, "%s", shader_str[0]);
}

float g_global_shader_params_floats_data[4 * 1024] = {};
int32_t g_global_shader_params_ints_data[4 * 1024] = {};
bool g_global_shader_params_are_floats_dirty = true;
bool g_global_shader_params_are_ints_dirty = true;

static void UpdateGlobalUniformBlocks() {
static UniformRingBuffer global_float_buffer;
static UniformRingBuffer global_int_buffer;

if (global_float_buffer.gl_id == -1) {
global_float_buffer.Create(64 * 1024);
}

if (global_int_buffer.gl_id == -1) {
global_int_buffer.Create(64 * 1024);
}

if (g_global_shader_params_are_floats_dirty) {
global_float_buffer.Fill(sizeof(g_global_shader_params_floats_data), &g_global_shader_params_floats_data[0]);
glBindBufferRange(GL_UNIFORM_BUFFER, UBO_PARAMS_GLOBAL_VEC4, global_float_buffer.gl_id, global_float_buffer.offset, global_float_buffer.next_offset - global_float_buffer.offset);
g_global_shader_params_are_floats_dirty = false;
}

if (g_global_shader_params_are_ints_dirty) {
global_int_buffer.Fill(sizeof(g_global_shader_params_ints_data), &g_global_shader_params_ints_data[0]);
glBindBufferRange(GL_UNIFORM_BUFFER, UBO_PARAMS_GLOBAL_IVEC4, global_int_buffer.gl_id, global_int_buffer.offset, global_int_buffer.next_offset - global_int_buffer.offset);
g_global_shader_params_are_ints_dirty = false;
}
}

extern bool last_ofr_is_valid;

// Draw all objects
Expand All @@ -592,6 +622,7 @@ void SceneGraph::Draw(SceneGraph::SceneDrawType scene_draw_type) {
Camera* camera = ActiveCameras::Get();

UpdateShaderSuffix(this, Object::kFullDraw);
UpdateGlobalUniformBlocks();

graphics->setDepthFunc(GL_LEQUAL);
if (nav_mesh_ && (nav_mesh_renderer_.IsNavMeshVisible() || nav_mesh_renderer_.IsCollisionMeshVisible())) {
Expand Down
11 changes: 11 additions & 0 deletions Source/Objects/envobject.cpp
Expand Up @@ -614,6 +614,17 @@ void EnvObject::DrawInstances(EnvObject** instance_array, int num_instances, con
shaders->SetUniformVec4("primary_light_color", vec4(scenegraph_->primary_light.color,
scenegraph_->primary_light.intensity));
}

int global_params_float_block_index = shaders->GetUBOBindIndex(the_shader, "GlobalVec4Params");
if ((unsigned)global_params_float_block_index != GL_INVALID_INDEX) {
glUniformBlockBinding(shaders->programs[the_shader].gl_program, global_params_float_block_index, UBO_PARAMS_GLOBAL_VEC4);
}

int global_params_int_block_index = shaders->GetUBOBindIndex(the_shader, "GlobalIVec4Params");
if ((unsigned)global_params_int_block_index != GL_INVALID_INDEX) {
glUniformBlockBinding(shaders->programs[the_shader].gl_program, global_params_int_block_index, UBO_PARAMS_GLOBAL_IVEC4);
}

PROFILER_LEAVE(g_profiler_ctx); // Misc uniforms

PROFILER_LEAVE(g_profiler_ctx); // Shader
Expand Down

0 comments on commit 6b71e47

Please sign in to comment.