/*---------------------------------------------------------------------------* Copyright (C) Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ ////=========================================================================== /// demoGfxShader.c /// /// This is graphics shader system code for the DEMO library. /// ////=========================================================================== #include #include #include BOOL DEMOGfxLoadShaders(DEMOGfxShader *pShader, u32 index, const void *pData) { memset(pShader, 0, sizeof(DEMOGfxShader)); // Load shaders from memory. if(!DEMOGFDReadVertexShader(&pShader->pVertexShader, index, pData)) { OSReport("Warning : The file did not contain a vertex shader\n"); return FALSE; } if(!DEMOGFDReadPixelShader(&pShader->pPixelShader, index, pData)) { OSReport("Warning : The file did not contain a pixel shader\n"); return FALSE; } if(!DEMOGFDReadGeometryShader(&pShader->pGeometryShader, index, pData)) { //OSReport("Warning : The file did not contain a geometry shader\n"); //return FALSE; } return TRUE; } BOOL DEMOGfxInitShaderAttribute(DEMOGfxShader *pShader, const char *name, u32 bufferIndex, u32 offset, GX2AttribFormat format) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(bufferIndex > GX2_MAX_ATTRIB_BUFFERS ) { OSReport("Warning : The attribute buffer index (%d) is over %d\n", bufferIndex, GX2_MAX_ATTRIB_BUFFERS); return FALSE; } // Get location u32 location = (u32)GX2GetVertexAttribVarLocation(pShader->pVertexShader, name); ASSERT(DEMOGfxCheckShaderLocation(location)); // Initialize the vertex attribute structure for the fetch shader. GX2InitAttribStream(&pShader->attribs[pShader->attribCount], location, bufferIndex, offset, format); // Increase count pShader->attribCount++; return TRUE; } BOOL DEMOGfxGetVertexShaderSamplerLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->samplersVS.count > GX2_MAX_SAMPLERS ) { OSReport("Warning : The sampler count (%d) in VS is over %d\n", pShader->samplersVS.count, GX2_MAX_SAMPLERS); return FALSE; } // Set sampler data pShader->samplersVS.location[pShader->samplersVS.count] = (u32)GX2GetVertexSamplerVarLocation(pShader->pVertexShader, name); ASSERT(DEMOGfxCheckShaderLocation(pShader->samplersVS.location[pShader->samplersVS.count])); // Increase count pShader->samplersVS.count++; return TRUE; } BOOL DEMOGfxGetPixelShaderSamplerLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->samplersPS.count > GX2_MAX_SAMPLERS ) { OSReport("Warning : The sampler count (%d) in PS is over %d\n", pShader->samplersPS.count, GX2_MAX_SAMPLERS); return FALSE; } // Get sampler location pShader->samplersPS.location[pShader->samplersPS.count] = (u32)GX2GetPixelSamplerVarLocation(pShader->pPixelShader, name); ASSERT(DEMOGfxCheckShaderLocation(pShader->samplersPS.location[pShader->samplersPS.count])); // Increase count pShader->samplersPS.count++; return TRUE; } BOOL DEMOGfxGetGeometryShaderSamplerLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->samplersGS.count > GX2_MAX_SAMPLERS ) { OSReport("Warning : The sampler count (%d) in GS is over %d\n", pShader->samplersGS.count, GX2_MAX_SAMPLERS); return FALSE; } // Get sampler location pShader->samplersGS.location[pShader->samplersGS.count] = (u32)GX2GetGeometrySamplerVarLocation(pShader->pGeometryShader, name); ASSERT(DEMOGfxCheckShaderLocation(pShader->samplersGS.location[pShader->samplersGS.count])); // Increase count pShader->samplersGS.count++; return TRUE; } BOOL DEMOGfxGetVertexShaderUniformLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->uniformsVS.count > GX2_MAX_VS_UNIFORM_VARS ) { OSReport("Warning : The uniform count (%d) in VS is over %d\n", pShader->uniformsVS.count, GX2_MAX_VS_UNIFORM_VARS); return FALSE; } // Get uniform location pShader->uniformsVS.location[pShader->uniformsVS.count] = (u32)GX2GetVertexUniformVarOffset(pShader->pVertexShader, name); ASSERT(DEMOGfxCheckShaderLocation(pShader->uniformsVS.location[pShader->uniformsVS.count])); // Increase count pShader->uniformsVS.count++; return TRUE; } BOOL DEMOGfxGetPixelShaderUniformLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->uniformsPS.count > GX2_MAX_PS_UNIFORM_VARS ) { OSReport("Warning : The uniform count (%d) in PS is over %d\n", pShader->uniformsPS.count, GX2_MAX_PS_UNIFORM_VARS); return FALSE; } // Get uniform location pShader->uniformsPS.location[pShader->uniformsPS.count] = (u32)GX2GetPixelUniformVarOffset(pShader->pPixelShader, name); ASSERT(DEMOGfxCheckShaderLocation(pShader->uniformsPS.location[pShader->uniformsPS.count])); // Increase count pShader->uniformsPS.count++; return TRUE; } BOOL DEMOGfxGetVertexShaderUniformBlockLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->uniformBlocksVS.count > GX2_MAX_UNIFORM_BLOCKS ) { OSReport("Warning : The uniform block count (%d) in VS is over %d\n", pShader->uniformBlocksVS.count, GX2_MAX_UNIFORM_BLOCKS); return FALSE; } GX2UniformBlock *block = GX2GetVertexUniformBlock(pShader->pVertexShader, name); // Get uniform block location and size pShader->uniformBlocksVS.location[pShader->uniformBlocksVS.count] = block->location; pShader->uniformBlocksVS.size[pShader->uniformBlocksVS.count] = block->size; ASSERT(DEMOGfxCheckShaderLocation(pShader->uniformBlocksVS.location[pShader->uniformBlocksVS.count])); // Increase count pShader->uniformBlocksVS.count++; return TRUE; } BOOL DEMOGfxGetPixelShaderUniformBlockLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->uniformBlocksPS.count > GX2_MAX_UNIFORM_BLOCKS ) { OSReport("Warning : The uniform block count (%d) in PS is over %d\n", pShader->uniformBlocksPS.count, GX2_MAX_UNIFORM_BLOCKS); return FALSE; } GX2UniformBlock *block = GX2GetPixelUniformBlock(pShader->pPixelShader, name); // Get uniform block location and size pShader->uniformBlocksPS.location[pShader->uniformBlocksPS.count] = block->location; pShader->uniformBlocksPS.size[pShader->uniformBlocksPS.count] = block->size; ASSERT(DEMOGfxCheckShaderLocation(pShader->uniformBlocksPS.location[pShader->uniformBlocksPS.count])); // Increase count pShader->uniformBlocksPS.count++; return TRUE; } BOOL DEMOGfxGetGeometryShaderUniformBlockLocation(DEMOGfxShader *pShader, const char *name) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } if(pShader->uniformBlocksGS.count > GX2_MAX_UNIFORM_BLOCKS ) { OSReport("Warning : The uniform block count (%d) in GS is over %d\n", pShader->uniformBlocksGS.count, GX2_MAX_UNIFORM_BLOCKS); return FALSE; } GX2UniformBlock *block = GX2GetGeometryUniformBlock(pShader->pGeometryShader, name); // Get uniform block location and size pShader->uniformBlocksGS.location[pShader->uniformBlocksGS.count] = block->location; pShader->uniformBlocksGS.size[pShader->uniformBlocksGS.count] = block->size; ASSERT(DEMOGfxCheckShaderLocation(pShader->uniformBlocksGS.location[pShader->uniformBlocksGS.count])); // Increase count pShader->uniformBlocksGS.count++; return TRUE; } BOOL DEMOGfxInitFetchShader(DEMOGfxShader *pShader) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } // Allocate memory for the fetch shader based on the number of attributes // for the vertex shader. u32 shaderSize = GX2CalcFetchShaderSize(pShader->attribCount); // size in bytes pShader->pFetchShaderBuffer = DEMOGfxAllocMEM2(shaderSize, GX2_SHADER_ALIGNMENT); // Initialize the fetch shader. This will construct the fetch shader to // use later in a call to GX2SetShaders. GX2InitFetchShader(&pShader->fetchShader, pShader->pFetchShaderBuffer, pShader->attribCount, pShader->attribs); ASSERT(pShader->fetchShader.shaderSize == shaderSize); GX2Invalidate(GX2_INVALIDATE_CPU_SHADER, pShader->pFetchShaderBuffer, shaderSize); return TRUE; } BOOL DEMOGfxFreeShaders(DEMOGfxShader *pShader) { if(!pShader) { OSReport("Error : Null pointer.\n"); return FALSE; } // Free memory for the fetch shader if(pShader->pFetchShaderBuffer) DEMOGfxFreeMEM2(pShader->pFetchShaderBuffer); if(pShader->pVertexShader) DEMOGFDFreeVertexShader(pShader->pVertexShader); if(pShader->pPixelShader) DEMOGFDFreePixelShader(pShader->pPixelShader); if(pShader->pGeometryShader) DEMOGFDFreeGeometryShader(pShader->pGeometryShader); return TRUE; }