Skip to content

Commit

Permalink
Optimize gpu color buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Dec 9, 2024
1 parent f940707 commit 730fcdc
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 42 deletions.
2 changes: 2 additions & 0 deletions lib/renderer/include/renderer/context/include_opengl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ constexpr GLuint GL_TEXTURE_2D_ARRAY = 0x8C1A;
constexpr GLuint GL_STATIC_DRAW = 0x88E4;
constexpr GLuint GL_DYNAMIC_DRAW = 0x88E8;
constexpr GLuint GL_DYNAMIC_STORAGE_BIT = 0x0100;
constexpr GLuint GL_MAP_READ_BIT = 0x0001;
constexpr GLuint GL_MAP_WRITE_BIT = 0x0002;
constexpr GLuint GL_STREAM_DRAW = 0x88E0;
constexpr GLuint GL_PATCH_VERTICES = 0x8E72;
constexpr GLuint GL_TRIANGLES = 0x0004;
Expand Down
2 changes: 1 addition & 1 deletion lib/renderer/include/renderer/context/opengl_45.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ namespace VTX::Renderer::Context

void _createOuputs( const Pass * const, std::set<GLenum> & p_drawBuffers, const Key, Keys & );

void _createProgram( const Program &, const Key, Keys & );
const GL::Program * const _createProgram( const Program &, const Key, Keys & );

std::optional<std::pair<const Output * const, const Key>> _getInputTextureKey(
const Links &,
Expand Down
3 changes: 2 additions & 1 deletion lib/renderer/include/renderer/descriptors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ namespace VTX::Renderer

struct ComputePass
{
Program program;
std::string name;
Program program;

std::vector<BufferData> data;
std::variant<Vec3i, size_t> size;
Expand Down
5 changes: 2 additions & 3 deletions lib/renderer/shaders/layout_uniforms_color.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
layout( std140, binding = 14 ) readonly buffer UniformsColor
{
vec4 uniformsColor[];
layout( std140, binding = 14 ) uniform UniformsColor {
vec4 uniformsColor[ 256 ];
};
35 changes: 10 additions & 25 deletions lib/renderer/src/renderer/context/opengl_45.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ namespace VTX::Renderer::Context
_purgeResources( keys );
}

void OpenGL45::_createProgram( const Program & p_descProgram, const Key p_key, Keys & p_keys )
const GL::Program * const OpenGL45::_createProgram( const Program & p_descProgram, const Key p_key, Keys & p_keys )
{
const Key keyProgram = p_key + p_descProgram.name;
p_keys.push_back( keyProgram );
Expand All @@ -451,6 +451,8 @@ namespace VTX::Renderer::Context
// Create small and immutable buffer.
_createBufferData( BufferData { "", 0, p_descProgram.data, 0, nullptr, false, true }, keyProgram, p_keys );
}

return _programs[ keyProgram ];
}

void OpenGL45::resize( const RenderQueue & p_renderQueue, const size_t p_width, const size_t p_height )
Expand Down Expand Up @@ -559,39 +561,23 @@ namespace VTX::Renderer::Context

void OpenGL45::compute( const ComputePass & p_pass )
{
/*
// return;
// TODO: Create program and data (refacto build).
const Program & descProgram = p_pass.program;

std::string definesToInject = "#define LOCAL_SIZE_X " + std::to_string( LOCAL_SIZE_X ) + "\n"
+ "#define LOCAL_SIZE_Y " + std::to_string( LOCAL_SIZE_Y ) + "\n"
+ "#define LOCAL_SIZE_Z " + std::to_string( LOCAL_SIZE_Z ) + "\n";

const GL::Program * const program
= _programManager->createProgram( descProgram.name, descProgram.shaders, definesToInject );
Keys keys = {};
const GL::Program * const program = _createProgram( descProgram, p_pass.name, keys );
//= _programManager->createProgram( descProgram.name, descProgram.shaders, definesToInject );

// Create and bind p_buffers.
for ( const BufferData & bufferData : p_pass.data )
{
// Create buffer.
const Key keyBuffer = _getKey( bufferData );
// buffers.push_back( keyBuffer );
if ( not _buffers.contains( keyBuffer ) )
{
_buffers.emplace( keyBuffer, std::make_unique<GL::Buffer>() );
_createBufferData(
_buffers[ keyBuffer ].get(),
bufferData.values,
bufferData.size,
bufferData.data,
bufferData.isSizeFixed
);
}
_buffers[ keyBuffer ].get()->bind( GL_SHADER_STORAGE_BUFFER, bufferData.binding );
auto * const buffer = _createBufferData( bufferData, p_pass.name, keys );
buffer->bind( GL_SHADER_STORAGE_BUFFER, bufferData.binding );
}

program->use();
Expand Down Expand Up @@ -626,10 +612,9 @@ namespace VTX::Renderer::Context
// Unbind p_buffers.
for ( const BufferData & bufferData : p_pass.data )
{
const Key keyBuffer = bufferData.name;
const Key keyBuffer = p_pass.name + bufferData.name;
_buffers[ keyBuffer ].get()->unbind();
}
*/
}

void OpenGL45::_createInputs(
Expand Down Expand Up @@ -931,7 +916,7 @@ namespace VTX::Renderer::Context
GLsizei( maxSize ),
p_bufferData.data,
p_bufferData.isSizeFixed,
p_bufferData.isSizeFixed ? GL_DYNAMIC_STORAGE_BIT : GL_STATIC_DRAW
p_bufferData.isSizeFixed ? GL_DYNAMIC_STORAGE_BIT | GL_MAP_READ_BIT : GL_STATIC_DRAW
);
}

Expand Down
28 changes: 16 additions & 12 deletions lib/renderer/src/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ namespace VTX::Renderer
true }
);

_renderGraph->addGlobalData(
{ "ColorLayout", 14, { { "Colors", E_TYPE::COLOR4, BufferValue<Util::Color::Rgba> {} } }, 0, nullptr, true }
);
_renderGraph->addGlobalData( { "ColorLayout",
14,
{ { "Colors", E_TYPE::COLOR4, BufferValue<Util::Color::Rgba> {} } },
4096,
nullptr,
false,
true } );

_renderGraph->addGlobalData( { "Models",
13,
Expand Down Expand Up @@ -248,7 +252,6 @@ namespace VTX::Renderer
onReady();
}

return;
///////////////////// COMPUTE
uint size = 10000;

Expand All @@ -257,19 +260,20 @@ namespace VTX::Renderer
BufferData bufferWriteOnly { "WriteOnly", 2, {}, size * sizeof( Vec4f ), nullptr, true, true };
BufferData bufferReadWrite { "ReadWrite", 3, {}, size * sizeof( Vec4f ), nullptr, true, true };

auto computePass = ComputePass {
Program { "ComputeDebug",
std::vector<FilePath> { "compute/debug.comp" },
BufferDataValues { { { "Intensity", E_TYPE::UINT, BufferValue<uint> { size } } } } },
{ bufferReadOnly, bufferWriteOnly, bufferReadWrite },
size
};
auto computePass
= ComputePass { "ComputeDebug",
Program { "ComputeDebug",
std::vector<FilePath> { "compute/debug.comp" },
BufferDataValues { { { "Size", E_TYPE::UINT, BufferValue<uint> { size } } } } },
{ bufferReadOnly, bufferWriteOnly, bufferReadWrite },
size };

_context->compute( computePass );

// Get write only buffer data.
std::vector<Vec4f> writeData( size );
_context->get<Vec4f>( writeData, "WriteOnly" );
_context->get<Vec4f>( writeData, "ComputeDebugWriteOnly" );
VTX_DEBUG( "Compute OK" );
}

void Renderer::clean()
Expand Down

0 comments on commit 730fcdc

Please sign in to comment.