16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-17 23:39:18 +02:00

ubo (ugly for now)

This commit is contained in:
milek7
2018-06-26 01:05:46 +02:00
parent a9713a6ee4
commit 1c26096c5c
8 changed files with 145 additions and 54 deletions

View File

@@ -83,6 +83,7 @@ set(SOURCES
"gl/shader.cpp"
"gl/shader_mvp.cpp"
"gl/vao.cpp"
"gl/ubo.cpp"
)
set (PREFIX "")

View File

@@ -3,36 +3,6 @@
void gl::program_mvp::init()
{
mv_uniform = glGetUniformLocation(*this, "modelview");
mvn_uniform = glGetUniformLocation(*this, "modelviewnormal");
p_uniform = glGetUniformLocation(*this, "projection");
}
void gl::program_mvp::set_mv(const glm::mat4 &m)
{
if (last_mv == m)
return;
last_mv = m;
if (mvn_uniform != -1)
{
glm::mat3 mvn = glm::mat3(glm::transpose(glm::inverse(m)));
glUniformMatrix3fv(mvn_uniform, 1, GL_FALSE, glm::value_ptr(mvn));
}
glUniformMatrix4fv(mv_uniform, 1, GL_FALSE, glm::value_ptr(m));
}
void gl::program_mvp::set_p(const glm::mat4 &m)
{
if (last_p == m)
return;
last_p = m;
glUniformMatrix4fv(p_uniform, 1, GL_FALSE, glm::value_ptr(m));
}
void gl::program_mvp::copy_gl_mvp()
{
set_mv(OpenGLMatrices.data(GL_MODELVIEW));
set_p(OpenGLMatrices.data(GL_PROJECTION));
glUniformBlockBinding(*this, 0, glGetUniformBlockIndex(*this, "scene_ubo"));
glUniformBlockBinding(*this, 1, glGetUniformBlockIndex(*this, "model_ubo"));
}

View File

@@ -6,20 +6,9 @@ namespace gl
{
class program_mvp : public program
{
GLuint mv_uniform;
GLint mvn_uniform;
GLuint p_uniform;
glm::mat4 last_mv;
glm::mat4 last_p;
public:
using program::program;
void set_mv(const glm::mat4 &);
void set_p(const glm::mat4 &);
void copy_gl_mvp();
virtual void init() override;
};
}

26
gl/ubo.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "stdafx.h"
#include "ubo.h"
gl::ubo::ubo(int size, int index)
{
glGenBuffers(1, *this);
bind();
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, index, *this);
}
gl::ubo::~ubo()
{
glDeleteBuffers(1, *this);
}
void gl::ubo::bind(GLuint i)
{
glBindBuffer(GL_UNIFORM_BUFFER, i);
}
void gl::ubo::update(void *data, int offset, int size)
{
bind();
glBufferSubData(GL_UNIFORM_BUFFER, offset, size, data);
}

48
gl/ubo.h Normal file
View File

@@ -0,0 +1,48 @@
#include "object.h"
#include "bindable.h"
#define UBS_PAD(x) uint64_t : x * 4; uint64_t : x * 4;
#define DEFINE_UBS(x, y) _Pragma("pack(push, 1)"); struct x y; _Pragma("pack(pop)")
namespace gl
{
static GLuint next_binding_point;
class ubo : public object, public bindable<ubo>
{
public:
ubo(int size, int index);
~ubo();
using bindable::bind;
static void bind(GLuint i);
void update(void *data, int offset, int size);
};
_Pragma("pack(push, 1)")
struct scene_ubs
{
glm::mat4 projection;
void set(const glm::mat4 &m)
{
projection = m;
}
};
struct model_ubs
{
glm::mat4 modelview;
glm::mat4 modelviewnormal;
void set(const glm::mat4 &m)
{
modelview = m;
modelviewnormal = glm::mat3(glm::transpose(glm::inverse(m)));
}
};
_Pragma("pack(pop)")
}

View File

@@ -162,6 +162,9 @@ opengl_renderer::Init( GLFWwindow *Window ) {
shader = std::make_unique<gl::program_mvp>(std::vector<std::reference_wrapper<const gl::shader>>({vert, frag}));
shader->init();
scene_ubo = std::make_unique<gl::ubo>(sizeof(gl::scene_ubs), 0);
model_ubo = std::make_unique<gl::ubo>(sizeof(gl::model_ubs), 1);
return true;
}
@@ -574,6 +577,12 @@ opengl_renderer::Render( world_environment *Environment ) {
::glDepthMask( GL_FALSE );
::glPushMatrix();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
// skydome
Environment->m_skydome.Render();
// skydome uses a custom vbo which could potentially confuse the main geometry system. hardly elegant but, eh
@@ -928,7 +937,12 @@ opengl_renderer::Render( scene::shape_node const &Shape, bool const Ignorerange
}
}
// render
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( data.geometry );
// debug data
++m_debugstats.shapes;
@@ -1253,7 +1267,12 @@ opengl_renderer::Render( TSubModel *Submodel ) {
}
// main draw call
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( Submodel->m_geometry );
@@ -1322,7 +1341,12 @@ opengl_renderer::Render( TSubModel *Submodel ) {
switch_units( m_unitstate.diffuse, false, false );
// main draw call
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( Submodel->m_geometry );
// post-draw reset
@@ -1353,7 +1377,12 @@ opengl_renderer::Render( TSubModel *Submodel ) {
Bind_Material( null_handle );
// main draw call
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( Submodel->m_geometry, gfx::color_streams );
}
break;
@@ -1393,7 +1422,11 @@ opengl_renderer::Render( TTrack *Track ) {
++m_debugstats.paths;
++m_debugstats.drawcalls;
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
switch( m_renderpass.draw_mode ) {
case rendermode::color:
@@ -1440,7 +1473,11 @@ opengl_renderer::Render( scene::basic_cell::path_sequence::const_iterator First,
}
}
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
// first pass, material 1
for( auto first { First }; first != Last; ++first ) {
@@ -1725,7 +1762,11 @@ opengl_renderer::Render_Alpha( TTraction *Traction ) {
1.f, 1.5f ) );
// McZapkie-261102: kolor zalezy od materialu i zasniedzenia
// render
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( Traction->m_geometry );
// debug data
++m_debugstats.traction;
@@ -1763,7 +1804,11 @@ opengl_renderer::Render_Alpha( scene::lines_node const &Lines ) {
0.5f * linealpha + data.line_width * data.area.radius / 1000.f,
1.f, 8.f ) );
// render
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( data.geometry );
++m_debugstats.lines;
++m_debugstats.drawcalls;
@@ -1941,7 +1986,11 @@ opengl_renderer::Render_Alpha( TSubModel *Submodel ) {
}
// main draw call
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( Submodel->m_geometry );
#ifdef EU07_USE_OPTIMIZED_NORMALIZATION
@@ -2006,7 +2055,11 @@ opengl_renderer::Render_Alpha( TSubModel *Submodel ) {
switch_units( unitstate.diffuse, false, false );
// main draw call
shader->copy_gl_mvp();
scene_ubs.set(OpenGLMatrices.data(GL_PROJECTION));
model_ubs.set(OpenGLMatrices.data(GL_MODELVIEW));
scene_ubo->update(&scene_ubs, 0, sizeof(scene_ubs));
model_ubo->update(&model_ubs, 0, sizeof(model_ubs));
m_geometry.draw( m_billboardgeometry );
::glPopMatrix();

View File

@@ -20,6 +20,7 @@ http://mozilla.org/MPL/2.0/.
#include "scene.h"
#include "light.h"
#include "gl/shader_mvp.h"
#include "gl/ubo.h"
#define EU07_USE_PICKING_FRAMEBUFFER
//#define EU07_USE_DEBUG_SHADOWMAP
@@ -398,6 +399,10 @@ private:
GLuint64 m_gllasttime = 0;
std::unique_ptr<gl::program_mvp> shader;
std::unique_ptr<gl::ubo> scene_ubo;
std::unique_ptr<gl::ubo> model_ubo;
gl::scene_ubs scene_ubs;
gl::model_ubs model_ubs;
};
extern opengl_renderer GfxRenderer;

View File

@@ -148,7 +148,6 @@ void CSkyDome::Render() {
}
m_shader->bind();
m_shader->copy_gl_mvp();
m_vao->bind();
glDrawElements( GL_TRIANGLES, static_cast<GLsizei>( m_indices.size() ), GL_UNSIGNED_SHORT, reinterpret_cast<void const*>( 0 ) );