demotool/ui-shaders.hh

271 lines
5.8 KiB
C++
Raw Normal View History

2017-10-03 13:53:50 +02:00
#pragma once
#include "c-filewatcher.hh"
2017-10-03 13:53:50 +02:00
/*= INPUT FILES ==============================================================*/
// Type of input chunk
enum class E_ShaderInputChunk {
CODE ,
2017-10-08 10:13:11 +02:00
INCLUDE ,
2017-10-03 13:53:50 +02:00
};
// Input chunk data
struct T_ShaderInputChunk
{
E_ShaderInputChunk type;
T_String text;
2017-10-03 13:53:50 +02:00
uint32_t lines;
T_ShaderInputChunk( ) = default;
T_ShaderInputChunk(
2017-11-04 12:00:58 +01:00
const E_ShaderInputChunk type ,
T_String text ,
2017-11-04 12:00:58 +01:00
const uint32_t lines )
2017-10-03 13:53:50 +02:00
: type( type ) , text( std::move( text ) ) , lines( lines )
{ }
};
// Input file type
enum class E_ShaderInput {
CHUNK , // Chunk that may be repeated
LIBRARY , // Library (will only be loaded once)
// "Main" shader source files
VERTEX , FRAGMENT , GEOMETRY ,
COMPUTE
2017-10-03 13:53:50 +02:00
};
// Preprocessing errors
struct T_ShaderInputError
{
uint32_t line;
T_String error;
T_ShaderInputError(
const uint32_t line ,
T_String error )
: line( line ) , error( std::move( error ) )
{ }
2017-10-03 13:53:50 +02:00
};
// Source file
struct T_ShaderInput
{
E_ShaderInput type = E_ShaderInput::CHUNK;
T_Array< T_ShaderInputChunk > chunks;
T_Array< T_ShaderInputError > errors;
2017-10-03 13:53:50 +02:00
bool load( T_String const& path );
2017-10-03 13:53:50 +02:00
};
using P_ShaderInput = T_OwnPtr< T_ShaderInput >;
2017-10-03 13:53:50 +02:00
2017-10-03 16:09:12 +02:00
// Type of shader
enum class E_ShaderType {
VERTEX , FRAGMENT , GEOMETRY ,
COMPUTE ,
__COUNT__
2017-10-03 16:09:12 +02:00
};
// Errors in shader code - the errors it represents may come from either
// the input loader, the shader loader or the driver.
struct T_ShaderError
{
T_String source;
2017-10-03 16:09:12 +02:00
uint32_t line;
T_String error;
T_ShaderError( ) = default;
T_ShaderError( T_String source ,
const uint32_t line ,
T_String error )
: source( std::move( source ) ) , line( line ) ,
error( std::move( error ) )
{ }
T_ShaderError( T_String source ,
const uint32_t line ,
T_StringBuilder& error )
: source( std::move( source ) ) , line( line ) ,
error( std::move( error ) )
{ }
2017-10-03 16:09:12 +02:00
};
struct T_ShaderManager;
struct T_ShaderProgram
{
friend struct T_ShaderManager;
T_ShaderProgram( );
COPY( T_ShaderProgram );
MOVE( T_ShaderProgram );
~T_ShaderProgram( );
bool valid( ) const noexcept;
void enable( ) const;
GLuint id( ) const;
T_Optional< E_ShaderType > type( ) const;
T_String name( ) const;
private:
explicit T_ShaderProgram( uint32_t id ) noexcept;
uint32_t id_;
};
struct T_ShaderPipeline
2017-10-03 16:09:12 +02:00
{
friend struct T_ShaderManager;
T_ShaderPipeline( );
COPY( T_ShaderPipeline );
MOVE( T_ShaderPipeline );
~T_ShaderPipeline( );
bool valid( ) const noexcept;
void enable( ) const;
GLuint id( ) const;
2017-11-04 12:00:58 +01:00
GLuint program( const E_ShaderType program ) const;
private:
explicit T_ShaderPipeline( T_String id ) noexcept;
T_String id_;
};
struct T_ShaderManager
{
friend struct T_ShaderPipeline;
friend struct T_ShaderProgram;
struct T_ShaderCode
{
E_ShaderType type;
T_AutoArray< uint32_t , 8 > starts; // Position of chunk in source file
T_AutoArray< uint32_t , 8 > counts; // Chunk lengths
T_AutoArray< T_String , 8 > sources; // Chunk source files
T_StringBuilder code;
T_Array< T_ShaderError > errors;
T_KeyValueTable< T_String , bool > files;
};
T_ShaderManager( ) noexcept;
2017-11-24 13:45:00 +01:00
// Build / get a program based on its name
T_ShaderProgram program( T_String const& name );
2017-11-24 13:45:00 +01:00
// Build a program from its source code
T_ShaderProgram program( T_String const& name ,
E_ShaderType type ,
char const* source );
T_ShaderPipeline pipeline(
std::initializer_list< T_String > shaders );
T_ShaderPipeline pipeline(
T_String const* shaderNames ,
uint32_t count );
void update( );
2017-10-06 18:42:51 +02:00
void makeUI( );
bool& uiEnabled( )
{ return uiEnabled_; }
private:
struct T_Pipeline_
{
T_String idString;
uint32_t references = 0;
GLuint id = 0;
T_Array< T_String > programs{ 6 };
T_Pipeline_( T_String const& id ,
std::initializer_list< T_String > shaders )
: idString{ id } , programs{ shaders }
{}
T_Pipeline_( T_String const& id ,
T_String const* const shaderNames ,
const uint32_t count ) noexcept
: idString{ id }
{
for ( auto i = 0u ; i < count ; i ++ ) {
programs.add( shaderNames[ i ] );
}
}
};
struct T_Program_
{
T_String name;
T_Array< T_String > plReferences;
uint32_t saReferences{ 0 };
T_ShaderCode code;
GLuint id;
T_Optional< T_WatchedFiles > watch;
};
2017-10-06 18:42:51 +02:00
bool uiEnabled_ = false;
T_ObjectTable< T_String , T_Pipeline_ > pipelines_;
T_Array< T_Program_ > programs_;
T_KeyValueTable< T_String , uint32_t > programIndex_;
2017-10-03 16:09:12 +02:00
T_KeyValueTable< T_String , P_ShaderInput > inputs_;
T_KeyValueTable< T_String , T_Array< T_String > > missing_;
T_Array< T_String > updates_;
// Load/use existing program for use with pipelines
void loadProgram(
T_String const& pipeline ,
T_String const& name );
bool useExistingProgram(
T_String const& pipeline ,
T_String const& name );
// Load/use existing program for standalone use
void loadProgram( T_String const& name );
2017-11-24 13:45:00 +01:00
void loadBuiltinProgram(
T_String const& name ,
E_ShaderType type ,
char const* source );
bool useExistingProgram( T_String const& name );
// Program management
T_Program_& initProgramRecord( // Init management data
2017-11-24 13:45:00 +01:00
T_String const& record ,
E_ShaderType type = E_ShaderType::__COUNT__ ,
char const* source = nullptr );
uint32_t newProgramRecord( ); // Allocate entry in index
T_ShaderInput const* getInput(
T_String const& name );
2017-10-03 16:09:12 +02:00
void dereferencePipeline( T_String const& id );
void dereferenceProgram(
2017-11-04 12:00:58 +01:00
const uint32_t index ,
T_String const& pipeline );
void dereferenceProgram(
const uint32_t index );
void initPipeline( T_Pipeline_& pipeline ) const;
void initProgram( T_Program_& program );
2017-11-24 13:45:00 +01:00
void initBuiltinProgram( T_Program_& program ,
E_ShaderType type ,
char const* source );
void buildProgram( T_Program_& program );
void parseGLSLError(
2017-11-04 12:00:58 +01:00
T_ShaderCode& code ,
char const* errorLine );
void programUpdated( T_String const& name );
void resetProgram( T_Program_& program );
};