2017-10-02 13:51:08 +02:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "demo.hh"
|
2017-10-30 18:29:52 +01:00
|
|
|
#include "sync.hh"
|
2017-11-14 22:04:00 +01:00
|
|
|
#include "rendertarget.hh"
|
2017-10-30 18:29:52 +01:00
|
|
|
#include "globals.hh"
|
2017-11-14 22:04:00 +01:00
|
|
|
#include <ebcl/Files.hh>
|
|
|
|
#include <ebcl/SRDText.hh>
|
2017-10-02 13:51:08 +02:00
|
|
|
|
|
|
|
|
2017-11-14 22:04:00 +01:00
|
|
|
T_Demo::T_Demo( const uint32_t width ,
|
|
|
|
const uint32_t height )
|
2017-10-04 11:20:27 +02:00
|
|
|
: width( width ) , height( height )
|
2017-10-02 13:51:08 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
bool T_Demo::initialise( )
|
|
|
|
{
|
2017-11-14 22:04:00 +01:00
|
|
|
#if 0
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher = std::make_unique< T_Raymarcher >(
|
2017-10-04 11:20:27 +02:00
|
|
|
width , height );
|
|
|
|
dof = std::make_unique< T_DoFPass >(
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher->output( ) , raymarcher->depth( ) );
|
2017-10-04 11:20:27 +02:00
|
|
|
bloom = std::make_unique< T_BloomPass >(
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher->output( ) );
|
2017-10-04 11:20:27 +02:00
|
|
|
combine = std::make_unique< T_CombinePass >(
|
2017-10-02 13:51:08 +02:00
|
|
|
dof->output( ) , bloom->output( ) );
|
2017-10-05 18:35:35 +02:00
|
|
|
fxaa = std::make_unique< T_FXAAPass >(
|
|
|
|
combine->output( ) );
|
2017-10-30 19:04:10 +01:00
|
|
|
|
|
|
|
Globals::Sync( ).clearInputs( );
|
2017-10-31 14:21:42 +01:00
|
|
|
// XXX should come from program
|
2017-11-05 21:06:38 +01:00
|
|
|
Globals::Sync( ).addInput( "dof-sharp-distance" , 15 );
|
|
|
|
Globals::Sync( ).addInput( "dof-sharp-range" , 5 );
|
|
|
|
Globals::Sync( ).addInput( "dof-falloff" , 10 );
|
|
|
|
Globals::Sync( ).addInput( "dof-max-blur" , 16 );
|
|
|
|
Globals::Sync( ).addInput( "dof-samples" , 16 );
|
2017-10-30 22:32:28 +01:00
|
|
|
Globals::Sync( ).updateCurveCaches( );
|
2017-11-14 22:04:00 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( !program ) {
|
|
|
|
loadProgram( );
|
|
|
|
}
|
2017-10-30 22:32:28 +01:00
|
|
|
|
2017-10-02 13:51:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_Demo::makeUI( )
|
|
|
|
{
|
2017-10-06 14:29:01 +02:00
|
|
|
auto const& dspSize( ImGui::GetIO( ).DisplaySize );
|
2017-10-06 14:45:00 +02:00
|
|
|
ImGui::SetNextWindowSize( ImVec2( 300 , dspSize.y - 300 ) ,
|
2017-10-06 14:29:01 +02:00
|
|
|
ImGuiSetCond_Once );
|
|
|
|
ImGui::SetNextWindowPos( ImVec2( 0 , 150 ) ,
|
|
|
|
ImGuiSetCond_Once );
|
|
|
|
ImGui::Begin( "Demo controls" );
|
|
|
|
|
2017-11-14 22:04:00 +01:00
|
|
|
#if 0
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher->makeUI( );
|
|
|
|
dof->makeUI( );
|
|
|
|
bloom->makeUI( );
|
|
|
|
combine->makeUI( );
|
2017-10-05 18:35:35 +02:00
|
|
|
fxaa->makeUI( );
|
2017-11-14 22:04:00 +01:00
|
|
|
#else
|
|
|
|
ImGui::Text( "lol, gtfo" );
|
|
|
|
#endif
|
2017-10-06 14:29:01 +02:00
|
|
|
|
|
|
|
ImGui::End( );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Demo::render( )
|
|
|
|
{
|
2017-11-15 09:46:07 +01:00
|
|
|
if ( program && !context ) {
|
2017-11-14 22:04:00 +01:00
|
|
|
runInit( );
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:29:52 +01:00
|
|
|
auto& sync( Globals::Sync( ) );
|
2017-10-07 17:39:38 +02:00
|
|
|
if ( playing ) {
|
|
|
|
const float time = SDL_GetTicks( ) * 1e-3;
|
|
|
|
if ( playingPrevious ) {
|
2017-10-30 18:29:52 +01:00
|
|
|
sync.timeDelta( time - lastFrame );
|
|
|
|
playing = !sync.finished( );
|
2017-10-07 17:39:38 +02:00
|
|
|
}
|
|
|
|
lastFrame = time;
|
|
|
|
}
|
|
|
|
playingPrevious = playing;
|
|
|
|
|
2017-11-14 23:44:13 +01:00
|
|
|
if ( context && !context->aborted ) {
|
|
|
|
try {
|
|
|
|
context->run( ops::T_OpContext::R_RENDER , sync.time( ) , width , height );
|
|
|
|
} catch ( ops::X_OpFailure const& fail ) {
|
|
|
|
printf( "FAILED TO RUN FRAME!\n\t%s\n" , fail.what( ) );
|
|
|
|
context->aborted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 22:04:00 +01:00
|
|
|
#if 0
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher->render( );
|
2017-10-30 18:29:52 +01:00
|
|
|
dof->render( );
|
2017-10-02 13:51:08 +02:00
|
|
|
bloom->render( );
|
|
|
|
combine->render( );
|
2017-10-05 18:35:35 +02:00
|
|
|
fxaa->render( );
|
2017-11-14 22:04:00 +01:00
|
|
|
#endif
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Demo::handleDND(
|
2017-11-14 22:04:00 +01:00
|
|
|
ImVec2 const& move ,
|
|
|
|
const bool hasCtrl ,
|
|
|
|
const bool hasShift ,
|
|
|
|
const bool lmb // Left mouse button
|
2017-10-02 13:51:08 +02:00
|
|
|
)
|
|
|
|
{
|
2017-11-14 22:04:00 +01:00
|
|
|
#if 0
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher->camera( ).handleDND( move , hasCtrl , hasShift , lmb );
|
2017-11-14 22:04:00 +01:00
|
|
|
#endif
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_Demo::handleWheel(
|
2017-11-14 22:04:00 +01:00
|
|
|
const float wheel ,
|
|
|
|
const bool hasCtrl ,
|
|
|
|
const bool hasShift
|
2017-10-02 13:51:08 +02:00
|
|
|
)
|
|
|
|
{
|
2017-11-14 22:04:00 +01:00
|
|
|
#if 0
|
2017-10-02 13:51:08 +02:00
|
|
|
raymarcher->camera( ).handleWheel( wheel , hasCtrl , hasShift );
|
2017-11-14 22:04:00 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
void PrintStreamError(
|
|
|
|
char const* const prefix ,
|
|
|
|
T_String const& name ,
|
|
|
|
ebcl::X_StreamError const& error )
|
|
|
|
{
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << prefix << " '" << name << "': " << error.what( );
|
|
|
|
if ( error.code( ) == ebcl::E_StreamError::SYSTEM_ERROR ) {
|
|
|
|
sb << " (error code " << error.systemError( ) << ")";
|
|
|
|
}
|
|
|
|
sb << '\n' << '\0';
|
|
|
|
fprintf( stderr , "%s" , sb.data( ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteSRDError(
|
|
|
|
T_StringBuilder& sb ,
|
|
|
|
ebcl::T_SRDError const& error )
|
|
|
|
{
|
|
|
|
sb << error.location( ) << " - " << error.error( ) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
bool T_Demo::loadProgram( )
|
|
|
|
{
|
|
|
|
using namespace ebcl;
|
|
|
|
const T_String inputName( T_String::Pooled( "demo.srd" ) );
|
|
|
|
T_File input( inputName , E_FileMode::READ_ONLY );
|
|
|
|
try {
|
|
|
|
input.open( );
|
|
|
|
} catch ( X_StreamError const& e ) {
|
|
|
|
PrintStreamError( "Could not open" , inputName , e );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load SRD data
|
|
|
|
T_SRDMemoryTarget srdOut;
|
|
|
|
srdOut.clearComments( true ).clearFlushToken( true );
|
|
|
|
try {
|
|
|
|
T_SRDTextReader srdReader{ srdOut };
|
|
|
|
T_FileInputStream fis{ input };
|
|
|
|
srdReader.read( inputName , fis );
|
|
|
|
} catch ( X_StreamError const& e ) {
|
|
|
|
PrintStreamError( "Could not open" , inputName , e );
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} catch ( X_SRDErrors const& e ) {
|
|
|
|
T_StringBuilder sb;
|
|
|
|
const auto nErrors( e.errors.size( ) );
|
|
|
|
for ( auto i = 0u ; i < nErrors ; i ++ ) {
|
|
|
|
WriteSRDError( sb , e.errors[ i ] );
|
|
|
|
}
|
|
|
|
sb << "No parsing happened due to format errors\n" << '\0';
|
|
|
|
fprintf( stderr , "%s" , sb.data( ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the fuck
|
|
|
|
opast::T_Parser parser;
|
|
|
|
if ( parser.parse( srdOut.list( ) ) ) {
|
|
|
|
printf( "Parser successful. Compiling...\n" );
|
|
|
|
ops::T_Compiler compiler;
|
|
|
|
program = compiler.compile( *parser.result( ) );
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 09:46:07 +01:00
|
|
|
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "- PARSE ERRORS -------------------------\n";
|
|
|
|
const auto nErrors( parser.errors( ).size( ) );
|
|
|
|
for ( auto i = 0u ; i < nErrors ; i ++ ) {
|
|
|
|
WriteSRDError( sb , parser.errors( )[ i ] );
|
|
|
|
}
|
|
|
|
sb << "----------------------------------------\n";
|
|
|
|
sb << '\0';
|
|
|
|
fprintf( stderr , "%s" , sb.data( ) );
|
2017-11-14 22:04:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void T_Demo::runInit( )
|
|
|
|
{
|
|
|
|
context = NewOwned< ops::T_OpContext >( *program );
|
|
|
|
try {
|
|
|
|
context->run( ops::T_OpContext::R_INIT , 0 , width , height );
|
|
|
|
} catch ( ops::X_OpFailure const& fail ) {
|
|
|
|
printf( "FAILED TO RUN INIT!\n\t%s\n" , fail.what( ) );
|
2017-11-14 23:44:13 +01:00
|
|
|
context->aborted = true;
|
|
|
|
return;
|
2017-11-14 22:04:00 +01:00
|
|
|
}
|
2017-11-14 23:44:13 +01:00
|
|
|
|
|
|
|
Globals::Sync( ).clearInputs( );
|
|
|
|
const auto n( context->initialInputs.size( ) );
|
|
|
|
assert( n == program->inputs.size( ) );
|
|
|
|
for ( auto i = 0u ; i < n ; i ++ ) {
|
|
|
|
Globals::Sync( ).addInput( program->inputs[ i ] ,
|
|
|
|
context->initialInputs[ i ] );
|
2017-11-15 09:46:07 +01:00
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "#%d %s pos %d\n" , i , program->inputs[ i ].toOSString( ).data( ) ,
|
|
|
|
Globals::Sync( ).inputPos( program->inputs[ i ] ) );
|
|
|
|
#endif //INVASIVE_TRACES
|
2017-11-14 23:44:13 +01:00
|
|
|
}
|
|
|
|
Globals::Sync( ).updateCurveCaches( );
|
2017-10-02 13:51:08 +02:00
|
|
|
}
|