327 lines
11 KiB
C
Raw Normal View History

#ifndef _cg_common_h
#define _cg_common_h
#include <Cg/cg.h>
2014-02-25 21:53:20 +01:00
#include "../export/PSGL/psgl.h"
#include "../PSGL/private.h"
#include <string>
#include "Cg/CgInternal.h"
#include "Cg/CgProgramGroup.h"
#ifdef __cplusplus
2013-01-04 18:27:05 +01:00
extern "C" {
#endif
#define RGL_MAX_VP_SHARED_CONSTANTS 256
#define RGL_MAX_FP_SHARED_CONSTANTS 1024
#define RGL_BOOLEAN_REGISTERS_COUNT 32
2013-01-04 18:27:05 +01:00
// parameter setter, prototype of functions called when a uniform is set.
2014-02-19 01:13:39 +01:00
typedef void(*_cgSetFunction) (void *, const void*);
2013-01-04 18:27:05 +01:00
typedef void(*_cgSetArrayIndexFunction) (void *, const void*, const int index);
2013-01-04 18:27:05 +01:00
typedef struct _CgUniform
{
void *pushBufferPtr;
} _CgUniform;
2013-01-04 18:27:05 +01:00
typedef struct _CGprogram _CGprogram;
2013-01-04 18:27:05 +01:00
typedef struct CgRuntimeParameter
{
_cgSetArrayIndexFunction samplerSetter;
_cgSetArrayIndexFunction setterIndex;
_cgSetArrayIndexFunction settercIndex;
_cgSetArrayIndexFunction setterrIndex;
void *pushBufferPointer;
const CgParameterEntry *parameterEntry;
_CGprogram *program;
int glType;
CGparameter id;
} CgRuntimeParameter;
struct _CGprogram
{
struct _CGprogram* next; // link to next in NULL-terminated singly linked list of programs
CGprogram id; // numerical id for this program object
struct _CGcontext* parentContext; // parent context for this program
void* parentEffect; // parent effect for this program (only used for default program of an effect, containing effect parameters)
bool inLocalMemory; // only pertains to fragment programs which can be in location local, the default, or location main via cgb interfaces
// parameters in the CG_PROGRAM namespace
2013-01-04 18:27:05 +01:00
unsigned int constantPushBufferWordSize;
unsigned int* constantPushBuffer;
unsigned int lastConstantUpdate;
2013-01-04 18:27:05 +01:00
// executable program image
void* platformProgram;
2013-01-04 18:27:05 +01:00
// entire binary program image from compiler
void* platformProgramBinary;
2013-01-04 18:27:05 +01:00
// extra information to ease the coding of the rgl runtime
// the following are used to know which parameters are samplers, so that we can walk them in a quick way.
unsigned int samplerCount;
unsigned int * samplerIndices;
unsigned int * samplerUnits;
2013-01-04 18:27:05 +01:00
//binary format additions
//info previously contained in platformProgram ( loadAddress + nvBinary )
CgProgramHeader header;
const char *name;
const void *ucode;
2013-01-04 18:27:05 +01:00
GLuint loadProgramId;
// offset into the allocation id above, normally zero for internal use.
// But for psglSetFragmentProgramConfiguration it's possible for
// users to manage sub-heap in allocation and put multiple
// program in each allocation.
GLuint loadProgramOffset;
2013-01-04 18:27:05 +01:00
int version; //contained a boolean indicating if the structure pointers have been patched or not
2013-01-04 18:27:05 +01:00
char *parameterResources;
int rtParametersCount;
CgRuntimeParameter *runtimeParameters;
const CgParameterEntry *parametersEntries;
2013-01-04 18:27:05 +01:00
unsigned short *resources;
unsigned short *pushBufferPointers;
2013-01-04 18:27:05 +01:00
int defaultValuesIndexCount;
const CgParameterDefaultValue *defaultValuesIndices;
2013-01-04 18:27:05 +01:00
int semanticCount;
const CgParameterSemantic *semanticIndices;
2013-01-04 18:27:05 +01:00
int defaultValueCount;
const float *defaultValues;
2013-01-04 18:27:05 +01:00
const char *stringTable;
2013-01-04 18:27:05 +01:00
unsigned int **constantPushBufferPointers;
2013-01-04 18:27:05 +01:00
//tmp
unsigned int *samplerValuesLocation;
void *memoryBlock;
2013-01-04 18:27:05 +01:00
_CGprogramGroup *programGroup;
int programIndexInGroup;
2013-01-04 18:27:05 +01:00
//runtime compilation / conversion
void *runtimeElf;
};
2013-01-04 18:27:05 +01:00
typedef struct _CGcontext
{
struct _CGcontext* next; // for global linked list of CGcontexts
CGcontext id; // numerical handle for this object
unsigned int programCount; // number of programs in the list
struct _CGprogram* programList; // head of singly linked list of programs
2013-01-04 18:27:05 +01:00
CGenum compileType; // compile manual, immediate or lazy (unused so far)
2013-01-04 18:27:05 +01:00
// default program, fake owner of the context parameters
_CGprogram defaultProgram;
2013-01-04 18:27:05 +01:00
//groups
CGprogramGroup groupList;
2013-01-04 18:27:05 +01:00
//"static" variable used to store the values of the last parameter for which getParameterValue has been called
double currentParameterValue[16];
char currentParameterName[128];
} _CGcontext;
2013-01-04 18:27:05 +01:00
// internal error handling
RGL_EXPORT void rglCgRaiseError( CGerror error );
// interface between object types
extern void rglCgProgramDestroyAll( _CGcontext* c );
extern void rglCgDestroyContextParam( CgRuntimeParameter* p );
RGL_EXPORT void rglCgProgramErase( _CGprogram* prog );
2013-01-04 18:27:05 +01:00
// default setters
void _cgRaiseInvalidParam( void *data, const void*v );
void _cgRaiseNotMatrixParam( void *data, const void*v );
void _cgIgnoreSetParam( void *dat, const void*v );
2013-03-27 19:18:48 +01:00
void _cgIgnoreParamIndex( void *dat, const void*v, const int index );
2013-01-04 18:27:05 +01:00
// cg helpers
2013-01-04 18:27:05 +01:00
// Is macros
#define CG_IS_CONTEXT(_ctx) rglIsName(&_CurrentContext->cgContextNameSpace, (unsigned int)_ctx)
#define CG_IS_PROGRAM(_program) rglIsName(&_CurrentContext->cgProgramNameSpace, (unsigned int)_program)
#define CG_IS_PARAMETER(_param) rglIsName(&_CurrentContext->cgParameterNameSpace, (unsigned int)(((unsigned int)_param)&CG_PARAMETERMASK))
2013-01-04 18:27:05 +01:00
//array indices
#define CG_PARAMETERSIZE 22 //22 bits == 4 millions parameters
#define CG_PARAMETERMASK ((1<<CG_PARAMETERSIZE)-1)
#define CG_GETINDEX(param) (int)((unsigned int)(param)>>CG_PARAMETERSIZE)
2013-01-04 18:27:05 +01:00
static inline bool isMatrix (CGtype type)
{
if (( type >= CG_FLOAT1x1 && type <= CG_FLOAT4x4 ) ||
( type >= CG_HALF1x1 && type <= CG_HALF4x4 ) ||
( type >= CG_INT1x1 && type <= CG_INT4x4 ) ||
( type >= CG_BOOL1x1 && type <= CG_BOOL4x4 ) ||
( type >= CG_FIXED1x1 && type <= CG_FIXED4x4 ))
return true;
return false;
}
static inline bool isSampler (CGtype type)
{
return ( type >= CG_SAMPLER1D && type <= CG_SAMPLERCUBE );
}
2013-01-04 18:27:05 +01:00
unsigned int rglCountFloatsInCgType( CGtype type );
2013-01-04 18:27:05 +01:00
unsigned int rglGetTypeRowCount( CGtype parameterType );
unsigned int rglGetTypeColCount( CGtype parameterType );
2013-01-04 18:27:05 +01:00
// the internal cg conversions
inline static CgRuntimeParameter* _cgGetParamPtr (CGparameter p)
{
return ( CgRuntimeParameter* )rglGetNamedValue( &_CurrentContext->cgParameterNameSpace, ( unsigned int )((( unsigned int )p )&CG_PARAMETERMASK ) );
}
2013-01-04 18:27:05 +01:00
inline static _CGprogram* _cgGetProgPtr( CGprogram p )
{
return ( _CGprogram* )rglGetNamedValue( &_CurrentContext->cgProgramNameSpace, (unsigned int)p );
}
2013-01-04 18:27:05 +01:00
inline static _CGcontext* _cgGetContextPtr( CGcontext c )
{
return ( _CGcontext* )rglGetNamedValue( &_CurrentContext->cgContextNameSpace, (unsigned int)c );
}
2013-01-04 18:27:05 +01:00
inline static CgRuntimeParameter* rglCgGLTestParameter( CGparameter param )
{
return _cgGetParamPtr( param );
}
2013-01-04 18:27:05 +01:00
CgRuntimeParameter* _cgGLTestArrayParameter( CGparameter paramIn, long offset, long nelements );
CgRuntimeParameter* _cgGLTestTextureParameter( CGparameter param );
2013-01-04 18:27:05 +01:00
static inline int rglGetSizeofSubArray( const unsigned short *dimensions, unsigned short count )
{
int res = 1;
for (int i = 0;i < count;i++)
res *= ( int )(*(dimensions++));
return res;
}
2013-01-04 18:27:05 +01:00
// platform API
CGprofile rglPlatformGetLatestProfile( CGGLenum profile_type );
int rglPlatformCopyProgram( _CGprogram* source, _CGprogram* destination );
2013-01-04 18:27:05 +01:00
void rglPlatformProgramErase (void *data);
2013-01-04 18:27:05 +01:00
int rglPlatformGenerateVertexProgram (void *data, const CgProgramHeader *programHeader, const void *ucode, const CgParameterTableHeader *parameterHeader, const char *stringTable, const float *defaultValues );
CGbool rglPlatformSupportsVertexProgram ( CGprofile p );
2013-01-04 18:27:05 +01:00
int rglPlatformGenerateFragmentProgram (void *data, const CgProgramHeader *programHeader, const void *ucode, const CgParameterTableHeader *parameterHeader, const char *stringTable, const float *defaultValues );
CGbool rglPlatformSupportsFragmentProgram ( CGprofile p );
2013-01-04 18:27:05 +01:00
// names API
2013-01-04 18:27:05 +01:00
static inline GLenum rglCgGetSamplerGLTypeFromCgType( CGtype type )
{
switch ( type )
{
2013-01-04 18:27:05 +01:00
case CG_SAMPLER1D:
case CG_SAMPLER2D:
case CG_SAMPLERRECT:
return GL_TEXTURE_2D;
case CG_SAMPLER3D:
return GL_TEXTURE_3D;
case CG_SAMPLERCUBE:
return GL_TEXTURE_CUBE_MAP;
default:
return 0;
}
2013-01-04 18:27:05 +01:00
}
2013-01-04 18:27:05 +01:00
struct rglNameSpace;
#define VERTEX_PROFILE_INDEX 0
#define FRAGMENT_PROFILE_INDEX 1
2013-01-04 18:27:05 +01:00
// -------------------------------------------
2013-01-04 18:27:05 +01:00
typedef void( * CgcontextHookFunction )( _CGcontext *context );
extern RGL_EXPORT CgcontextHookFunction _cgContextCreateHook;
extern RGL_EXPORT CgcontextHookFunction _cgContextDestroyHook;
2013-01-04 18:27:05 +01:00
typedef void( * CgparameterHookFunction )( CgRuntimeParameter *parameter );
extern RGL_EXPORT CgparameterHookFunction _cgParameterCreateHook;
extern RGL_EXPORT CgparameterHookFunction _cgParameterDestroyHook;
typedef void( * CgprogramHookFunction )( _CGprogram *program );
typedef void( * CgprogramCopyHookFunction )( _CGprogram *newprogram, _CGprogram *oldprogram );
extern RGL_EXPORT CgprogramHookFunction _cgProgramCreateHook;
extern RGL_EXPORT CgprogramHookFunction _cgProgramDestroyHook;
extern RGL_EXPORT CgprogramCopyHookFunction _cgProgramCopyHook;
typedef int (*cgRTCgcCompileHookFunction) (const char*, const char*, const char*, const char**, char**);
2013-01-04 18:27:05 +01:00
typedef void(*cgRTCgcFreeHookFunction) (char*);
extern RGL_EXPORT cgRTCgcCompileHookFunction _cgRTCgcCompileProgramHook;
extern RGL_EXPORT cgRTCgcFreeHookFunction _cgRTCgcFreeCompiledProgramHook;
//-----------------------------------------------
//inlined helper functions
static inline const CgParameterResource *rglGetParameterResource (const void *data, const CgParameterEntry *entry )
{
const _CGprogram *program = (const _CGprogram*)data;
return (CgParameterResource *)(program->parameterResources + entry->typeIndex);
}
static inline CGtype rglGetParameterCGtype (const void *data, const CgParameterEntry *entry)
{
const _CGprogram *program = (const _CGprogram*)data;
if (entry->flags & CGP_RTCREATED)
return (CGtype)entry->typeIndex;
else
{
2013-01-04 18:27:05 +01:00
const CgParameterResource *parameterResource = rglGetParameterResource(program, entry);
if (parameterResource)
return (CGtype)parameterResource->type;
}
2013-01-04 18:27:05 +01:00
return CG_UNKNOWN_TYPE;
}
static inline const CgParameterArray *rglGetParameterArray (const void *data, const CgParameterEntry *entry)
{
const _CGprogram *program = (const _CGprogram*)data;
return (CgParameterArray*)(program->parameterResources + entry->typeIndex);
}
static inline const CgParameterStructure *rglGetParameterStructure (const void *data, const CgParameterEntry *entry )
{
const _CGprogram *program = (const _CGprogram*)data;
return (CgParameterStructure*)(program->parameterResources + entry->typeIndex);
}
inline int rglGetProgramProfileIndex( CGprofile profile )
{
if ( profile == CG_PROFILE_SCE_FP_TYPEB || profile == CG_PROFILE_SCE_FP_TYPEC || profile == CG_PROFILE_SCE_FP_RSX )
return FRAGMENT_PROFILE_INDEX;
else if ( profile == CG_PROFILE_SCE_VP_TYPEB || profile == CG_PROFILE_SCE_VP_TYPEC || profile == CG_PROFILE_SCE_VP_RSX )
return VERTEX_PROFILE_INDEX;
else
return -1;
}
#ifdef __cplusplus
2013-01-04 18:27:05 +01:00
}
#endif
#endif