2017-10-03 13:53:50 +02:00
|
|
|
#include "externals.hh"
|
2017-11-24 14:52:56 +01:00
|
|
|
|
2017-11-23 23:05:14 +01:00
|
|
|
#include "common.hh"
|
2017-11-24 14:52:56 +01:00
|
|
|
#include "c-project.hh"
|
|
|
|
|
2017-11-23 22:44:20 +01:00
|
|
|
#include "ui.hh"
|
2017-11-23 23:31:24 +01:00
|
|
|
#include "ui-shaders.hh"
|
2017-11-23 14:43:15 +01:00
|
|
|
#include "ui-utilities.hh"
|
2017-10-03 13:53:50 +02:00
|
|
|
|
2017-11-09 12:14:21 +01:00
|
|
|
#include <ebcl/Sets.hh>
|
|
|
|
|
2017-10-03 13:53:50 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
const std::regex GLSLErrorNv_( "^[0-9]*\\(([0-9]+).*$" );
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
const GLenum ProgramTypes_[] = {
|
|
|
|
GL_VERTEX_SHADER ,
|
|
|
|
GL_FRAGMENT_SHADER ,
|
|
|
|
GL_GEOMETRY_SHADER ,
|
|
|
|
GL_COMPUTE_SHADER
|
|
|
|
};
|
|
|
|
|
|
|
|
const GLbitfield PipelineStages_[] = {
|
|
|
|
GL_VERTEX_SHADER_BIT ,
|
|
|
|
GL_FRAGMENT_SHADER_BIT ,
|
|
|
|
GL_GEOMETRY_SHADER_BIT ,
|
|
|
|
GL_COMPUTE_SHADER_BIT
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert( sizeof( PipelineStages_ ) / sizeof( GLbitfield ) == size_t( E_ShaderType::__COUNT__ ) ,
|
|
|
|
"missing pipeline stage constants" );
|
|
|
|
|
2017-10-03 16:10:48 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 13:53:50 +02:00
|
|
|
|
|
|
|
|
2017-11-14 15:27:25 +01:00
|
|
|
/*= T_ShaderPogram ===========================================================*/
|
|
|
|
|
|
|
|
T_ShaderProgram::T_ShaderProgram( )
|
|
|
|
: T_ShaderProgram( 0 )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_ShaderProgram::T_ShaderProgram(
|
|
|
|
T_ShaderProgram const& other )
|
|
|
|
: T_ShaderProgram( other.id_ )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_ShaderProgram::T_ShaderProgram(
|
|
|
|
T_ShaderProgram&& other ) noexcept
|
|
|
|
: T_ShaderProgram( )
|
|
|
|
{
|
|
|
|
swap( other.id_ , id_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderProgram::T_ShaderProgram(
|
|
|
|
const uint32_t id ) noexcept
|
|
|
|
: id_( id )
|
|
|
|
{
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).programs_[ id_ - 1 ].saReferences ++;
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderProgram::~T_ShaderProgram( )
|
|
|
|
{
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).dereferenceProgram( id_ - 1 );
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderProgram& T_ShaderProgram::operator=(
|
|
|
|
T_ShaderProgram const& other )
|
|
|
|
{
|
|
|
|
if ( this != &other ) {
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).dereferenceProgram( id_ - 1 );
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
id_ = other.id_;
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).programs_[ id_ - 1 ].saReferences ++;
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderProgram& T_ShaderProgram::operator=(
|
|
|
|
T_ShaderProgram&& other ) noexcept
|
|
|
|
{
|
|
|
|
if ( this != &other ) {
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).dereferenceProgram( id_ - 1 );
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
id_ = other.id_;
|
|
|
|
other.id_ = T_String{};
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool T_ShaderProgram::valid( ) const noexcept
|
|
|
|
{
|
2017-11-23 22:44:20 +01:00
|
|
|
return id_ && UI::Shaders( ).programs_[ id_ - 1 ].id != 0;
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderProgram::enable( ) const
|
|
|
|
{
|
|
|
|
if ( !id_ ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:44:20 +01:00
|
|
|
auto const& p( UI::Shaders( ).programs_[ id_ - 1 ] );
|
2017-11-14 15:27:25 +01:00
|
|
|
if ( p.id ) {
|
|
|
|
glBindProgramPipeline( 0 );
|
|
|
|
glUseProgram( p.id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint T_ShaderProgram::id( ) const
|
|
|
|
{
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
return UI::Shaders( ).programs_[ id_ - 1 ].id;
|
2017-11-14 15:27:25 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_Optional< E_ShaderType > T_ShaderProgram::type( ) const
|
|
|
|
{
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
return UI::Shaders( ).programs_[ id_ - 1 ].code.type;
|
2017-11-14 15:27:25 +01:00
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_String T_ShaderProgram::name( ) const
|
|
|
|
{
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
return UI::Shaders( ).programs_[ id_ - 1 ].name;
|
2017-11-14 15:27:25 +01:00
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
/*= T_ShaderPipeline =========================================================*/
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline( )
|
2017-11-05 10:02:30 +01:00
|
|
|
: T_ShaderPipeline( T_String{} )
|
2017-10-04 16:29:39 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline const& other )
|
2017-11-05 10:02:30 +01:00
|
|
|
: T_ShaderPipeline( other.id_ )
|
2017-10-04 16:29:39 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
T_ShaderPipeline::T_ShaderPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline&& other ) noexcept
|
2017-10-04 16:29:39 +02:00
|
|
|
: T_ShaderPipeline( )
|
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
swap( other.id_ , id_ );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
T_ShaderPipeline::T_ShaderPipeline( T_String id ) noexcept
|
|
|
|
: id_( std::move( id ) )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).pipelines_.get( id_ )->references ++;
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline::~T_ShaderPipeline( )
|
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).dereferencePipeline( id_ );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline& T_ShaderPipeline::operator=(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline const& other )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
if ( this != &other ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).dereferencePipeline( id_ );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
id_ = other.id_;
|
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).pipelines_.get( id_ )->references ++;
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_ShaderPipeline& T_ShaderPipeline::operator=(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderPipeline&& other ) noexcept
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
if ( this != &other ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
UI::Shaders( ).dereferencePipeline( id_ );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
id_ = other.id_;
|
|
|
|
other.id_ = T_String{};
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool T_ShaderPipeline::valid( ) const noexcept
|
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
return id_
|
2017-11-23 22:44:20 +01:00
|
|
|
&& UI::Shaders( ).pipelines_.get( id_ )->id != 0;
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderPipeline::enable( ) const
|
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( !id_ ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:44:20 +01:00
|
|
|
auto const* pl( UI::Shaders( ).pipelines_.get( id_ ) );
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( pl && pl->id ) {
|
2017-11-14 15:27:25 +01:00
|
|
|
glUseProgram( 0 );
|
2017-11-05 10:02:30 +01:00
|
|
|
glBindProgramPipeline( pl->id );
|
2017-12-25 20:39:56 +01:00
|
|
|
UI::Shaders( ).cPipeline_ = id_;
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-10-15 09:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint T_ShaderPipeline::id( ) const
|
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( id_ ) {
|
2017-11-23 22:44:20 +01:00
|
|
|
return UI::Shaders( ).pipelines_.get( id_ )->id;
|
2017-10-15 09:34:03 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 17:29:48 +02:00
|
|
|
GLuint T_ShaderPipeline::program(
|
2017-11-04 12:00:58 +01:00
|
|
|
const E_ShaderType program ) const
|
2017-10-04 17:29:48 +02:00
|
|
|
{
|
|
|
|
if ( !valid( ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:44:20 +01:00
|
|
|
auto const& sm( UI::Shaders( ) );
|
2017-11-05 10:02:30 +01:00
|
|
|
auto const& pl( *sm.pipelines_.get( id_ ) );
|
2017-10-04 17:29:48 +02:00
|
|
|
for ( auto const& pn : pl.programs ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
auto const* pos( sm.programIndex_.get( pn ) );
|
|
|
|
if ( !pos ) {
|
2017-10-04 17:29:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
auto& p( sm.programs_[ *pos ] );
|
2017-10-04 17:29:48 +02:00
|
|
|
if ( p.code.type == program ) {
|
|
|
|
return p.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
/*= T_CodeBuilder_ ===========================================================*/
|
2017-10-03 16:09:12 +02:00
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
namespace {
|
2017-10-03 16:09:12 +02:00
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
using F_GetInput_ = std::function< T_ShaderInput const*( T_String const& ) >;
|
2017-10-04 16:29:39 +02:00
|
|
|
using T_Code_ = T_ShaderManager::T_ShaderCode;
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
// Code builder, state and functions
|
|
|
|
struct T_CodeBuilder_
|
|
|
|
{
|
2017-10-03 16:09:12 +02:00
|
|
|
struct T_StackEntry_ {
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String name;
|
2017-10-03 16:09:12 +02:00
|
|
|
T_ShaderInput const* input;
|
|
|
|
uint32_t pos;
|
|
|
|
};
|
2017-10-03 16:41:00 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
F_GetInput_ loader;
|
2017-11-05 10:02:30 +01:00
|
|
|
const T_String name;
|
2017-10-04 16:29:39 +02:00
|
|
|
T_Code_& code;
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
T_ShaderInput const* main;
|
2017-11-05 10:02:30 +01:00
|
|
|
T_KeyValueTable< T_String , uint32_t > pos;
|
2017-10-03 16:09:12 +02:00
|
|
|
std::vector< T_StackEntry_ > stack;
|
2017-11-05 10:02:30 +01:00
|
|
|
T_Array< T_String > libraries;
|
2017-10-03 16:41:00 +02:00
|
|
|
T_ShaderInput const* current;
|
2017-10-03 16:09:12 +02:00
|
|
|
uint32_t cpos{ 0 };
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String cname;
|
2017-10-03 16:41:00 +02:00
|
|
|
|
2017-11-04 12:00:58 +01:00
|
|
|
T_CodeBuilder_( F_GetInput_ loader ,
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& name ,
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Code_& code )
|
2017-10-03 16:41:00 +02:00
|
|
|
: loader( loader ) , name( name ) , code( code ) ,
|
2017-10-04 16:29:39 +02:00
|
|
|
main( loader( name ) )
|
2017-10-03 16:41:00 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool buildCode( );
|
2017-11-04 12:00:58 +01:00
|
|
|
void appendChunk( T_ShaderInputChunk const& chunk );
|
2017-11-05 10:02:30 +01:00
|
|
|
void include( T_String const& name ,
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t lines );
|
2017-10-03 16:41:00 +02:00
|
|
|
void next( );
|
2017-10-08 09:45:47 +02:00
|
|
|
|
|
|
|
void addInputLoaderErrors(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderInput const* input ,
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& name );
|
2017-10-03 16:41:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
bool T_CodeBuilder_::buildCode( )
|
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
code = T_Code_{ };
|
2017-11-05 10:02:30 +01:00
|
|
|
code.files.add( name , main != nullptr );
|
2017-10-03 16:41:00 +02:00
|
|
|
if ( !main ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-04 17:29:48 +02:00
|
|
|
if ( main->type == E_ShaderInput::CHUNK
|
|
|
|
|| main->type == E_ShaderInput::LIBRARY ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
code.errors.addNew( name , 0 , "invalid type" );
|
2017-10-04 17:29:48 +02:00
|
|
|
} else {
|
|
|
|
code.type = E_ShaderType( int( main->type ) - 2 );
|
|
|
|
}
|
2017-10-08 09:45:47 +02:00
|
|
|
addInputLoaderErrors( main , name );
|
2017-10-03 16:41:00 +02:00
|
|
|
cname = name;
|
|
|
|
current = main;
|
2017-10-03 16:09:12 +02:00
|
|
|
|
|
|
|
while ( cpos < current->chunks.size( ) ) {
|
|
|
|
auto& chunk( current->chunks[ cpos ] );
|
|
|
|
if ( chunk.type == E_ShaderInputChunk::CODE ) {
|
2017-10-03 16:41:00 +02:00
|
|
|
appendChunk( chunk );
|
2017-10-03 16:09:12 +02:00
|
|
|
} else {
|
2017-10-03 16:41:00 +02:00
|
|
|
include( chunk.text , chunk.lines );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
next( );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
code.code << '\0';
|
2017-10-03 16:09:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
void T_CodeBuilder_::appendChunk(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderInputChunk const& chunk )
|
2017-10-03 16:41:00 +02:00
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
auto* p( pos.get( cname ) );
|
|
|
|
if ( !p ) {
|
|
|
|
pos.add( cname , 0u );
|
|
|
|
p = pos.get( cname );
|
|
|
|
}
|
|
|
|
code.sources.addNew( cname );
|
|
|
|
code.counts.add( chunk.lines );
|
|
|
|
code.starts.add( *p );
|
|
|
|
code.code << chunk.text;
|
|
|
|
*p += chunk.lines;
|
2017-10-03 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_CodeBuilder_::include(
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& nname ,
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t lines )
|
2017-10-03 16:41:00 +02:00
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
auto* const p( pos.get( cname ) );
|
|
|
|
const auto prevPos( *p );
|
|
|
|
*p += lines;
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
// Avoid recursion
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( nname == cname || 0 != count_if( stack.begin( ) , stack.end( ) ,
|
2017-10-03 16:41:00 +02:00
|
|
|
[nname] ( T_StackEntry_ const& e ) {
|
|
|
|
return nname == e.name;
|
|
|
|
} ) ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
T_StringBuilder sb;
|
|
|
|
code.errors.addNew( cname , prevPos ,
|
|
|
|
sb << "recursive inclusion of '"
|
|
|
|
<< nname << "'" );
|
2017-10-03 16:41:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid including libraries more than once
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( libraries.contains( nname ) ) {
|
2017-10-03 16:41:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
T_ShaderInput const* const isi( loader( nname ) );
|
2017-11-05 10:02:30 +01:00
|
|
|
code.files.add( nname , isi != nullptr );
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
// Check for problems
|
|
|
|
if ( !isi ) {
|
|
|
|
// Not found
|
2017-11-05 10:02:30 +01:00
|
|
|
code.errors.addNew( cname , prevPos , "file not found" );
|
2017-10-03 16:41:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( isi->type != E_ShaderInput::CHUNK && isi->type != E_ShaderInput::LIBRARY ) {
|
|
|
|
// Trying to load a top-level shader
|
2017-11-05 10:02:30 +01:00
|
|
|
code.errors.addNew( cname , prevPos , "trying to include a top-level file" );
|
2017-10-03 16:41:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add input loader errors
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( !libraries.contains( nname ) ) {
|
2017-10-08 09:45:47 +02:00
|
|
|
addInputLoaderErrors( isi , nname );
|
2017-10-03 16:41:00 +02:00
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
libraries.add( nname );
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
// Enter the new file
|
|
|
|
stack.push_back( T_StackEntry_{ cname , current , cpos } );
|
|
|
|
cname = nname;
|
|
|
|
current = isi;
|
|
|
|
cpos = UINT32_MAX;
|
2017-11-05 10:02:30 +01:00
|
|
|
pos.set( cname , 0u );
|
2017-10-03 16:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_CodeBuilder_::next( )
|
|
|
|
{
|
|
|
|
cpos ++;
|
|
|
|
while ( cpos == current->chunks.size( ) && !stack.empty( ) ) {
|
|
|
|
T_StackEntry_ const& se( stack[ stack.size( ) - 1 ] );
|
2017-11-05 10:02:30 +01:00
|
|
|
pos.remove( cname );
|
2017-10-03 16:41:00 +02:00
|
|
|
cpos = se.pos + 1;
|
|
|
|
current = se.input;
|
|
|
|
cname = se.name;
|
|
|
|
stack.pop_back( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 09:45:47 +02:00
|
|
|
void T_CodeBuilder_::addInputLoaderErrors(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderInput const* input ,
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& name )
|
2017-10-08 09:45:47 +02:00
|
|
|
{
|
|
|
|
for ( auto const& errs : input->errors ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
code.errors.addNew( name , errs.line , (char*) errs.error.toOSString( ).data( ) );
|
2017-10-08 09:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:41:00 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
T_ShaderManager::T_ShaderManager( ) noexcept
|
|
|
|
: pipelines_( []( T_Pipeline_ const& p ) -> T_String {
|
|
|
|
return p.idString;
|
|
|
|
} )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 16:29:39 +02:00
|
|
|
|
2017-11-14 15:27:25 +01:00
|
|
|
T_ShaderProgram T_ShaderManager::program(
|
|
|
|
T_String const& name )
|
|
|
|
{
|
|
|
|
loadProgram( name );
|
|
|
|
assert( programIndex_.contains( name ) );
|
2017-11-14 22:04:00 +01:00
|
|
|
assert( programs_[ *programIndex_.get( name ) ].name == name );
|
|
|
|
return T_ShaderProgram( *programIndex_.get( name ) + 1 );
|
2017-11-14 15:27:25 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
T_ShaderProgram T_ShaderManager::program(
|
|
|
|
T_String const& name ,
|
|
|
|
const E_ShaderType type ,
|
|
|
|
char const* const source )
|
|
|
|
{
|
|
|
|
assert( name && name.startsWith( "*" ) );
|
|
|
|
assert( type != E_ShaderType::__COUNT__ );
|
|
|
|
loadBuiltinProgram( name , type , source );
|
|
|
|
assert( programIndex_.contains( name ) );
|
|
|
|
assert( programs_[ *programIndex_.get( name ) ].name == name );
|
|
|
|
return T_ShaderProgram( *programIndex_.get( name ) + 1 );
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
T_ShaderPipeline T_ShaderManager::pipeline(
|
2017-11-05 10:02:30 +01:00
|
|
|
std::initializer_list< T_String > shaders )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
if ( shaders.size( ) < 1 || shaders.size( ) > 5 ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
const T_String id( ([&shaders] () {
|
|
|
|
T_StringBuilder sb;
|
|
|
|
for ( auto const& s : shaders ) {
|
|
|
|
sb << s << '|';
|
|
|
|
}
|
|
|
|
sb << '\0';
|
|
|
|
return T_String{ std::move( sb.data( ) ) };
|
2017-10-04 16:29:39 +02:00
|
|
|
} )() );
|
2017-11-05 10:02:30 +01:00
|
|
|
|
|
|
|
auto const* existing( pipelines_.get( id ) );
|
|
|
|
if ( existing ) {
|
|
|
|
return T_ShaderPipeline{ existing->idString };
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelines_.add( T_Pipeline_{ id , shaders } );
|
|
|
|
auto& p( *pipelines_.get( id ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
for ( auto const& pName : shaders ) {
|
2017-11-14 15:27:25 +01:00
|
|
|
loadProgram( p.idString , pName );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
initPipeline( p );
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
return T_ShaderPipeline{ p.idString };
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 17:35:13 +01:00
|
|
|
T_ShaderPipeline T_ShaderManager::pipeline(
|
|
|
|
T_String const* shaderNames ,
|
|
|
|
uint32_t count )
|
|
|
|
{
|
|
|
|
if ( count < 1 || count > 5 ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const T_String id( ([shaderNames,count] () {
|
|
|
|
T_StringBuilder sb;
|
|
|
|
for ( auto i = 0u ; i < count ; i ++ ) {
|
|
|
|
sb << shaderNames[ i ] << '|';
|
|
|
|
}
|
|
|
|
sb << '\0';
|
|
|
|
return T_String{ std::move( sb.data( ) ) };
|
|
|
|
} )() );
|
|
|
|
|
|
|
|
auto const* existing( pipelines_.get( id ) );
|
|
|
|
if ( existing ) {
|
|
|
|
return T_ShaderPipeline{ existing->idString };
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelines_.add( T_Pipeline_{ id , shaderNames , count } );
|
|
|
|
auto& p( *pipelines_.get( id ) );
|
|
|
|
for ( auto i = 0u ; i < count ; i ++ ) {
|
|
|
|
loadProgram( p.idString , shaderNames[ i ] );
|
|
|
|
}
|
|
|
|
initPipeline( p );
|
|
|
|
|
|
|
|
return T_ShaderPipeline{ p.idString };
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:42:51 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
void T_ShaderManager::update( )
|
2017-10-03 16:41:00 +02:00
|
|
|
{
|
2017-11-09 12:14:21 +01:00
|
|
|
using namespace ebcl;
|
|
|
|
T_Set< T_String > temp{ UseTag< ArrayBacked< 64 > >( ) };
|
2017-11-05 13:47:26 +01:00
|
|
|
T_StringBuilder sb;
|
2017-10-04 17:29:48 +02:00
|
|
|
inputs_.clear( );
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
// Check for missing files
|
2017-11-05 13:47:26 +01:00
|
|
|
for ( auto it = missing_.keys( ).cbegin( ) ; it.valid( ) ; ++it ) {
|
2017-12-27 14:53:19 +01:00
|
|
|
const bool exists( ([] ( T_String const& name ) -> bool {
|
|
|
|
T_FSPath p{ T_FSPath{ "shaders" } + T_FSPath{ name } };
|
2017-10-04 16:29:39 +02:00
|
|
|
struct stat buffer;
|
2017-12-27 14:53:19 +01:00
|
|
|
return ( stat( Common::Project( ).strPathOf( p ).data( ) , &buffer ) == 0 );
|
2017-11-05 13:47:26 +01:00
|
|
|
})( *it ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
if ( !exists ) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-05 13:47:26 +01:00
|
|
|
auto& set( missing_.values( )[ it.pos( ) ] );
|
|
|
|
for ( auto& s : set ) {
|
|
|
|
if ( !updates_.contains( s ) ) {
|
|
|
|
updates_.add( std::move( s ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
temp.add( *it );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all marked entries
|
2017-11-09 12:14:21 +01:00
|
|
|
for ( auto i = 0u ; i < temp.size( ) ; i ++ ) {
|
|
|
|
missing_.remove( temp[ i ] );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-11-09 12:14:21 +01:00
|
|
|
temp.clear( );
|
2017-10-04 11:20:27 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
// Reset programs that need to be updated
|
2017-11-05 13:47:26 +01:00
|
|
|
for ( auto it = updates_.begin( ) ; it.valid( ) ; ) {
|
|
|
|
auto const* p( programIndex_.get( *it ) );
|
2017-11-05 10:02:30 +01:00
|
|
|
if ( p ) {
|
|
|
|
auto& pr( programs_[ *p ] );
|
2017-11-14 15:27:25 +01:00
|
|
|
for ( auto const& ref : pr.plReferences ) {
|
2017-11-05 13:47:26 +01:00
|
|
|
if ( !temp.contains( ref ) ) {
|
|
|
|
temp.add( ref );
|
2017-11-05 10:02:30 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
resetProgram( pr );
|
2017-11-05 13:47:26 +01:00
|
|
|
++it;
|
2017-11-05 10:02:30 +01:00
|
|
|
} else {
|
2017-11-05 13:47:26 +01:00
|
|
|
updates_.removeSwap( it.pos( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
2017-10-04 17:29:48 +02:00
|
|
|
if ( updates_.empty( ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-04 11:20:27 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
// Reset pipelines affected by the programs above
|
2017-11-09 12:14:21 +01:00
|
|
|
for ( auto i = 0u ; i < temp.size( ) ; i ++ ) {
|
|
|
|
auto& pipeline( *pipelines_.get( temp[ i ] ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
if ( pipeline.id != 0 ) {
|
|
|
|
glDeleteProgramPipelines( 1 , &pipeline.id );
|
|
|
|
pipeline.id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load all updated programs
|
2017-11-05 13:47:26 +01:00
|
|
|
sb.clear( );
|
2017-10-04 16:29:39 +02:00
|
|
|
for ( auto const& name : updates_ ) {
|
2017-11-05 13:47:26 +01:00
|
|
|
auto& pr( programs_[ *programIndex_.get( name ) ] );
|
2017-10-04 16:29:39 +02:00
|
|
|
initProgram( pr );
|
2017-10-04 17:29:48 +02:00
|
|
|
for ( auto const& e : pr.code.errors ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
sb << e.source << ':' << e.line << ": "
|
|
|
|
<< e.error << '\n';
|
2017-10-04 17:29:48 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-11-05 13:47:26 +01:00
|
|
|
sb << '\0';
|
|
|
|
printf( "%s" , sb.data( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
// Try to initialise all affected pipelines
|
2017-11-09 12:14:21 +01:00
|
|
|
for ( auto i = 0u ; i < temp.size( ) ; i ++ ) {
|
|
|
|
initPipeline( *pipelines_.get( temp[ i ] ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-10-04 17:29:48 +02:00
|
|
|
|
|
|
|
updates_.clear( );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 16:29:39 +02:00
|
|
|
|
2017-11-14 15:27:25 +01:00
|
|
|
void T_ShaderManager::loadProgram(
|
|
|
|
T_String const& pipeline ,
|
|
|
|
T_String const& name )
|
|
|
|
{
|
|
|
|
if ( !useExistingProgram( pipeline , name ) ) {
|
|
|
|
initProgramRecord( name ).plReferences.add( pipeline );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool T_ShaderManager::useExistingProgram(
|
|
|
|
T_String const& pipeline ,
|
|
|
|
T_String const& name )
|
|
|
|
{
|
|
|
|
auto const* pos( programIndex_.get( name ) );
|
|
|
|
if ( !pos ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto& refs( programs_[ *pos ].plReferences );
|
|
|
|
assert( !refs.contains( pipeline ) );
|
|
|
|
refs.add( pipeline );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::loadProgram(
|
|
|
|
T_String const& name )
|
|
|
|
{
|
2017-11-14 22:04:00 +01:00
|
|
|
if ( !useExistingProgram( name ) ) {
|
2017-11-14 15:27:25 +01:00
|
|
|
initProgramRecord( name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
void T_ShaderManager::loadBuiltinProgram(
|
|
|
|
T_String const& name ,
|
|
|
|
const E_ShaderType type ,
|
|
|
|
char const* const source )
|
|
|
|
{
|
|
|
|
if ( !useExistingProgram( name ) ) {
|
|
|
|
initProgramRecord( name , type , source );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 15:27:25 +01:00
|
|
|
bool T_ShaderManager::useExistingProgram(
|
|
|
|
T_String const& name )
|
|
|
|
{
|
|
|
|
auto const* pos( programIndex_.get( name ) );
|
|
|
|
if ( !pos ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
programs_[ *pos ].saReferences ++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-11-14 15:27:25 +01:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
uint32_t T_ShaderManager::newProgramRecord( )
|
|
|
|
{
|
|
|
|
uint32_t i = 0;
|
|
|
|
while ( i < programs_.size( ) ) {
|
2017-11-14 15:27:25 +01:00
|
|
|
if ( programs_[ i ].plReferences.empty( )
|
|
|
|
&& !programs_[ i ].saReferences ) {
|
2017-10-04 16:29:39 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i ++;
|
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
programs_.addNew( );
|
2017-10-04 16:29:39 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-11-14 15:27:25 +01:00
|
|
|
T_ShaderManager::T_Program_& T_ShaderManager::initProgramRecord(
|
2017-11-24 13:45:00 +01:00
|
|
|
T_String const& name ,
|
|
|
|
const E_ShaderType type ,
|
|
|
|
char const* const source )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
const uint32_t index( newProgramRecord( ) );
|
|
|
|
auto& program( programs_[ index ] );
|
2017-11-05 13:47:26 +01:00
|
|
|
programIndex_.add( name , index );
|
|
|
|
program.name = name;
|
2017-10-04 16:29:39 +02:00
|
|
|
program.id = 0;
|
2017-11-24 13:45:00 +01:00
|
|
|
if ( type == E_ShaderType::__COUNT__ || source == nullptr ) {
|
|
|
|
initProgram( program );
|
|
|
|
} else {
|
|
|
|
initBuiltinProgram( program , type , source );
|
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
|
|
|
|
T_StringBuilder sb;
|
2017-10-04 17:29:48 +02:00
|
|
|
for ( auto const& e : program.code.errors ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
sb << e.source << ':' << e.line << ": "
|
|
|
|
<< e.error << '\n';
|
2017-10-04 17:29:48 +02:00
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
sb << '\0';
|
|
|
|
printf( "%s" , sb.data( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
if ( type != E_ShaderType::__COUNT__ && source != nullptr && program.id == 0 ) {
|
|
|
|
std::abort( );
|
|
|
|
}
|
|
|
|
|
2017-11-14 15:27:25 +01:00
|
|
|
return program;
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
T_ShaderInput const* T_ShaderManager::getInput(
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& name )
|
2017-10-03 16:09:12 +02:00
|
|
|
{
|
2017-11-05 11:08:00 +01:00
|
|
|
auto const* const existing( inputs_.get( name ) );
|
|
|
|
if ( existing ) {
|
|
|
|
return existing->get( );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
2017-11-05 11:08:00 +01:00
|
|
|
|
2017-10-03 16:09:12 +02:00
|
|
|
T_ShaderInput ni;
|
2017-11-24 14:52:56 +01:00
|
|
|
const T_String path{ [&]() -> T_String {
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "shaders/" << name;
|
|
|
|
return std::move( sb );
|
|
|
|
}() };
|
2017-12-27 14:53:19 +01:00
|
|
|
if ( !ni.load( Common::Project( ).strPathOf( path ) ) ) {
|
2017-10-03 16:09:12 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-05 11:08:00 +01:00
|
|
|
inputs_.add( name , NewOwned< T_ShaderInput >( std::move( ni ) ) );
|
|
|
|
return inputs_.get( name )->get( );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 16:29:39 +02:00
|
|
|
|
|
|
|
void T_ShaderManager::dereferencePipeline(
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& id )
|
2017-10-03 16:09:12 +02:00
|
|
|
{
|
2017-11-05 10:02:30 +01:00
|
|
|
auto& pipeline( *pipelines_.get( id ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
assert( pipeline.references > 0 );
|
|
|
|
pipeline.references --;
|
|
|
|
if ( pipeline.references > 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pipeline.id != 0 ) {
|
|
|
|
glDeleteProgramPipelines( 1 , &pipeline.id );
|
|
|
|
pipeline.id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( auto const& pName : pipeline.programs ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
auto const* pos( programIndex_.get( pName ) );
|
|
|
|
assert( pos );
|
|
|
|
dereferenceProgram( *pos , id );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-11-05 10:02:30 +01:00
|
|
|
|
|
|
|
pipelines_.remove( pipeline.idString );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::dereferenceProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
const uint32_t index ,
|
2017-11-05 10:02:30 +01:00
|
|
|
T_String const& pipeline )
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
auto& program( programs_[ index ] );
|
2017-11-14 15:27:25 +01:00
|
|
|
auto& refs( program.plReferences );
|
2017-11-05 10:02:30 +01:00
|
|
|
const auto plidx( refs.indexOf( pipeline ) );
|
|
|
|
assert( plidx >= 0 );
|
|
|
|
refs.remove( plidx );
|
2017-11-14 15:27:25 +01:00
|
|
|
if ( !refs.empty( ) || program.saReferences ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resetProgram( program );
|
|
|
|
programIndex_.remove( program.name );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::dereferenceProgram(
|
|
|
|
const uint32_t index )
|
|
|
|
{
|
|
|
|
auto& program( programs_[ index ] );
|
|
|
|
assert( program.saReferences > 0 );
|
|
|
|
program.saReferences --;
|
|
|
|
if ( program.saReferences || !program.plReferences.empty( ) ) {
|
2017-10-04 16:29:39 +02:00
|
|
|
return;
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
resetProgram( program );
|
2017-11-05 13:47:26 +01:00
|
|
|
programIndex_.remove( program.name );
|
2017-10-04 11:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-10-04 11:20:27 +02:00
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::initPipeline(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Pipeline_& pipeline ) const
|
2017-10-04 11:20:27 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
assert( pipeline.id == 0 );
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
printf( "init pipeline %s\n" , pipeline.idString.toOSString( ).data( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
constexpr auto nst{ size_t( E_ShaderType::__COUNT__ ) };
|
|
|
|
int32_t programs[ nst ];
|
|
|
|
for ( auto i = 0u ; i < nst ; i ++ ) {
|
|
|
|
programs[ i ] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( auto const& pName : pipeline.programs ) {
|
2017-11-05 10:02:30 +01:00
|
|
|
const uint32_t pid( *programIndex_.get( pName ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
auto const& program( programs_[ pid ] );
|
|
|
|
if ( programs[ int( program.code.type ) ] != -1
|
|
|
|
|| program.id == 0 ) {
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... failed\n" );
|
2017-10-04 16:29:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
programs[ int( program.code.type ) ] = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint id( 0 );
|
2017-10-06 20:38:08 +02:00
|
|
|
GL_CHECK( {} );
|
2017-10-04 16:29:39 +02:00
|
|
|
glGenProgramPipelines( 1 , &id );
|
|
|
|
GL_CHECK( return );
|
|
|
|
for ( auto i = 0u ; i < nst ; i ++ ) {
|
|
|
|
const auto pid( programs[ i ] );
|
|
|
|
if ( pid == -1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto& program( programs_[ pid ] );
|
|
|
|
const GLbitfield type( PipelineStages_[ int( program.code.type ) ] );
|
|
|
|
glUseProgramStages( id , type , program.id );
|
|
|
|
}
|
|
|
|
GL_CHECK({
|
|
|
|
glDeleteProgramPipelines( 1 , &id );
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... failed\n" );
|
2017-10-04 16:29:39 +02:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
pipeline.id = id;
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... success\n" );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::initProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Program_& program )
|
2017-10-03 13:53:50 +02:00
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
assert( program.id == 0 );
|
|
|
|
|
|
|
|
// Build the code
|
|
|
|
auto name( program.name );
|
2017-11-05 13:47:26 +01:00
|
|
|
printf( "init program %s\n" , program.name.toOSString( ).data( ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
auto& code( program.code );
|
2017-11-05 10:02:30 +01:00
|
|
|
T_CodeBuilder_ cb( [this]( T_String const& n ) { return getInput( n ); } ,
|
2017-11-05 13:47:26 +01:00
|
|
|
name , code );
|
2017-10-04 16:29:39 +02:00
|
|
|
const bool built(
|
|
|
|
T_CodeBuilder_{
|
2017-11-05 10:02:30 +01:00
|
|
|
[this]( T_String const& n ) { return getInput( n ); } ,
|
2017-11-05 13:47:26 +01:00
|
|
|
program.name , code
|
2017-10-04 16:29:39 +02:00
|
|
|
}.buildCode( ) );
|
|
|
|
|
|
|
|
// Initialise file watcher + missing files
|
2017-11-23 23:05:14 +01:00
|
|
|
program.watch = T_WatchedFiles{ Common::Watcher( ) ,
|
2017-10-04 16:29:39 +02:00
|
|
|
[this,name]() {
|
|
|
|
programUpdated( name );
|
2017-11-05 10:02:30 +01:00
|
|
|
} };
|
|
|
|
const auto nf( code.files.size( ) );
|
|
|
|
auto& w( *( program.watch.target( ) ) );
|
|
|
|
for ( auto i = 0u ; i < nf ; i ++ ) {
|
|
|
|
T_String const& fn( code.files.keys( )[ i ] );
|
|
|
|
if ( code.files.values( )[ i ] ) {
|
2017-12-27 14:53:19 +01:00
|
|
|
T_FSPath p{ T_FSPath{ "shaders" } + T_FSPath{ fn } };
|
|
|
|
w.watch( Common::Project( ).strPathOf( p ) );
|
2017-10-04 16:29:39 +02:00
|
|
|
} else {
|
2017-11-05 13:47:26 +01:00
|
|
|
auto& mset( missing_.getOrCreate( fn ) );
|
|
|
|
if ( !mset.contains( name ) ) {
|
|
|
|
mset.add( name );
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !( built && code.errors.empty( ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-24 13:45:00 +01:00
|
|
|
buildProgram( program );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::initBuiltinProgram(
|
|
|
|
T_Program_& program ,
|
|
|
|
E_ShaderType type ,
|
|
|
|
char const* source )
|
|
|
|
{
|
|
|
|
auto name( program.name );
|
|
|
|
printf( "init built-in program %s\n" ,
|
|
|
|
program.name.substr( 1 ).toOSString( ).data( ) );
|
|
|
|
|
|
|
|
program.code.code << source << '\0';
|
|
|
|
program.code.type = type;
|
|
|
|
program.code.counts.add( UINT32_MAX );
|
|
|
|
program.code.starts.add( 0 );
|
|
|
|
program.code.sources.add( program.name );
|
2017-10-04 16:29:39 +02:00
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
buildProgram( program );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::buildProgram(
|
|
|
|
T_Program_& program )
|
|
|
|
{
|
2017-10-04 16:29:39 +02:00
|
|
|
// Try to compile the program
|
2017-11-24 13:45:00 +01:00
|
|
|
auto& code( program.code );
|
|
|
|
auto const& name( program.name );
|
2017-10-04 16:29:39 +02:00
|
|
|
char const* const list[] = {
|
2017-11-05 10:02:30 +01:00
|
|
|
program.code.code.data( )
|
2017-10-04 16:29:39 +02:00
|
|
|
};
|
|
|
|
const GLenum sid = glCreateShaderProgramv(
|
|
|
|
ProgramTypes_[ int( code.type ) ] ,
|
|
|
|
1 , &list[ 0 ] );
|
|
|
|
if ( sid == 0 ) {
|
2017-11-05 13:47:26 +01:00
|
|
|
code.errors.addNew( name , 0 , "failed to create GL program" );
|
2017-10-04 16:29:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and convert the log
|
|
|
|
int infoLogLength( 0 );
|
|
|
|
glGetProgramiv( sid , GL_INFO_LOG_LENGTH , &infoLogLength );
|
|
|
|
if ( infoLogLength ) {
|
|
|
|
char buffer[ infoLogLength + 1 ];
|
|
|
|
glGetProgramInfoLog( sid , infoLogLength , nullptr , buffer );
|
|
|
|
char* start( buffer );
|
|
|
|
char* found( strchr( buffer , '\n' ) );
|
|
|
|
while ( found ) {
|
|
|
|
*found = 0;
|
|
|
|
parseGLSLError( code , start );
|
|
|
|
start = found + 1;
|
|
|
|
found = strchr( start , '\n' );
|
2017-10-03 16:25:46 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
if ( start < &buffer[ infoLogLength - 1 ] ) {
|
|
|
|
parseGLSLError( code , start );
|
2017-10-03 16:09:12 +02:00
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Did it link?
|
|
|
|
GLint lnk( 0 );
|
|
|
|
glGetProgramiv( sid , GL_LINK_STATUS , &lnk );
|
|
|
|
if ( lnk ) {
|
|
|
|
program.id = sid;
|
2017-10-04 17:29:48 +02:00
|
|
|
printf( "... success\n" );
|
2017-10-04 16:29:39 +02:00
|
|
|
} else {
|
|
|
|
glDeleteProgram( sid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::parseGLSLError(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_ShaderCode& code ,
|
|
|
|
char const* errorLine )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
|
|
|
assert( !code.sources.empty( ) );
|
|
|
|
|
|
|
|
std::cmatch m;
|
|
|
|
uint32_t rawLine;
|
|
|
|
if ( std::regex_match( errorLine , m , GLSLErrorNv_ ) ) {
|
2017-10-04 17:29:48 +02:00
|
|
|
rawLine = atoi( m[ 1 ].str( ).c_str( ) );
|
2017-10-03 16:09:12 +02:00
|
|
|
} else {
|
2017-10-04 16:29:39 +02:00
|
|
|
rawLine = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t check = 0 , pos = 0;
|
|
|
|
while ( pos < code.starts.size( ) && check < rawLine ) {
|
|
|
|
check += code.counts[ pos ];
|
|
|
|
pos ++;
|
|
|
|
}
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
code.errors.addNew(
|
|
|
|
pos == 0 ? "*unknown*" : ((char*)code.sources[ pos - 1 ].toOSString( ).data( ) ) ,
|
2017-10-04 17:29:48 +02:00
|
|
|
pos == 0 ? 0 : ( rawLine + code.counts[ pos - 1 ] - check + code.starts[ pos - 1 ] ) ,
|
2017-11-05 10:02:30 +01:00
|
|
|
errorLine );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 13:45:00 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-10-04 16:29:39 +02:00
|
|
|
void T_ShaderManager::programUpdated(
|
2017-11-05 13:47:26 +01:00
|
|
|
T_String const& name )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
2017-11-05 13:47:26 +01:00
|
|
|
if ( !updates_.contains( name ) ) {
|
|
|
|
updates_.add( name );
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_ShaderManager::resetProgram(
|
2017-11-04 12:00:58 +01:00
|
|
|
T_Program_& program )
|
2017-10-04 16:29:39 +02:00
|
|
|
{
|
2017-11-08 09:09:21 +01:00
|
|
|
if ( program.watch ) {
|
|
|
|
program.watch->clear( );
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
|
|
|
if ( program.id != 0 ) {
|
|
|
|
glDeleteProgram( program.id );
|
|
|
|
program.id = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-05 10:02:30 +01:00
|
|
|
auto const& files( program.code.files );
|
|
|
|
const auto nf( files.size( ) );
|
|
|
|
for ( auto i = 0u ; i < nf ; i ++ ) {
|
|
|
|
T_String const& k{ files.keys( )[ i ] };
|
|
|
|
bool v{ files[ i ] };
|
2017-11-05 13:47:26 +01:00
|
|
|
auto* const ptr( missing_.get( k ) );
|
|
|
|
if ( v || !ptr ) {
|
2017-10-04 16:29:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-05 13:47:26 +01:00
|
|
|
auto& set( *ptr );
|
|
|
|
const auto index( set.indexOf( program.name ) );
|
|
|
|
if ( index != -1 ) {
|
|
|
|
set.removeSwap( index );
|
|
|
|
if ( set.empty( ) ) {
|
|
|
|
missing_.remove( k );
|
|
|
|
}
|
2017-10-04 16:29:39 +02:00
|
|
|
}
|
2017-10-03 13:53:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-06 18:42:51 +02:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void T_ShaderManager::makeUI( )
|
|
|
|
{
|
|
|
|
if ( !uiEnabled_ ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:21:08 +01:00
|
|
|
using namespace ImGui;
|
|
|
|
auto const& dspSize( GetIO( ).DisplaySize );
|
2017-11-21 16:30:15 +01:00
|
|
|
SetNextWindowSize( ImVec2( dspSize.x , dspSize.y * .34f ) , ImGuiSetCond_Appearing );
|
|
|
|
SetNextWindowPos( ImVec2( 0 , dspSize.y * .66f ) , ImGuiSetCond_Appearing );
|
2017-11-19 10:21:08 +01:00
|
|
|
Begin( "Shaders" , &uiEnabled_ , ImGuiWindowFlags_NoCollapse );
|
2017-10-06 18:42:51 +02:00
|
|
|
|
|
|
|
const auto n( std::count_if( programs_.begin( ) , programs_.end( ) ,
|
2017-11-24 13:45:00 +01:00
|
|
|
[]( auto const& p ) {
|
|
|
|
return ( !p.plReferences.empty( ) || p.saReferences != 0 )
|
|
|
|
&& !p.name.startsWith( "*" );
|
|
|
|
} ) );
|
2017-10-06 18:42:51 +02:00
|
|
|
|
|
|
|
std::vector< size_t > indices;
|
|
|
|
const auto rn = programs_.size( );
|
|
|
|
for ( auto i = 0u ; i < rn ; i ++ ) {
|
2017-11-14 15:27:25 +01:00
|
|
|
auto const& p( programs_[ i ] );
|
2017-11-24 13:45:00 +01:00
|
|
|
if ( ( !p.plReferences.empty( ) || p.saReferences )
|
|
|
|
&& !p.name.startsWith( "*" ) ) {
|
2017-10-06 18:42:51 +02:00
|
|
|
indices.push_back( i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort( indices.begin( ) , indices.end( ) ,
|
|
|
|
[this]( size_t a , size_t b ) {
|
|
|
|
auto const& pa( programs_[ a ] );
|
|
|
|
auto const& pb( programs_[ b ] );
|
|
|
|
if ( pa.code.errors.size( ) != pb.code.errors.size( ) ) {
|
|
|
|
return pa.code.errors.size( ) > pb.code.errors.size( );
|
|
|
|
}
|
2017-11-14 15:27:25 +01:00
|
|
|
if ( pa.plReferences.size( ) != pb.plReferences.size( ) ) {
|
|
|
|
return pa.plReferences.size( ) > pb.plReferences.size( );
|
2017-10-06 18:42:51 +02:00
|
|
|
}
|
|
|
|
return programs_[ a ].name < programs_[ b ].name;
|
|
|
|
} );
|
|
|
|
|
|
|
|
for ( auto i = 0u ; i < n ; i ++ ) {
|
|
|
|
auto const& program( programs_[ indices[ i ] ] );
|
|
|
|
const auto nErrors( program.code.errors.size( ) );
|
|
|
|
|
|
|
|
const bool open( nErrors
|
2017-11-19 10:21:08 +01:00
|
|
|
? TreeNodeEx( &program , ImGuiTreeNodeFlags_OpenOnArrow
|
2017-10-06 18:42:51 +02:00
|
|
|
| ImGuiTreeNodeFlags_OpenOnDoubleClick , "%s" ,
|
2017-11-05 13:47:26 +01:00
|
|
|
program.name.toOSString( ).data( ) )
|
2017-10-06 18:42:51 +02:00
|
|
|
: false );
|
|
|
|
|
|
|
|
if ( !nErrors ) {
|
2017-11-19 10:21:08 +01:00
|
|
|
Text( "%s" , program.name.toOSString( ).data( ) );
|
2017-10-06 18:42:51 +02:00
|
|
|
}
|
|
|
|
|
2017-11-19 10:21:08 +01:00
|
|
|
SameLine( 400 );
|
|
|
|
Text( "Usage: %u" , program.plReferences.size( ) + program.saReferences );
|
|
|
|
SameLine( 550 );
|
2017-10-06 18:42:51 +02:00
|
|
|
if ( program.code.errors.empty( ) ) {
|
2017-11-19 10:21:08 +01:00
|
|
|
PushStyleColor( ImGuiCol_Text , ImVec4( .6 , 1 , .6 , 1 ) );
|
|
|
|
Text( "No errors" );
|
2017-10-06 18:42:51 +02:00
|
|
|
} else {
|
2017-11-19 10:21:08 +01:00
|
|
|
PushStyleColor( ImGuiCol_Text , ImVec4( 1 , .6 , .6 , 1 ) );
|
|
|
|
Text( "%u error%s" , nErrors , nErrors > 1 ? "s" : "" );
|
2017-10-06 18:42:51 +02:00
|
|
|
}
|
2017-11-19 10:21:08 +01:00
|
|
|
PopStyleColor( );
|
2017-10-06 18:42:51 +02:00
|
|
|
|
|
|
|
if ( open ) {
|
|
|
|
for ( auto const& err : program.code.errors ) {
|
2017-11-19 10:21:08 +01:00
|
|
|
NewLine( );
|
|
|
|
SameLine( 50 );
|
|
|
|
Text( "%s" , err.source.toOSString( ).data( ) );
|
|
|
|
SameLine( 250 );
|
|
|
|
Text( "line %d" , err.line );
|
|
|
|
SameLine( 370 );
|
|
|
|
Text( "%s" , err.error.toOSString( ).data( ) );
|
2017-10-06 18:42:51 +02:00
|
|
|
}
|
2017-11-19 10:21:08 +01:00
|
|
|
TreePop( );
|
2017-10-06 18:42:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:21:08 +01:00
|
|
|
End( );
|
2017-10-06 18:42:51 +02:00
|
|
|
}
|