2018-11-16 00:31:39 +01:00
/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System ( TM ) emulator .
This file is licensed under the Snes9x License .
For further information , consult the LICENSE file in the root directory .
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-12-10 15:21:37 +01:00
2011-07-02 04:25:13 +02:00
# include "CGLCG.h"
# include "wsnes9x.h"
# include "win32_display.h"
2018-05-20 15:34:48 +02:00
# include "snes9x.h"
2011-07-02 04:25:13 +02:00
# include <png.h>
# ifndef max
# define max(a, b) (((a) > (b)) ? (a) : (b))
# endif
2011-12-10 15:21:37 +01:00
static float npot ( float desired )
2011-07-02 04:25:13 +02:00
{
float out = 512.0 ;
while ( out < desired )
out * = 2 ;
return out ;
}
const GLfloat CGLCG : : lut_coords [ 8 ] = { 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 } ;
CGLCG : : CGLCG ( CGcontext cgContext )
{
this - > cgContext = cgContext ;
fboFunctionsLoaded = FALSE ;
ClearPasses ( ) ;
LoadFBOFunctions ( ) ;
frameCnt = 0 ;
}
CGLCG : : ~ CGLCG ( void )
{
2011-07-03 23:12:41 +02:00
LoadShader ( NULL ) ;
2011-07-02 04:25:13 +02:00
}
void CGLCG : : ClearPasses ( )
{
2011-07-03 23:12:41 +02:00
/* clean up cg programs, fbos and textures from all regular passes
pass 0 is the orignal texture , so ignore that
*/
2011-07-02 04:25:13 +02:00
if ( shaderPasses . size ( ) > 1 ) {
2011-07-03 23:12:41 +02:00
for ( glPassVector : : iterator it = ( shaderPasses . begin ( ) + 1 ) ; it ! = shaderPasses . end ( ) ; it + + ) {
2011-07-02 04:25:13 +02:00
if ( it - > cgFragmentProgram )
cgDestroyProgram ( it - > cgFragmentProgram ) ;
if ( it - > cgVertexProgram )
cgDestroyProgram ( it - > cgVertexProgram ) ;
if ( it - > fbo )
glDeleteFramebuffers ( 1 , & it - > fbo ) ;
if ( it - > tex )
glDeleteTextures ( 1 , & it - > tex ) ;
}
}
2011-07-03 23:12:41 +02:00
for ( glLutVector : : iterator it = lookupTextures . begin ( ) ; it ! = lookupTextures . end ( ) ; it + + ) {
2011-07-02 04:25:13 +02:00
if ( it - > tex )
glDeleteTextures ( 1 , & it - > tex ) ;
}
2011-12-10 15:21:37 +01:00
for ( glPrevDeque : : iterator it = prevPasses . begin ( ) ; it ! = prevPasses . end ( ) ; it + + ) {
if ( it - > tex )
glDeleteTextures ( 1 , & it - > tex ) ;
}
2011-07-02 04:25:13 +02:00
shaderPasses . clear ( ) ;
lookupTextures . clear ( ) ;
2011-12-10 15:21:37 +01:00
prevPasses . clear ( ) ;
// prevPasses deque is always filled with PREV + PREV1-6 elements
prevPasses . resize ( 7 ) ;
2011-07-03 23:12:41 +02:00
2011-07-02 04:25:13 +02:00
shaderLoaded = false ;
}
bool CGLCG : : LoadFBOFunctions ( )
{
if ( fboFunctionsLoaded )
return true ;
const char * extensions = ( const char * ) glGetString ( GL_EXTENSIONS ) ;
if ( extensions & & strstr ( extensions , " framebuffer_object " ) ) {
if ( glGenFramebuffers & & glDeleteFramebuffers & & glBindFramebuffer & & glFramebufferTexture2D & & glClientActiveTexture ) {
fboFunctionsLoaded = true ;
}
}
return fboFunctionsLoaded ;
}
void CGLCG : : checkForCgError ( const char * situation )
{
char buffer [ 4096 ] ;
CGerror error = cgGetError ( ) ;
const char * string = cgGetErrorString ( error ) ;
if ( error ! = CG_NO_ERROR ) {
sprintf ( buffer ,
" Situation: %s \n "
" Error: %s \n \n "
" Cg compiler output... \n " , situation , string ) ;
MessageBoxA ( 0 , buffer ,
" Cg error " , MB_OK | MB_ICONEXCLAMATION ) ;
if ( error = = CG_COMPILER_ERROR ) {
MessageBoxA ( 0 , cgGetLastListing ( cgContext ) ,
" Cg compilation error " , MB_OK | MB_ICONEXCLAMATION ) ;
}
}
}
bool CGLCG : : LoadShader ( const TCHAR * shaderFile )
{
CCGShader cgShader ;
TCHAR shaderPath [ MAX_PATH ] ;
TCHAR tempPath [ MAX_PATH ] ;
CGprofile vertexProfile , fragmentProfile ;
GLenum error ;
if ( ! fboFunctionsLoaded ) {
MessageBox ( NULL , TEXT ( " Your OpenGL graphics driver does not support framebuffer objects. \n You will not be able to use CG shaders in OpenGL mode. " ) , TEXT ( " CG Error " ) ,
MB_OK | MB_ICONEXCLAMATION ) ;
return false ;
}
vertexProfile = cgGLGetLatestProfile ( CG_GL_VERTEX ) ;
fragmentProfile = cgGLGetLatestProfile ( CG_GL_FRAGMENT ) ;
cgGLDisableProfile ( vertexProfile ) ;
cgGLDisableProfile ( fragmentProfile ) ;
ClearPasses ( ) ;
if ( shaderFile = = NULL | | * shaderFile = = TEXT ( ' \0 ' ) )
return true ;
2016-10-08 23:07:02 +02:00
lstrcpy ( shaderPath , shaderFile ) ;
ReduceToPath ( shaderPath ) ;
2011-07-02 04:25:13 +02:00
SetCurrentDirectory ( shaderPath ) ;
if ( ! cgShader . LoadShader ( _tToChar ( shaderFile ) ) )
return false ;
cgGLSetOptimalOptions ( vertexProfile ) ;
cgGLSetOptimalOptions ( fragmentProfile ) ;
2011-07-03 23:12:41 +02:00
/* insert dummy pass that will contain the original texture
*/
2011-07-02 04:25:13 +02:00
shaderPasses . push_back ( shaderPass ( ) ) ;
2011-07-03 23:12:41 +02:00
for ( CCGShader : : passVector : : iterator it = cgShader . shaderPasses . begin ( ) ;
it ! = cgShader . shaderPasses . end ( ) ; it + + ) {
shaderPass pass ;
2011-07-02 04:25:13 +02:00
pass . scaleParams = it - > scaleParams ;
2011-07-03 23:12:41 +02:00
/* if this is the last pass (the only one that can have CG_SCALE_NONE)
and no filter has been set use the GUI setting
*/
if ( pass . scaleParams . scaleTypeX = = CG_SCALE_NONE & & ! it - > filterSet ) {
2018-05-20 18:01:03 +02:00
pass . linearFilter = Settings . BilinearFilter ;
2011-07-03 23:12:41 +02:00
} else {
pass . linearFilter = it - > linearFilter ;
}
2013-03-26 15:39:17 +01:00
pass . frameCounterMod = it - > frameCounterMod ;
2013-03-26 16:09:01 +01:00
pass . floatFbo = it - > floatFbo ;
2011-07-03 23:12:41 +02:00
// paths in the meta file can be relative
2011-07-02 04:25:13 +02:00
_tfullpath ( tempPath , _tFromChar ( it - > cgShaderFile ) , MAX_PATH ) ;
char * fileContents = ReadShaderFileContents ( tempPath ) ;
if ( ! fileContents )
return false ;
2016-10-08 23:07:02 +02:00
// individual shader might include files, these should be relative to shader
ReduceToPath ( tempPath ) ;
SetCurrentDirectory ( tempPath ) ;
2011-07-02 04:25:13 +02:00
pass . cgVertexProgram = cgCreateProgram ( cgContext , CG_SOURCE , fileContents ,
vertexProfile , " main_vertex " , NULL ) ;
checkForCgError ( " Compiling vertex program " ) ;
pass . cgFragmentProgram = cgCreateProgram ( cgContext , CG_SOURCE , fileContents ,
fragmentProfile , " main_fragment " , NULL ) ;
checkForCgError ( " Compiling fragment program " ) ;
2016-10-08 23:07:02 +02:00
// set path back for next pass
SetCurrentDirectory ( shaderPath ) ;
2011-07-02 04:25:13 +02:00
delete [ ] fileContents ;
if ( ! pass . cgVertexProgram | | ! pass . cgFragmentProgram ) {
return false ;
}
cgGLLoadProgram ( pass . cgVertexProgram ) ;
cgGLLoadProgram ( pass . cgFragmentProgram ) ;
2011-07-03 23:12:41 +02:00
/* generate framebuffer and texture for this pass and apply
default texture settings
*/
2011-07-02 04:25:13 +02:00
glGenFramebuffers ( 1 , & pass . fbo ) ;
glGenTextures ( 1 , & pass . tex ) ;
glBindTexture ( GL_TEXTURE_2D , pass . tex ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_BORDER ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_BORDER ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ) ;
2011-07-03 23:12:41 +02:00
shaderPasses . push_back ( pass ) ;
2011-07-02 04:25:13 +02:00
}
2011-07-03 23:12:41 +02:00
for ( std : : vector < CCGShader : : lookupTexture > : : iterator it = cgShader . lookupTextures . begin ( ) ; it ! = cgShader . lookupTextures . end ( ) ; it + + ) {
lookupTexture tex ;
2011-07-02 04:25:13 +02:00
strcpy ( tex . id , it - > id ) ;
2011-07-03 23:12:41 +02:00
/* generate texture for the lut and apply specified filter setting
*/
2011-07-02 04:25:13 +02:00
glGenTextures ( 1 , & tex . tex ) ;
glBindTexture ( GL_TEXTURE_2D , tex . tex ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_BORDER ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_BORDER ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , it - > linearfilter ? GL_LINEAR : GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , it - > linearfilter ? GL_LINEAR : GL_NEAREST ) ;
2011-07-03 23:12:41 +02:00
2011-07-02 04:25:13 +02:00
_tfullpath ( tempPath , _tFromChar ( it - > texturePath ) , MAX_PATH ) ;
2011-07-03 23:12:41 +02:00
// simple file extension png/tga decision
2011-07-02 04:25:13 +02:00
int strLen = strlen ( it - > texturePath ) ;
if ( strLen > 4 ) {
if ( ! strcasecmp ( & it - > texturePath [ strLen - 4 ] , " .png " ) ) {
int width , height ;
bool hasAlpha ;
GLubyte * texData ;
if ( loadPngImage ( tempPath , width , height , hasAlpha , & texData ) ) {
glPixelStorei ( GL_UNPACK_ROW_LENGTH , width ) ;
2013-05-18 21:48:50 +02:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , width ,
2011-07-02 04:25:13 +02:00
height , 0 , hasAlpha ? GL_RGBA : GL_RGB , GL_UNSIGNED_BYTE , texData ) ;
free ( texData ) ;
}
} else if ( ! strcasecmp ( & it - > texturePath [ strLen - 4 ] , " .tga " ) ) {
STGA stga ;
if ( loadTGA ( tempPath , stga ) ) {
glPixelStorei ( GL_UNPACK_ROW_LENGTH , stga . width ) ;
2013-05-18 21:48:50 +02:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , stga . width ,
2011-07-02 04:25:13 +02:00
stga . height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , stga . data ) ;
}
}
}
2011-07-03 23:12:41 +02:00
lookupTextures . push_back ( tex ) ;
2011-07-02 04:25:13 +02:00
}
2011-07-03 23:12:41 +02:00
/* enable texture unit 1 for the lookup textures
*/
2011-07-02 04:25:13 +02:00
glClientActiveTexture ( GL_TEXTURE1 ) ;
glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
glTexCoordPointer ( 2 , GL_FLOAT , 0 , lut_coords ) ;
glClientActiveTexture ( GL_TEXTURE0 ) ;
2011-12-10 15:21:37 +01:00
/* generate textures and set default values for the pref-filled PREV deque.
2011-07-03 23:12:41 +02:00
*/
2011-12-10 15:21:37 +01:00
for ( int i = 0 ; i < prevPasses . size ( ) ; i + + ) {
glGenTextures ( 1 , & prevPasses [ i ] . tex ) ;
glBindTexture ( GL_TEXTURE_2D , prevPasses [ i ] . tex ) ;
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGB , 512 , 512 , 0 , GL_RGB , GL_UNSIGNED_SHORT_5_6_5 , NULL ) ;
glBindTexture ( GL_TEXTURE_2D , 0 ) ;
prevPasses [ i ] . textureSize . x = prevPasses [ i ] . textureSize . y = prevPasses [ i ] . textureSize . x = prevPasses [ i ] . textureSize . y = 0 ;
memset ( prevPasses [ i ] . texCoords , 0 , sizeof ( prevPasses [ i ] . texCoords ) ) ;
}
2011-07-03 23:12:41 +02:00
2011-07-02 04:25:13 +02:00
shaderLoaded = true ;
return true ;
}
void CGLCG : : setTexCoords ( int pass , xySize inputSize , xySize textureSize , bool topdown )
{
float tX = inputSize . x / textureSize . x ;
float tY = inputSize . y / textureSize . y ;
2011-07-03 23:12:41 +02:00
// last pass uses top-down coordinates, all others bottom-up
2011-07-02 04:25:13 +02:00
if ( topdown ) {
shaderPasses [ pass ] . texcoords [ 0 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 1 ] = tY ;
shaderPasses [ pass ] . texcoords [ 2 ] = tX ;
shaderPasses [ pass ] . texcoords [ 3 ] = tY ;
shaderPasses [ pass ] . texcoords [ 4 ] = tX ;
shaderPasses [ pass ] . texcoords [ 5 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 6 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 7 ] = 0.0f ;
} else {
shaderPasses [ pass ] . texcoords [ 0 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 1 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 2 ] = tX ;
shaderPasses [ pass ] . texcoords [ 3 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 4 ] = tX ;
shaderPasses [ pass ] . texcoords [ 5 ] = tY ;
shaderPasses [ pass ] . texcoords [ 6 ] = 0.0f ;
shaderPasses [ pass ] . texcoords [ 7 ] = tY ;
}
glTexCoordPointer ( 2 , GL_FLOAT , 0 , shaderPasses [ pass ] . texcoords ) ;
}
2011-07-03 23:12:41 +02:00
void CGLCG : : Render ( GLuint & origTex , xySize textureSize , xySize inputSize , xySize viewportSize , xySize windowSize )
2011-07-02 04:25:13 +02:00
{
GLenum error ;
frameCnt + + ;
CGprofile vertexProfile , fragmentProfile ;
if ( ! shaderLoaded )
return ;
vertexProfile = cgGLGetLatestProfile ( CG_GL_VERTEX ) ;
fragmentProfile = cgGLGetLatestProfile ( CG_GL_FRAGMENT ) ;
cgGLEnableProfile ( vertexProfile ) ;
cgGLEnableProfile ( fragmentProfile ) ;
2011-07-03 23:12:41 +02:00
/* set up our dummy pass for easier loop code
*/
2011-07-02 04:25:13 +02:00
shaderPasses [ 0 ] . tex = origTex ;
shaderPasses [ 0 ] . outputSize = inputSize ;
2011-07-03 20:42:28 +02:00
shaderPasses [ 0 ] . textureSize = textureSize ;
2011-07-02 04:25:13 +02:00
2011-07-03 23:12:41 +02:00
/* loop through all real passes
*/
2011-07-02 04:25:13 +02:00
for ( int i = 1 ; i < shaderPasses . size ( ) ; i + + ) {
switch ( shaderPasses [ i ] . scaleParams . scaleTypeX ) {
case CG_SCALE_ABSOLUTE :
shaderPasses [ i ] . outputSize . x = ( double ) shaderPasses [ i ] . scaleParams . absX ;
break ;
case CG_SCALE_SOURCE :
shaderPasses [ i ] . outputSize . x = shaderPasses [ i - 1 ] . outputSize . x * shaderPasses [ i ] . scaleParams . scaleX ;
break ;
case CG_SCALE_VIEWPORT :
shaderPasses [ i ] . outputSize . x = viewportSize . x * shaderPasses [ i ] . scaleParams . scaleX ;
break ;
default :
shaderPasses [ i ] . outputSize . x = viewportSize . x ;
}
switch ( shaderPasses [ i ] . scaleParams . scaleTypeY ) {
case CG_SCALE_ABSOLUTE :
2011-12-10 15:21:37 +01:00
shaderPasses [ i ] . outputSize . y = ( double ) shaderPasses [ i ] . scaleParams . absY ;
2011-07-02 04:25:13 +02:00
break ;
case CG_SCALE_SOURCE :
shaderPasses [ i ] . outputSize . y = shaderPasses [ i - 1 ] . outputSize . y * shaderPasses [ i ] . scaleParams . scaleY ;
break ;
case CG_SCALE_VIEWPORT :
shaderPasses [ i ] . outputSize . y = viewportSize . y * shaderPasses [ i ] . scaleParams . scaleY ;
break ;
default :
shaderPasses [ i ] . outputSize . y = viewportSize . y ;
}
2011-07-03 23:12:41 +02:00
/* use next power of two in both directions
*/
2011-07-02 04:25:13 +02:00
float texSize = npot ( max ( shaderPasses [ i ] . outputSize . x , shaderPasses [ i ] . outputSize . y ) ) ;
shaderPasses [ i ] . textureSize . x = shaderPasses [ i ] . textureSize . y = texSize ;
2011-07-03 23:12:41 +02:00
/* set size of output texture
*/
2011-07-02 04:25:13 +02:00
glBindTexture ( GL_TEXTURE_2D , shaderPasses [ i ] . tex ) ;
2013-03-26 16:09:01 +01:00
glTexImage2D ( GL_TEXTURE_2D , 0 , ( shaderPasses [ i ] . floatFbo ? GL_RGBA32F : GL_RGBA ) , ( unsigned int ) shaderPasses [ i ] . textureSize . x ,
2011-07-03 23:12:41 +02:00
( unsigned int ) shaderPasses [ i ] . textureSize . y , 0 , GL_RGBA , GL_UNSIGNED_INT_8_8_8_8 , NULL ) ;
/* viewport determines the area we render into the output texture
*/
2018-08-07 16:03:41 +02:00
glViewport ( 0 , 0 , ( GLsizei ) shaderPasses [ i ] . outputSize . x , ( GLsizei ) shaderPasses [ i ] . outputSize . y ) ;
2011-07-03 23:12:41 +02:00
/* set up framebuffer and attach output texture
*/
2011-07-02 04:25:13 +02:00
glBindFramebuffer ( GL_FRAMEBUFFER , shaderPasses [ i ] . fbo ) ;
glFramebufferTexture2D ( GL_FRAMEBUFFER , GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D , shaderPasses [ i ] . tex , 0 ) ;
2011-07-03 23:12:41 +02:00
/* set up input texture (output of previous pass) and apply filter settings
*/
2011-07-02 04:25:13 +02:00
glBindTexture ( GL_TEXTURE_2D , shaderPasses [ i - 1 ] . tex ) ;
glPixelStorei ( GL_UNPACK_ROW_LENGTH , ( GLint ) shaderPasses [ i - 1 ] . textureSize . x ) ;
2011-07-03 23:12:41 +02:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER ,
shaderPasses [ i ] . linearFilter ? GL_LINEAR : GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER ,
shaderPasses [ i ] . linearFilter ? GL_LINEAR : GL_NEAREST ) ;
/* calculate tex coords first since we pass them to the shader
*/
2011-07-03 20:42:28 +02:00
setTexCoords ( i , shaderPasses [ i - 1 ] . outputSize , shaderPasses [ i - 1 ] . textureSize ) ;
2011-07-03 23:12:41 +02:00
2011-07-03 20:42:28 +02:00
setShaderVars ( i ) ;
2011-07-03 23:12:41 +02:00
2011-07-03 20:42:28 +02:00
cgGLBindProgram ( shaderPasses [ i ] . cgVertexProgram ) ;
cgGLBindProgram ( shaderPasses [ i ] . cgFragmentProgram ) ;
2011-07-03 23:12:41 +02:00
2011-07-02 04:25:13 +02:00
glClearColor ( 0.0f , 0.0f , 0.0f , 0.5f ) ;
glClear ( GL_COLOR_BUFFER_BIT ) ;
glDrawArrays ( GL_QUADS , 0 , 4 ) ;
2011-12-10 15:21:37 +01:00
/* reset client states enabled during setShaderVars
*/
resetAttribParams ( ) ;
2011-07-02 04:25:13 +02:00
}
2011-07-03 23:12:41 +02:00
/* disable framebuffer
*/
2011-07-02 04:25:13 +02:00
glBindFramebuffer ( GL_FRAMEBUFFER , 0 ) ;
2011-07-03 23:12:41 +02:00
2011-12-10 15:21:37 +01:00
/* set last PREV texture as original, push current texture and
sizes to the front of the PREV deque and make sure the new
original texture has the same size as the old one
2011-07-03 23:12:41 +02:00
*/
2011-12-10 15:21:37 +01:00
origTex = prevPasses . back ( ) . tex ;
prevPasses . pop_back ( ) ;
prevPass pass ;
pass . videoSize = inputSize ;
pass . textureSize = textureSize ;
pass . tex = shaderPasses [ 0 ] . tex ;
memcpy ( pass . texCoords , shaderPasses [ 1 ] . texcoords , sizeof ( pass . texCoords ) ) ;
prevPasses . push_front ( pass ) ;
2011-07-03 23:12:41 +02:00
glBindTexture ( GL_TEXTURE_2D , origTex ) ;
2018-08-07 16:03:41 +02:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGB , ( GLsizei ) textureSize . x , ( GLsizei ) textureSize . y , 0 , GL_RGB , GL_UNSIGNED_SHORT_5_6_5 , NULL ) ;
2011-07-03 23:12:41 +02:00
/* bind output of last pass to be rendered on the backbuffer
*/
2011-07-02 04:25:13 +02:00
glBindTexture ( GL_TEXTURE_2D , shaderPasses . back ( ) . tex ) ;
glPixelStorei ( GL_UNPACK_ROW_LENGTH , ( GLint ) shaderPasses . back ( ) . textureSize . x ) ;
2011-07-03 23:12:41 +02:00
/* calculate and apply viewport and texture coordinates to
that will be used in the main ogl code
*/
2018-08-07 16:03:41 +02:00
RECT displayRect = CalculateDisplayRect ( ( unsigned int ) shaderPasses . back ( ) . outputSize . x , ( unsigned int ) shaderPasses . back ( ) . outputSize . y , ( unsigned int ) windowSize . x , ( unsigned int ) windowSize . y ) ;
glViewport ( displayRect . left , ( LONG ) windowSize . y - displayRect . bottom , displayRect . right - displayRect . left , displayRect . bottom - displayRect . top ) ;
2011-07-02 04:25:13 +02:00
setTexCoords ( shaderPasses . size ( ) - 1 , shaderPasses . back ( ) . outputSize , shaderPasses . back ( ) . textureSize , true ) ;
2011-07-03 23:12:41 +02:00
/* render to backbuffer without shaders
*/
2011-07-02 04:25:13 +02:00
cgGLDisableProfile ( vertexProfile ) ;
cgGLDisableProfile ( fragmentProfile ) ;
}
void CGLCG : : setShaderVars ( int pass )
{
2011-07-03 23:12:41 +02:00
/* mvp paramater
*/
2011-07-02 04:25:13 +02:00
CGparameter cgpModelViewProj = cgGetNamedParameter ( shaderPasses [ pass ] . cgVertexProgram , " modelViewProj " ) ;
2011-12-10 15:21:37 +01:00
if ( cgpModelViewProj )
cgGLSetStateMatrixParameter ( cgpModelViewProj , CG_GL_MODELVIEW_PROJECTION_MATRIX , CG_GL_MATRIX_IDENTITY ) ;
2011-07-02 04:25:13 +02:00
# define setProgram2fv(pass,varname,floats)\
{ \
CGparameter cgpf = cgGetNamedParameter ( shaderPasses [ pass ] . cgFragmentProgram , varname ) ; \
CGparameter cgpv = cgGetNamedParameter ( shaderPasses [ pass ] . cgVertexProgram , varname ) ; \
if ( cgpf ) \
cgGLSetParameter2fv ( cgpf , floats ) ; \
if ( cgpv ) \
cgGLSetParameter2fv ( cgpv , floats ) ; \
} \
# define setProgram1f(pass,varname,val)\
{ \
CGparameter cgpf = cgGetNamedParameter ( shaderPasses [ pass ] . cgFragmentProgram , varname ) ; \
CGparameter cgpv = cgGetNamedParameter ( shaderPasses [ pass ] . cgVertexProgram , varname ) ; \
if ( cgpf ) \
cgGLSetParameter1f ( cgpf , val ) ; \
if ( cgpv ) \
cgGLSetParameter1f ( cgpv , val ) ; \
} \
# define setTextureParameter(pass,varname,val)\
{ \
CGparameter cgpf = cgGetNamedParameter ( shaderPasses [ pass ] . cgFragmentProgram , varname ) ; \
if ( cgpf ) { \
cgGLSetTextureParameter ( cgpf , val ) ; \
cgGLEnableTextureParameter ( cgpf ) ; \
} \
} \
# define setTexCoordsParameter(pass,varname,val)\
{ \
CGparameter cgpv = cgGetNamedParameter ( shaderPasses [ pass ] . cgVertexProgram , varname ) ; \
if ( cgpv ) { \
cgGLSetParameterPointer ( cgpv , 2 , GL_FLOAT , 0 , val ) ; \
cgGLEnableClientState ( cgpv ) ; \
2011-12-10 15:21:37 +01:00
cgAttribParams . push_back ( cgpv ) ; \
2011-07-02 04:25:13 +02:00
} \
} \
2011-07-03 23:12:41 +02:00
/* IN paramater
*/
2018-08-07 16:03:41 +02:00
float inputSize [ 2 ] = { ( float ) shaderPasses [ pass - 1 ] . outputSize . x , ( float ) shaderPasses [ pass - 1 ] . outputSize . y } ;
float textureSize [ 2 ] = { ( float ) shaderPasses [ pass - 1 ] . textureSize . x , ( float ) shaderPasses [ pass - 1 ] . textureSize . y } ;
float outputSize [ 2 ] = { ( float ) shaderPasses [ pass ] . outputSize . x , ( float ) shaderPasses [ pass ] . outputSize . y } ;
2011-07-02 04:25:13 +02:00
setProgram2fv ( pass , " IN.video_size " , inputSize ) ;
setProgram2fv ( pass , " IN.texture_size " , textureSize ) ;
setProgram2fv ( pass , " IN.output_size " , outputSize ) ;
2013-03-26 15:39:17 +01:00
unsigned int shaderFrameCnt = frameCnt ;
if ( shaderPasses [ pass ] . frameCounterMod )
shaderFrameCnt % = shaderPasses [ pass ] . frameCounterMod ;
setProgram1f ( pass , " IN.frame_count " , ( float ) shaderFrameCnt ) ;
2018-05-20 15:34:48 +02:00
setProgram1f ( pass , " IN.frame_direction " , Settings . Rewinding ? - 1.0f : 1.0f ) ;
2011-07-02 04:25:13 +02:00
2011-07-03 23:12:41 +02:00
/* ORIG parameter
*/
2018-08-07 16:03:41 +02:00
float orig_videoSize [ 2 ] = { ( float ) shaderPasses [ 0 ] . outputSize . x , ( float ) shaderPasses [ 0 ] . outputSize . y } ;
float orig_textureSize [ 2 ] = { ( float ) shaderPasses [ 0 ] . textureSize . x , ( float ) shaderPasses [ 0 ] . textureSize . y } ;
2011-07-02 04:25:13 +02:00
2011-07-03 23:12:41 +02:00
setProgram2fv ( pass , " ORIG.video_size " , orig_videoSize ) ;
setProgram2fv ( pass , " ORIG.texture_size " , orig_textureSize ) ;
2011-07-02 04:25:13 +02:00
setTextureParameter ( pass , " ORIG.texture " , shaderPasses [ 0 ] . tex ) ;
2011-07-03 20:42:28 +02:00
setTexCoordsParameter ( pass , " ORIG.tex_coord " , shaderPasses [ 1 ] . texcoords ) ;
2011-07-02 04:25:13 +02:00
2011-07-03 23:12:41 +02:00
/* PREV parameter
*/
2011-12-10 15:21:37 +01:00
if ( prevPasses [ 0 ] . textureSize . x > 0 ) {
2018-08-07 16:03:41 +02:00
float prev_videoSize [ 2 ] = { ( float ) prevPasses [ 0 ] . videoSize . x , ( float ) prevPasses [ 0 ] . videoSize . y } ;
float prev_textureSize [ 2 ] = { ( float ) prevPasses [ 0 ] . textureSize . x , ( float ) prevPasses [ 0 ] . textureSize . y } ;
2011-12-10 15:21:37 +01:00
setProgram2fv ( pass , " PREV.video_size " , prev_videoSize ) ;
setProgram2fv ( pass , " PREV.texture_size " , prev_textureSize ) ;
setTextureParameter ( pass , " PREV.texture " , prevPasses [ 0 ] . tex ) ;
setTexCoordsParameter ( pass , " PREV.tex_coord " , prevPasses [ 0 ] . texCoords ) ;
}
2011-07-03 23:12:41 +02:00
2011-12-10 15:21:37 +01:00
/* PREV1-6 parameters
*/
for ( int i = 1 ; i < prevPasses . size ( ) ; i + + ) {
if ( prevPasses [ i ] . textureSize . x = = 0 )
break ;
char varname [ 100 ] ;
2018-08-07 16:03:41 +02:00
float prev_videoSize [ 2 ] = { ( float ) prevPasses [ i ] . videoSize . x , ( float ) prevPasses [ i ] . videoSize . y } ;
float prev_textureSize [ 2 ] = { ( float ) prevPasses [ i ] . textureSize . x , ( float ) prevPasses [ i ] . textureSize . y } ;
2011-12-10 15:21:37 +01:00
sprintf ( varname , " PREV%d.video_size " , i ) ;
setProgram2fv ( pass , varname , prev_videoSize ) ;
sprintf ( varname , " PREV%d.texture_size " , i ) ;
setProgram2fv ( pass , varname , prev_textureSize ) ;
sprintf ( varname , " PREV%d.texture " , i ) ;
setTextureParameter ( pass , varname , prevPasses [ i ] . tex ) ;
sprintf ( varname , " PREV%d.tex_coord " , i ) ;
setTexCoordsParameter ( pass , varname , prevPasses [ i ] . texCoords ) ;
}
2011-07-03 23:12:41 +02:00
/* LUT parameters
*/
2011-07-02 04:25:13 +02:00
for ( int i = 0 ; i < lookupTextures . size ( ) ; i + + ) {
setTextureParameter ( pass , lookupTextures [ i ] . id , lookupTextures [ i ] . tex ) ;
}
2011-07-03 23:12:41 +02:00
/* PASSX parameters, only for third pass and up
*/
if ( pass > 2 ) {
2011-07-02 04:25:13 +02:00
for ( int i = 1 ; i < pass - 1 ; i + + ) {
char varname [ 100 ] ;
2018-08-07 16:03:41 +02:00
float pass_videoSize [ 2 ] = { ( float ) shaderPasses [ i ] . outputSize . x , ( float ) shaderPasses [ i ] . outputSize . y } ;
float pass_textureSize [ 2 ] = { ( float ) shaderPasses [ i ] . textureSize . x , ( float ) shaderPasses [ i ] . textureSize . y } ;
2011-07-02 04:25:13 +02:00
sprintf ( varname , " PASS%d.video_size " , i ) ;
2011-07-03 23:12:41 +02:00
setProgram2fv ( pass , varname , pass_videoSize ) ;
2011-07-02 04:25:13 +02:00
sprintf ( varname , " PASS%d.texture_size " , i ) ;
2011-07-03 23:12:41 +02:00
setProgram2fv ( pass , varname , pass_textureSize ) ;
2011-07-02 04:25:13 +02:00
sprintf ( varname , " PASS%d.texture " , i ) ;
2011-07-03 20:42:28 +02:00
setTextureParameter ( pass , varname , shaderPasses [ i ] . tex ) ;
2011-07-02 04:25:13 +02:00
sprintf ( varname , " PASS%d.tex_coord " , i ) ;
2011-07-03 20:42:28 +02:00
setTexCoordsParameter ( pass , varname , shaderPasses [ i + 1 ] . texcoords ) ;
2011-07-02 04:25:13 +02:00
}
}
}
2011-12-10 15:21:37 +01:00
void CGLCG : : resetAttribParams ( )
{
for ( int i = 0 ; i < cgAttribParams . size ( ) ; i + + )
cgGLDisableClientState ( cgAttribParams [ i ] ) ;
cgAttribParams . clear ( ) ;
}