Skip to content

Commit

Permalink
Graphic context abstraction wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Dec 29, 2024
1 parent b4a9a60 commit f7b8ba0
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 29 deletions.
195 changes: 190 additions & 5 deletions lib/renderer/include/private/renderer/context/concept_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace VTX::Renderer::Context
{
using Key = std::string;
using Keys = std::vector<Key>;

/**
* @brief Shared attributes for all render contexts.
*/
Expand All @@ -19,14 +22,11 @@ namespace VTX::Renderer::Context
FilePath shaderPath;
};

using Key = std::string;
using Keys = std::vector<Key>;

/**
* @brief The context is the graphic API implementation.
*/
template<typename C>
concept Concept
concept ConceptContextImpl
= std::is_base_of<BaseContext, C>::value
&& requires(
C p_context,
Expand Down Expand Up @@ -55,7 +55,6 @@ namespace VTX::Renderer::Context
std::any & p_textureData,
const ComputePass & p_computePass,
const bool p_bool

) {
{
p_context.build(
Expand All @@ -79,6 +78,192 @@ namespace VTX::Renderer::Context
{ p_context.compute( p_computePass ) } -> std::same_as<void>;
};

/*
enum struct E_FUNCTION
{
BUILD,
RESIZE,
SET_OUTPUT,
SET_VALUE,
RESERVE_DATA,
SET,
SET_SUB,
GET,
FILL_INFOS,
MEASURE_TASK_DURATION,
COMPILE_SHADERS,
SNAPSHOT,
GET_TEXTURE_DATA,
COMPUTE
};
*/

/*
using FunctionBuild
= void ( * )( const RenderQueue &, const Links &, const Handle, const std::vector<BufferData> &, Instructions &,
InstructionsDurationRanges & ); using FunctionResize = void ( * )( const RenderQueue &, const size_t, const
size_t ); using FunctionSetOutput = void ( * )( const Handle ); using FunctionSetValue = void ( * )( const
std::any &, const Key &, const size_t ); using FunctionReserveData = void ( * )( const size_t, const Key &, const
std::any & ); using FunctionSet = void ( * )( const std::vector<std::any> &, const Key & ); using
FunctionSetSub = void ( * )( const std::vector<std::any> &, const Key &, const size_t, const bool, const size_t );
using FunctionGet = void ( * )( std::vector<std::any> &, const Key & );
using FunctionFillInfos = void ( * )( StructInfos & );
using FunctionMeasureTaskDuration = float ( * )( const Util::Chrono::Task & );
using FunctionCompileShaders = void ( * )();
using FunctionSnapshot
= void ( * )( std::vector<uchar> &, const RenderQueue &, const Instructions &, const size_t, const size_t );
using FunctionGetTextureData = void ( * )( std::any &, const size_t, const size_t, const Key &, const E_CHAN_OUT );
using FunctionCompute = void ( * )( const ComputePass & );
*/

using FunctionBuild = std::function<
void( const RenderQueue &, const Links &, const Handle, const std::vector<BufferData> &, Instructions &, InstructionsDurationRanges & )>;
using FunctionResize = std::function<void( const RenderQueue &, const size_t, const size_t )>;
using FunctionSetOutput = std::function<void( const Handle )>;
using FunctionSetValue = std::function<void( const std::any &, const Key &, const size_t )>;
using FunctionReserveData = std::function<void( const size_t, const Key &, const std::any & )>;
using FunctionSet = std::function<void( const std::vector<std::any> &, const Key & )>;
using FunctionSetSub
= std::function<void( const std::vector<std::any> &, const Key &, const size_t, const bool, const size_t )>;
using FunctionGet = std::function<void( std::vector<std::any> &, const Key & )>;
using FunctionFillInfos = std::function<void( StructInfos & )>;
using FunctionMeasureTaskDuration = std::function<float( const Util::Chrono::Task & )>;
using FunctionCompileShaders = std::function<void()>;
using FunctionSnapshot = std::function<
void( std::vector<uchar> &, const RenderQueue &, const Instructions &, const size_t, const size_t )>;
using FunctionGetTextureData
= std::function<void( std::any &, const size_t, const size_t, const Key &, const E_CHAN_OUT )>;
using FunctionCompute = std::function<void( const ComputePass & )>;

class Context
{
public:
// Function pointer to the build function.
template<ConceptContextImpl C>
void set()
{
std::unique_ptr<C> context = std::make_unique<C>();
//_impl = std::move( context );

//_build =
}

/*
template<E_FUNCTION F, typename... Args>
void run( Args... p_args )
{
if constexpr ( F == E_FUNCTION::BUILD )
{
_build( p_args... );
}
else if constexpr ( F == E_FUNCTION::RESIZE )
{
_resize( p_args... );
}
else if constexpr ( F == E_FUNCTION::SET_OUTPUT )
{
_setOutput( p_args... );
}
else if constexpr ( F == E_FUNCTION::SET_VALUE )
{
_setValue( p_args... );
}
else if constexpr ( F == E_FUNCTION::RESERVE_DATA )
{
_reserveData( p_args... );
}
else if constexpr ( F == E_FUNCTION::SET )
{
_set( p_args... );
}
else if constexpr ( F == E_FUNCTION::SET_SUB )
{
_setSub( p_args... );
}
else if constexpr ( F == E_FUNCTION::GET )
{
_get( p_args... );
}
else if constexpr ( F == E_FUNCTION::FILL_INFOS )
{
_fillInfos( p_args... );
}
else if constexpr ( F == E_FUNCTION::MEASURE_TASK_DURATION )
{
_measureTaskDuration( p_args... );
}
else if constexpr ( F == E_FUNCTION::COMPILE_SHADERS )
{
_compileShaders( p_args... );
}
else if constexpr ( F == E_FUNCTION::SNAPSHOT )
{
_snapshot( p_args... );
}
else if constexpr ( F == E_FUNCTION::GET_TEXTURE_DATA )
{
_getTextureData( p_args... );
}
else if constexpr ( F == E_FUNCTION::COMPUTE )
{
_compute( p_args... );
}
else
{
static_assert( std::is_same_v<F, void>, "Unknown function" );
}
}
*/

void build(
const RenderQueue & p_renderQueue,
const Links & p_links,
const Handle p_output,
const std::vector<BufferData> & p_globalData,
Instructions & p_instructions,
InstructionsDurationRanges & p_instructionsDurationRanges
)
{
_build( p_renderQueue, p_links, p_output, p_globalData, p_instructions, p_instructionsDurationRanges );
}

void resize( const RenderQueue & p_renderQueue, const size_t p_width, const size_t p_height )
{
_resize( p_renderQueue, p_width, p_height );
}

void setOutput( const Handle p_output ) { _setOutput( p_output ); }

void setValue( const std::any & p_value, const Key & p_key, const size_t p_index )
{
_setValue( p_value, p_key, p_index );
}

void reserveData( const size_t p_size, const Key & p_key, const std::any & p_value )
{
_reserveData( p_size, p_key, p_value );
}

void set( const std::vector<std::any> & p_data, const Key & p_key ) { _set( p_data, p_key ); }

private:
std::unique_ptr<BaseContext> _impl;

FunctionBuild _build;
FunctionResize _resize;
FunctionSetOutput _setOutput;
FunctionSetValue _setValue;
FunctionReserveData _reserveData;
FunctionSet _set;
FunctionSetSub _setSub;
FunctionGet _get;
FunctionFillInfos _fillInfos;
FunctionMeasureTaskDuration _measureTaskDuration;
FunctionCompileShaders _compileShaders;
FunctionSnapshot _snapshot;
FunctionGetTextureData _getTextureData;
FunctionCompute _compute;
};
} // namespace VTX::Renderer::Context

#endif
14 changes: 6 additions & 8 deletions lib/renderer/include/private/renderer/render_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ namespace VTX::Renderer
{
/**
* @brief A graph with nodes (passes) and links.
* @tparam C The render context.
* @tparam S The scheduler that convert graph to render queue.
*/
template<Context::Concept C, Scheduler::Concept S>
class RenderGraph
{
public:
Expand Down Expand Up @@ -113,7 +110,8 @@ namespace VTX::Renderer
std::erase_if( _links, [ &p_link ]( const std::unique_ptr<Link> & p_e ) { return p_e.get() == p_link; } );
}

C * setup(
template<Context::ConceptContextImpl C, Scheduler::Concept S>
Context::Context * setup(
void * const p_loader,
const size_t p_width,
const size_t p_height,
Expand All @@ -135,7 +133,8 @@ namespace VTX::Renderer
// Compute queue with scheduler.
try
{
_scheduler.schedule( _passes, _links, _output, _renderQueue );
S scheduler;
scheduler.schedule( _passes, _links, _output, _renderQueue );
}
catch ( const std::exception & p_e )
{
Expand Down Expand Up @@ -193,9 +192,8 @@ namespace VTX::Renderer
inline void addGlobalData( const BufferData & p_globalData ) { _globalData.emplace_back( p_globalData ); }

private:
S _scheduler;
RenderQueue _renderQueue;
std::unique_ptr<C> _context;
RenderQueue _renderQueue;
std::unique_ptr<Context::Context> _context;

const Output * _output;
Passes _passes;
Expand Down
18 changes: 6 additions & 12 deletions lib/renderer/include/private/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ namespace VTX::Renderer
class Renderer
{
public:
/**
* @brief The render graph with OpenGL45 API and the DFS scheduler.
*/
using RenderGraphOpenGL45 = RenderGraph<Context::OpenGL45, Scheduler::DepthFirstSearch>;
using RenderGraphDefault = RenderGraph<Context::Default, Scheduler::DepthFirstSearch>;

Renderer(
const size_t p_width,
const size_t p_height,
Expand Down Expand Up @@ -263,12 +257,12 @@ namespace VTX::Renderer
Util::Callback<> onReady;

private:
Context::OpenGL45 * _context = nullptr;
void * _loader = nullptr;
bool _needUpdate = false;
size_t _framesRemaining = BUFFER_COUNT;
FilePath _shaderPath;
std::unique_ptr<RenderGraphOpenGL45> _renderGraph;
Context::Context * _context = nullptr;
void * _loader = nullptr;
bool _needUpdate = false;
size_t _framesRemaining = BUFFER_COUNT;
FilePath _shaderPath;
std::unique_ptr<RenderGraph> _renderGraph;
// All instructions computed by the graph and his context.
Instructions _instructions;
// Used to log render times.
Expand Down
3 changes: 1 addition & 2 deletions lib/renderer/include/public/renderer/facade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "renderer/descriptors.hpp"
#include "renderer/struct_infos.hpp"
#include <any>
#include <util/callback.hpp>
#include <util/types.hpp>

Expand All @@ -21,7 +20,7 @@ namespace VTX::Renderer
} // namespace Proxy

/**
* @brief The facade is the only way to access the renderer from another package.
* @brief The facade is the only way to access the renderer from another package (pimpl).
*/
class Facade
{
Expand Down
14 changes: 12 additions & 2 deletions lib/renderer/src/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace VTX::Renderer
width( p_width ), height( p_height ), _shaderPath( p_shaderPath ), _loader( p_loader )
{
// Graph.
_renderGraph = std::make_unique<RenderGraphOpenGL45>();
_renderGraph = std::make_unique<RenderGraph>();

// Passes.
_refreshGraph();
Expand Down Expand Up @@ -226,7 +226,7 @@ namespace VTX::Renderer
Util::String::durationToStr( Util::CHRONO_CPU(
[ & ]()
{
_context = _renderGraph->setup(
_context = _renderGraph->setup<Context::OpenGL45, Scheduler::DepthFirstSearch>(
p_loader ? p_loader : _loader,
width,
height,
Expand Down Expand Up @@ -375,6 +375,8 @@ namespace VTX::Renderer
cacheSC.representations, "SpheresCylindersRepresentations", cacheSC.rangeSpheres.getFirst()
);
_context->setSub( cacheR.representations, "RibbonsRepresentations", cacheR.range.getFirst() );
cacheR.representations, "RibbonsRepresentations", cacheR.range.getFirst()
);
};

// Remove.
Expand All @@ -385,13 +387,17 @@ namespace VTX::Renderer
{
Cache::SphereCylinder & cacheSC = _cacheSpheresCylinders[ &p_proxy ];
_context->setSub( *p_proxy.atomPositions, "SpheresCylindersPositions", cacheSC.rangeSpheres.getFirst() );
*p_proxy.atomPositions, "SpheresCylindersPositions", cacheSC.rangeSpheres.getFirst()
);
};

// Colors.
p_proxy.onAtomColors += [ this, &p_proxy ]( const std::vector<uchar> & p_colors )
{
Cache::SphereCylinder & cacheSC = _cacheSpheresCylinders[ &p_proxy ];
_context->setSub( p_colors, "SpheresCylindersColors", cacheSC.rangeSpheres.getFirst() );
p_colors, "SpheresCylindersColors", cacheSC.rangeSpheres.getFirst()
);
};

// Residue colors.
Expand Down Expand Up @@ -452,6 +458,8 @@ namespace VTX::Renderer
*/

_context->setSub( cacheSC.flags, "SpheresCylindersFlags", cacheSC.rangeSpheres.getFirst() );
cacheSC.flags, "SpheresCylindersFlags", cacheSC.rangeSpheres.getFirst()
);

// TODO: ribbons and SES.
};
Expand Down Expand Up @@ -745,6 +753,8 @@ namespace VTX::Renderer
p_proxy.onChange += [ this, &p_proxy ]( const size_t p_index )
{
_context->setSub<Util::Color::Rgba>( { ( *p_proxy.colors )[ p_index ] }, "ColorLayout", p_index );
{ ( *p_proxy.colors )[ p_index ] }, "ColorLayout", p_index
);
setNeedUpdate( true );
};
}
Expand Down

0 comments on commit f7b8ba0

Please sign in to comment.