2017-10-14 12:00:47 +02:00
|
|
|
#pragma once
|
2017-11-12 10:50:05 +01:00
|
|
|
#include "sync.hh"
|
|
|
|
#include "opast.hh"
|
2017-10-14 12:00:47 +02:00
|
|
|
|
2017-11-14 17:35:13 +01:00
|
|
|
struct T_Rendertarget;
|
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
namespace ops {
|
|
|
|
|
|
|
|
using namespace ebcl;
|
|
|
|
|
|
|
|
enum E_OpType
|
|
|
|
{
|
|
|
|
OP_END ,
|
|
|
|
//
|
2017-11-12 17:01:13 +01:00
|
|
|
OP_CALL ,
|
|
|
|
OP_RET ,
|
2017-11-12 22:20:11 +01:00
|
|
|
//
|
|
|
|
OP_SKIP ,
|
|
|
|
OP_COND_SKIP ,
|
2017-11-12 17:01:13 +01:00
|
|
|
//
|
|
|
|
OP_RES_STACK ,
|
|
|
|
OP_PUSH ,
|
|
|
|
OP_POP ,
|
2017-11-13 14:41:03 +01:00
|
|
|
OP_DUP ,
|
2017-11-12 17:01:13 +01:00
|
|
|
//
|
|
|
|
OP_LOAD ,
|
2017-11-14 15:37:42 +01:00
|
|
|
OP_STORE ,
|
2017-11-12 17:01:13 +01:00
|
|
|
OP_SLOAD ,
|
2017-11-13 17:20:45 +01:00
|
|
|
OP_CONST ,
|
2017-11-14 17:35:13 +01:00
|
|
|
OP_OFFSET ,
|
2017-11-12 17:01:13 +01:00
|
|
|
//
|
2017-11-14 23:44:13 +01:00
|
|
|
OP_GET_INPUT ,
|
|
|
|
//
|
2017-11-12 10:50:05 +01:00
|
|
|
OP_FP_LOAD ,
|
2017-11-12 17:01:13 +01:00
|
|
|
OP_FP_STORE ,
|
|
|
|
OP_FP_SLOAD ,
|
|
|
|
OP_FP_SSTORE ,
|
|
|
|
OP_FP_SSTORE_INT ,
|
2017-11-12 10:50:05 +01:00
|
|
|
//
|
|
|
|
OP_FP_CMP ,
|
2017-11-12 17:01:13 +01:00
|
|
|
OP_FP_ADD ,
|
|
|
|
OP_FP_SUB ,
|
|
|
|
OP_FP_MUL ,
|
|
|
|
OP_FP_DIV ,
|
2017-11-13 16:58:38 +01:00
|
|
|
OP_FP_POW ,
|
|
|
|
//
|
2017-11-12 17:01:13 +01:00
|
|
|
OP_FP_NEG ,
|
|
|
|
OP_FP_INV ,
|
2017-11-13 16:58:38 +01:00
|
|
|
OP_FP_NOT ,
|
|
|
|
OP_FP_SIN ,
|
|
|
|
OP_FP_COS ,
|
|
|
|
OP_FP_TAN ,
|
|
|
|
OP_FP_SQRT ,
|
|
|
|
OP_FP_EXP ,
|
|
|
|
OP_FP_LN ,
|
2017-11-13 09:25:32 +01:00
|
|
|
//
|
2017-11-14 17:35:13 +01:00
|
|
|
OP_GEN_ASSETS ,
|
2017-11-13 09:25:32 +01:00
|
|
|
OP_INIT_PIPELINE ,
|
2017-11-13 10:02:16 +01:00
|
|
|
OP_INIT_PROGRAM ,
|
2017-11-13 18:16:48 +01:00
|
|
|
OP_INIT_SAMPLER ,
|
2017-11-13 10:02:16 +01:00
|
|
|
OP_INIT_TEXTURE ,
|
2017-11-13 14:41:03 +01:00
|
|
|
OP_FB_ATTACH ,
|
|
|
|
//
|
|
|
|
OP_USE_FRAMEBUFFER ,
|
|
|
|
OP_FB_TOGGLE ,
|
|
|
|
OP_USE_PIPELINE ,
|
|
|
|
OP_USE_PROGRAM ,
|
2017-11-13 16:58:38 +01:00
|
|
|
OP_USE_TEXTURE ,
|
2017-11-13 17:20:45 +01:00
|
|
|
OP_UNIFORMS ,
|
2017-11-13 17:28:57 +01:00
|
|
|
OP_VIEWPORT ,
|
2017-11-13 09:25:32 +01:00
|
|
|
//
|
2017-11-13 10:02:16 +01:00
|
|
|
OP_FULLSCREEN ,
|
|
|
|
OP_CLEAR ,
|
|
|
|
//
|
|
|
|
OP_UI_PENTER ,
|
|
|
|
OP_UI_PEXIT ,
|
2017-11-13 18:31:21 +01:00
|
|
|
OP_UI_INPUT_DFT ,
|
2017-11-13 18:51:15 +01:00
|
|
|
OP_UI_ODBG ,
|
2017-11-12 10:50:05 +01:00
|
|
|
};
|
|
|
|
|
2017-11-12 17:46:47 +01:00
|
|
|
M_LSHIFT_OP( T_StringBuilder , E_OpType );
|
|
|
|
uint32_t ArgumentsFor( E_OpType op ) noexcept;
|
2017-11-12 18:38:31 +01:00
|
|
|
int32_t DeltaMainStack( E_OpType op ) noexcept;
|
|
|
|
int32_t DeltaFPUStack( E_OpType op ) noexcept;
|
2017-11-12 17:46:47 +01:00
|
|
|
|
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
struct T_Op
|
|
|
|
{
|
2017-11-13 09:25:32 +01:00
|
|
|
static constexpr int MAX_ARGS = 2;
|
2017-11-12 22:20:11 +01:00
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
E_OpType op;
|
|
|
|
T_SRDLocation location;
|
2017-11-12 22:20:11 +01:00
|
|
|
uint32_t args[ MAX_ARGS ];
|
2017-11-12 10:50:05 +01:00
|
|
|
|
|
|
|
T_Op( const E_OpType op ,
|
|
|
|
T_SRDLocation const& location ,
|
2017-11-12 22:20:11 +01:00
|
|
|
const uint32_t arg0 = 0 ) noexcept
|
2017-11-12 10:50:05 +01:00
|
|
|
: op( op ) , location( location ) ,
|
2017-11-12 22:20:11 +01:00
|
|
|
args{ arg0 }
|
2017-11-12 10:50:05 +01:00
|
|
|
{ }
|
2017-11-12 22:20:11 +01:00
|
|
|
|
|
|
|
T_Op( const E_OpType op ,
|
|
|
|
T_SRDLocation const& location ,
|
|
|
|
std::initializer_list< uint32_t > a ) noexcept
|
|
|
|
: op( op ) , location( location )
|
|
|
|
{
|
|
|
|
assert( a.size( ) <= MAX_ARGS );
|
|
|
|
auto it = a.begin( );
|
|
|
|
for ( auto i = 0u ; i < a.size( ) ; i ++ , ++it ) {
|
|
|
|
args[ i ] = *it;
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 10:50:05 +01:00
|
|
|
};
|
2017-11-12 17:46:47 +01:00
|
|
|
M_LSHIFT_OP( T_StringBuilder , T_Op const& );
|
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
|
|
|
|
union T_OpValue
|
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
uint32_t u;
|
|
|
|
float f;
|
2017-11-14 12:49:59 +01:00
|
|
|
|
|
|
|
constexpr T_OpValue( ) : i{ 0 } {}
|
|
|
|
constexpr T_OpValue( uint32_t u ) : u{ u } {}
|
|
|
|
constexpr T_OpValue( float f ) : f{ f } {}
|
2017-11-12 10:50:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct T_OpProgram
|
|
|
|
{
|
|
|
|
T_MultiArray< T_Op > ops; // All operations
|
|
|
|
uint32_t init , // Index of initialisation function
|
|
|
|
frame; // Index of frame rendering function
|
|
|
|
|
|
|
|
T_Array< T_OpValue > constants; // Constants values
|
|
|
|
uint32_t nVariables{ 0 }; // Amount of variables
|
|
|
|
|
|
|
|
T_Array< T_String > inputs; // Input definitions
|
|
|
|
|
|
|
|
uint32_t nPrograms{ 0 } , // Amount of programs
|
|
|
|
nFramebuffers{ 0 } , // Amount of framebuffers
|
|
|
|
nPipelines{ 0 } , // Amount of pipelines
|
|
|
|
nSamplers{ 0 } , // Amount of samplers
|
|
|
|
nTextures{ 0 }; // Amount of textures
|
|
|
|
T_Array< T_String > progNames; // GLSL program files
|
|
|
|
|
|
|
|
T_Array< T_String > uiStrings; // UI strings for profiling, etc.
|
|
|
|
};
|
|
|
|
using P_OpProgram = T_OwnPtr< T_OpProgram >;
|
|
|
|
|
2017-11-14 12:49:59 +01:00
|
|
|
class T_Compiler : public A_PrivateImplementation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
T_Compiler( ) noexcept;
|
|
|
|
|
|
|
|
P_OpProgram compile(
|
|
|
|
opast::T_ParserOutput const& input ) noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
struct T_OpContext
|
|
|
|
{
|
|
|
|
T_OpProgram& program; // The program
|
|
|
|
|
|
|
|
uint32_t instrPtr; // Instruction pointer
|
2017-11-14 12:49:59 +01:00
|
|
|
bool aborted{ false }; // Did the program fail?
|
2017-11-12 10:50:05 +01:00
|
|
|
|
|
|
|
T_Array< T_OpValue > values; // VM data
|
|
|
|
T_Array< T_OpValue > stack; // Main VM stack
|
2017-11-14 14:25:55 +01:00
|
|
|
T_Array< float > initialInputs; // Initial input values
|
2017-11-12 10:50:05 +01:00
|
|
|
|
2017-11-14 12:49:59 +01:00
|
|
|
T_OpValue wreg; // Work register
|
2017-11-12 10:50:05 +01:00
|
|
|
double x87stack[ 8 ]; // x87 FPU emulation stack
|
2017-11-14 12:49:59 +01:00
|
|
|
uint32_t x87sp; // x87 FPU emulation stack pointer
|
|
|
|
|
2017-11-14 17:35:13 +01:00
|
|
|
// Allocated resources
|
|
|
|
T_Array< T_OwnPtr< T_Rendertarget > > framebuffers;
|
|
|
|
T_Array< T_OwnPtr< T_ShaderProgram > > programs;
|
|
|
|
T_Array< T_OwnPtr< T_ShaderPipeline > > pipelines;
|
|
|
|
T_Array< T_OwnPtr< T_TextureSampler > > samplers;
|
|
|
|
T_Array< T_OwnPtr< T_Texture > > textures;
|
|
|
|
|
2017-11-14 14:25:55 +01:00
|
|
|
T_Array< T_String > profiling; // Profiling sections that have been started
|
2017-11-14 17:35:13 +01:00
|
|
|
int32_t curFb{ -1 }; // Index of current framebuffer
|
2017-11-14 14:25:55 +01:00
|
|
|
|
2017-11-14 12:49:59 +01:00
|
|
|
enum E_RunTarget {
|
|
|
|
R_INIT ,
|
|
|
|
R_RENDER
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit T_OpContext( T_OpProgram& program ) noexcept;
|
|
|
|
void run( E_RunTarget target ,
|
|
|
|
float time ,
|
|
|
|
float width ,
|
|
|
|
float height );
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ensureStack( T_Op const& op ,
|
|
|
|
uint32_t min );
|
|
|
|
void ensureFpuStack( T_Op const& op ,
|
|
|
|
uint32_t minStacked ,
|
|
|
|
uint32_t minFree );
|
|
|
|
void checkAddress( T_Op const& op ,
|
|
|
|
uint32_t address );
|
2017-11-12 10:50:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class X_OpFailure : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
X_OpFailure( ) = delete;
|
|
|
|
DEF_COPY( X_OpFailure );
|
|
|
|
DEF_MOVE( X_OpFailure );
|
|
|
|
|
2017-11-14 12:49:59 +01:00
|
|
|
X_OpFailure(
|
|
|
|
T_Op const& op ,
|
2017-11-12 10:50:05 +01:00
|
|
|
T_String error ) noexcept;
|
|
|
|
|
|
|
|
T_Op const& op( ) const noexcept
|
|
|
|
{ return *op_; }
|
|
|
|
T_String const& error( ) const noexcept
|
|
|
|
{ return error_; }
|
|
|
|
|
|
|
|
char const* what( ) const noexcept override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
T_Op const* op_;
|
|
|
|
T_String error_;
|
|
|
|
T_String fullMessage_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ops
|