void FreeShaderInfo(ShaderInfo* psShaderInfo) { //Free any default values for constants. uint32_t cbuf; for(cbuf=0; cbuf<psShaderInfo->ui32NumConstantBuffers; ++cbuf) { ConstantBuffer* psCBuf = &psShaderInfo->psConstantBuffers[cbuf]; uint32_t var; if(psCBuf->ui32NumVars) { for(var=0; var < psCBuf->ui32NumVars; ++var) { ShaderVar* psVar = &psCBuf->asVars[var]; if(psVar->haveDefaultValue) { hlslcc_free(psVar->pui32DefaultValues); } } hlslcc_free(psCBuf->asVars); } } hlslcc_free(psShaderInfo->psInputSignatures); hlslcc_free(psShaderInfo->psResourceBindings); hlslcc_free(psShaderInfo->psConstantBuffers); hlslcc_free(psShaderInfo->psClassTypes); hlslcc_free(psShaderInfo->psClassInstances); hlslcc_free(psShaderInfo->psOutputSignatures); hlslcc_free(psShaderInfo->psPatchConstantSignatures); psShaderInfo->ui32NumInputSignatures = 0; psShaderInfo->ui32NumResourceBindings = 0; psShaderInfo->ui32NumConstantBuffers = 0; psShaderInfo->ui32NumClassTypes = 0; psShaderInfo->ui32NumClassInstances = 0; psShaderInfo->ui32NumOutputSignatures = 0; psShaderInfo->ui32NumPatchConstantSignatures = 0; }
HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromFile(const char* filename, unsigned int flags, GLLang language, const GlExtensions *extensions, GLSLCrossDependencyData* dependencies, GLSLShader* result) { FILE* shaderFile; int length; size_t readLength; char* shader; int success = 0; shaderFile = fopen(filename, "rb"); if(!shaderFile) { return 0; } fseek(shaderFile, 0, SEEK_END); length = ftell(shaderFile); fseek(shaderFile, 0, SEEK_SET); shader = (char*)hlslcc_malloc(length+1); readLength = fread(shader, 1, length, shaderFile); fclose(shaderFile); shaderFile = 0; shader[readLength] = '\0'; success = TranslateHLSLFromMem(shader, flags, language, extensions, dependencies, result); hlslcc_free(shader); return success; }
static void FreeSubOperands(Instruction* psInst, const uint32_t ui32NumInsts) { uint32_t ui32Inst; for(ui32Inst = 0; ui32Inst < ui32NumInsts; ++ui32Inst) { Instruction* psCurrentInst = &psInst[ui32Inst]; const uint32_t ui32NumOperands = psCurrentInst->ui32NumOperands; uint32_t ui32Operand; for(ui32Operand = 0; ui32Operand < ui32NumOperands; ++ui32Operand) { uint32_t ui32SubOperand; for(ui32SubOperand = 0; ui32SubOperand < MAX_SUB_OPERANDS; ++ui32SubOperand) { if(psCurrentInst->asOperands[ui32Operand].psSubOperand[ui32SubOperand]) { hlslcc_free(psCurrentInst->asOperands[ui32Operand].psSubOperand[ui32SubOperand]); psCurrentInst->asOperands[ui32Operand].psSubOperand[ui32SubOperand] = NULL; } } } } }
HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromMem(const char* shader, unsigned int flags, GLLang language, const GlExtensions *extensions, GLSLCrossDependencyData* dependencies, GLSLShader* result) { uint32_t* tokens; Shader* psShader; char* glslcstr = NULL; int GLSLShaderType = GL_FRAGMENT_SHADER_ARB; int success = 0; uint32_t i; tokens = (uint32_t*)shader; psShader = DecodeDXBC(tokens); if(psShader) { HLSLCrossCompilerContext sContext; sContext.psShader = psShader; sContext.flags = flags; sContext.psDependencies = dependencies; for(i=0; i<NUM_PHASES;++i) { sContext.havePostShaderCode[i] = 0; } TranslateToGLSL(&sContext, &language,extensions); switch(psShader->eShaderType) { case VERTEX_SHADER: { GLSLShaderType = GL_VERTEX_SHADER_ARB; break; } case GEOMETRY_SHADER: { GLSLShaderType = GL_GEOMETRY_SHADER; break; } case DOMAIN_SHADER: { GLSLShaderType = GL_TESS_EVALUATION_SHADER; break; } case HULL_SHADER: { GLSLShaderType = GL_TESS_CONTROL_SHADER; break; } case COMPUTE_SHADER: { GLSLShaderType = GL_COMPUTE_SHADER; break; } default: { break; } } glslcstr = bstr2cstr(sContext.glsl, '\0'); bdestroy(sContext.glsl); bdestroy(sContext.earlyMain); for(i=0; i<NUM_PHASES; ++i) { bdestroy(sContext.postShaderCode[i]); } hlslcc_free(psShader->psHSControlPointPhaseDecl); FreeSubOperands(psShader->psHSControlPointPhaseInstr, psShader->ui32HSControlPointInstrCount); hlslcc_free(psShader->psHSControlPointPhaseInstr); for(i=0; i < psShader->ui32ForkPhaseCount; ++i) { hlslcc_free(psShader->apsHSForkPhaseDecl[i]); FreeSubOperands(psShader->apsHSForkPhaseInstr[i], psShader->aui32HSForkInstrCount[i]); hlslcc_free(psShader->apsHSForkPhaseInstr[i]); } hlslcc_free(psShader->psHSJoinPhaseDecl); FreeSubOperands(psShader->psHSJoinPhaseInstr, psShader->ui32HSJoinInstrCount); hlslcc_free(psShader->psHSJoinPhaseInstr); hlslcc_free(psShader->psDecl); FreeSubOperands(psShader->psInst, psShader->ui32InstCount); hlslcc_free(psShader->psInst); memcpy(&result->reflection,&psShader->sInfo,sizeof(psShader->sInfo)); hlslcc_free(psShader); success = 1; } shader = 0; tokens = 0; /* Fill in the result struct */ result->shaderType = GLSLShaderType; result->sourceCode = glslcstr; result->GLSLLanguage = language; return success; }
HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromMem(const char* shader, unsigned int flags, GLLang language, const GlExtensions *extensions, GLSLCrossDependencyData* dependencies, GLSLShader* result) { uint32_t* tokens; Shader* psShader; char* glslcstr = NULL; int GLSLShaderType = GL_FRAGMENT_SHADER_ARB; int success = 0; uint32_t i; tokens = (uint32_t*)shader; psShader = DecodeDXBC(tokens); if(psShader) { HLSLCrossCompilerContext sContext; if(psShader->ui32MajorVersion <= 3) { flags &= ~HLSLCC_FLAG_COMBINE_TEXTURE_SAMPLERS; } sContext.psShader = psShader; sContext.flags = flags; sContext.psDependencies = dependencies; for(i=0; i<NUM_PHASES;++i) { sContext.havePostShaderCode[i] = 0; } TranslateToGLSL(&sContext, &language,extensions); switch(psShader->eShaderType) { case VERTEX_SHADER: { GLSLShaderType = GL_VERTEX_SHADER_ARB; break; } case GEOMETRY_SHADER: { GLSLShaderType = GL_GEOMETRY_SHADER; break; } case DOMAIN_SHADER: { GLSLShaderType = GL_TESS_EVALUATION_SHADER; break; } case HULL_SHADER: { GLSLShaderType = GL_TESS_CONTROL_SHADER; break; } case COMPUTE_SHADER: { GLSLShaderType = GL_COMPUTE_SHADER; break; } default: { break; } } glslcstr = bstr2cstr(sContext.glsl, '\0'); bdestroy(sContext.glsl); bdestroy(sContext.earlyMain); for(i=0; i<NUM_PHASES; ++i) { bdestroy(sContext.postShaderCode[i]); } for(i=0; i<NUM_PHASES;++i) { if(psShader->asPhase[i].ppsDecl != 0) { uint32_t k; for(k=0; k < psShader->asPhase[i].ui32InstanceCount; ++k) { hlslcc_free(psShader->asPhase[i].ppsDecl[k]); } hlslcc_free(psShader->asPhase[i].ppsDecl); } if(psShader->asPhase[i].ppsInst != 0) { uint32_t k; for(k=0; k < psShader->asPhase[i].ui32InstanceCount; ++k) { FreeSubOperands(psShader->asPhase[i].ppsInst[k], psShader->asPhase[i].pui32InstCount[k]); hlslcc_free(psShader->asPhase[i].ppsInst[k]); } hlslcc_free(psShader->asPhase[i].ppsInst); } } memcpy(&result->reflection,&psShader->sInfo,sizeof(psShader->sInfo)); result->textureSamplerInfo.ui32NumTextureSamplerPairs = psShader->textureSamplerInfo.ui32NumTextureSamplerPairs; for (i=0; i<result->textureSamplerInfo.ui32NumTextureSamplerPairs; i++) strcpy(result->textureSamplerInfo.aTextureSamplerPair[i].Name, psShader->textureSamplerInfo.aTextureSamplerPair[i].Name); hlslcc_free(psShader); success = 1; } shader = 0; tokens = 0; /* Fill in the result struct */ result->shaderType = GLSLShaderType; result->sourceCode = glslcstr; result->GLSLLanguage = language; return success; }