demotool/control.hh

323 lines
6.2 KiB
C++
Raw Normal View History

2017-10-14 12:00:47 +02:00
#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
struct T_SyncData;
namespace cops {
2017-10-15 10:22:24 +02:00
struct T_Program;
2017-10-14 12:00:47 +02:00
struct T_Context
{
2017-10-15 10:22:24 +02:00
T_Program const* program;
2017-10-14 12:00:47 +02:00
float const* time;
T_SyncData const* sync;
std::vector< float > varValues;
std::map< std::string , uint32_t > varPos;
std::vector< float > opStack;
T_Context& store(
__rd__ std::string const& name ,
__rd__ const float value );
2017-10-14 12:00:47 +02:00
};
class X_OpFailure : public std::exception
{
public:
X_OpFailure( ) = delete;
DEF_COPY( X_OpFailure );
DEF_MOVE( X_OpFailure );
X_OpFailure(
__rd__ std::string const& source ,
__rd__ uint32_t line ,
__rd__ std::string const& error ) noexcept;
std::string const& source( ) const noexcept
{ return source_; }
uint32_t line( ) const noexcept
{ return line_; }
std::string const& error( ) const noexcept
{ return error_; }
char const* what( ) const noexcept override;
private:
std::string source_;
uint32_t line_;
std::string error_;
std::string fullMessage_;
};
/*--------------------------------------------------------------------*/
enum E_Op
{
OP_LOAD_CONSTANT ,
OP_LOAD_VARIABLE ,
OP_LOAD_INPUT ,
OP_STORE_VARIABLE ,
//
OP_ADD , OP_MUL ,
OP_NEG , OP_INV ,
//
OP_DUP , OP_XCHG ,
//
OP_SET_UNIFORM ,
OP_USE_PIPELINE ,
OP_USE_TEXTURE ,
OP_USE_FRAMEBUFFER ,
2017-10-14 21:10:46 +02:00
OP_SET_VIEWPORT ,
//
OP_CLEAR ,
2017-10-15 10:22:24 +02:00
OP_FULLSCREEN ,
//
OP_CALL
2017-10-14 12:00:47 +02:00
};
struct T_Op
{
explicit T_Op(
__rd__ const E_Op op );
virtual ~T_Op( ) = 0;
E_Op op( ) const noexcept
{ return op_; }
virtual void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const = 0;
2017-10-14 12:00:47 +02:00
2017-10-15 10:22:24 +02:00
std::string source{};
uint32_t line{0};
2017-10-14 12:00:47 +02:00
protected:
X_OpFailure error(
__rd__ std::string const& message ) const noexcept;
private:
E_Op op_;
};
2017-10-14 21:10:46 +02:00
using P_Op = std::unique_ptr< T_Op >;
using T_Operations = std::vector< P_Op >;
2017-10-14 12:00:47 +02:00
template< typename T >
inline T_Operations& operator <<(
__rw__ T_Operations& ops ,
__rw__ T&& item )
{
ops.emplace_back( new T( std::move( item ) ) );
return ops;
}
2017-10-15 10:22:24 +02:00
void Execute(
__rd__ T_Operations const& operations ,
__rw__ T_Context& context );
/*--------------------------------------------------------------------*/
struct T_Function
{
std::string name;
uint32_t arguments;
T_Operations operations;
T_Function( __rd__ std::string const& name ,
__rd__ const uint32_t arguments )
: name( name ) , arguments( arguments )
{ }
DEF_COPY( T_Function );
DEF_MOVE( T_Function );
};
struct T_Program
{
std::map< std::string , T_Function > functions;
T_Operations main;
T_Operations& function(
__rd__ std::string const& name ,
__rd__ const uint32_t args );
void execute( __rw__ T_Context& context )
{
context.program = this;
Execute( main , context );
}
};
2017-10-14 12:00:47 +02:00
/*====================================================================*/
struct OPLoadConstant : public T_Op
{
explicit OPLoadConstant(
__rd__ const float constant );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
float constant;
};
struct OPLoadVariable : public T_Op
{
explicit OPLoadVariable(
__rd__ std::string const& variable );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
std::string variable;
};
struct OPLoadInput : public T_Op
{
explicit OPLoadInput(
__rd__ std::string const& input );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
std::string input;
};
struct OPStoreVariable : public T_Op
{
explicit OPStoreVariable(
__rd__ std::string const& variable );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
std::string variable;
};
/*--------------------------------------------------------------------*/
struct OPAdd : public T_Op
{
OPAdd( ) : T_Op( OP_ADD ) {}
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
};
struct OPMul : public T_Op
{
OPMul( ) : T_Op( OP_MUL ) {}
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
};
struct OPNeg : public T_Op
{
OPNeg( ) : T_Op( OP_NEG ) {}
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
};
struct OPInv : public T_Op
{
OPInv( ) : T_Op( OP_INV ) {}
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
};
/*--------------------------------------------------------------------*/
struct OPDup : public T_Op
{
explicit OPDup( uint32_t stackIndex = 0 );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
uint32_t stackIndex;
};
struct OPXchg : public T_Op
{
explicit OPXchg( uint32_t stackIndex = 1 );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
uint32_t stackIndex;
};
/*--------------------------------------------------------------------*/
struct OPSetUniform : public T_Op
{
OPSetUniform(
__rd__ const uint32_t count ,
__rd__ const bool integer );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
uint32_t count;
bool integer;
};
struct OPUsePipeline : public T_Op
{
explicit OPUsePipeline(
__rd__ const uint32_t index );
void execute(
2017-10-14 21:10:46 +02:00
__rw__ T_Context& ctx ) const override;
2017-10-14 12:00:47 +02:00
uint32_t pipeline;
};
2017-10-14 21:10:46 +02:00
struct OPUseTexture : public T_Op
{
OPUseTexture(
__rd__ const uint32_t binding ,
__rd__ const uint32_t texture ,
__rd__ const uint32_t sampler = 0 );
void execute(
__rw__ T_Context& ctx ) const override;
uint32_t binding;
uint32_t texture;
uint32_t sampler;
};
struct OPUseFramebuffer : public T_Op
{
explicit OPUseFramebuffer(
__rd__ const uint32_t framebuffer );
void execute(
__rw__ T_Context& ctx ) const override;
uint32_t framebuffer;
};
struct OPSetViewport : public T_Op
{
OPSetViewport( ) : T_Op( OP_SET_VIEWPORT ) { }
void execute(
__rw__ T_Context& ctx ) const override;
};
/*--------------------------------------------------------------------*/
struct OPClear : public T_Op
{
OPClear( ) : T_Op( OP_CLEAR ) { }
void execute(
__rw__ T_Context& ctx ) const override;
};
struct OPFullscreen : public T_Op
{
OPFullscreen( ) : T_Op( OP_FULLSCREEN ) { }
void execute(
__rw__ T_Context& ctx ) const override;
};
2017-10-15 10:22:24 +02:00
/*--------------------------------------------------------------------*/
2017-10-14 12:00:47 +02:00
2017-10-15 10:22:24 +02:00
struct OPCall : public T_Op
{
explicit OPCall(
__rd__ std::string const& function );
void execute(
__rw__ T_Context& ctx ) const override;
std::string function;
};
2017-10-14 12:00:47 +02:00
} // namespace cops