diff options
author | David Li <davidxli@google.com> | 2011-02-01 16:40:49 -0800 |
---|---|---|
committer | David Li <davidxli@google.com> | 2011-02-01 16:40:49 -0800 |
commit | d274f94df69a016386195efcf0640802c7d7d2dc (patch) | |
tree | 1e1689c4162bdd090d395808ea4c288728edeada | |
parent | c0025eb1a3d421c0355a21db9d8ea2bd81278460 (diff) |
Checkpoint on refactoring shader functions into pixelfinger2.
Signed-off-by: David Li <davidxli@google.com>
-rw-r--r-- | include/pixelflinger2/pixelflinger2_interface.h | 13 | ||||
-rw-r--r-- | src/glsl/linker.cpp | 5 | ||||
-rw-r--r-- | src/glsl/main.cpp | 167 | ||||
-rw-r--r-- | src/mesa/main/mtypes.h | 1 | ||||
-rw-r--r-- | src/mesa/program/prog_parameter.cpp | 4 | ||||
-rw-r--r-- | src/pixelflinger2/pixelflinger2.h | 2 | ||||
-rw-r--r-- | src/pixelflinger2/shader.cpp | 449 |
7 files changed, 340 insertions, 301 deletions
diff --git a/include/pixelflinger2/pixelflinger2_interface.h b/include/pixelflinger2/pixelflinger2_interface.h index 47a78e0..0be62a6 100644 --- a/include/pixelflinger2/pixelflinger2_interface.h +++ b/include/pixelflinger2/pixelflinger2_interface.h @@ -148,15 +148,22 @@ struct GGLInterface { GLboolean (* ShaderCompile)(const GGLInterface_t * iface, gl_shader_t * shader, const char * glsl, char ** infoLog); // could be used after link if original shaders will not be linked in another program - void (* ShaderFree)(const GGLInterface_t * iface, gl_shader_t * shader); + void (* ShaderDelete)(const GGLInterface_t * iface, gl_shader_t * shader); // creates empty program gl_shader_program_t * (* ShaderProgramCreate)(const GGLInterface_t * iface); + + // attaches a shader to program + void (* ShaderAttach)(const GGLInterface * iface, gl_shader_program_t * program, gl_shader_t * shader); + + // detaches a shader from program + void (* ShaderDetach)(const GGLInterface * iface, gl_shader_program_t * program, gl_shader_t * shader); + // duplicates shaders to program, and links varyings / attributes; can link 1 shader GLboolean (* ShaderProgramLink)(const GGLInterface_t * iface, gl_shader_program_t * program, - const unsigned count, gl_shader_t ** shaders, char ** infoLog); + char ** infoLog); // frees program - void (* ShaderProgramFree)(const GGLInterface_t * iface, gl_shader_program_t * program); + void (* ShaderProgramDelete)(const GGLInterface_t * iface, gl_shader_program_t * program); // LLVM JIT and set as active program void (* ShaderUse)(GGLInterface_t * iface, gl_shader_program_t * program); diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp index 2508b36..18e5154 100644 --- a/src/glsl/linker.cpp +++ b/src/glsl/linker.cpp @@ -835,7 +835,8 @@ link_intrastage_shaders(void *mem_ctx, return NULL; } - gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); + gl_shader *linked = ctx->Driver.NewShader(ctx, 0, main->Type); + hieralloc_steal(prog, linked); linked->ir = new(linked) exec_list; clone_ir_list(mem_ctx, linked->ir, main->ir); @@ -1720,7 +1721,7 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) assert(0); } } - + //prog->InputOuputBase = malloc(1024 * 8); //memset(prog->InputOuputBase, 0xdd, 1024 * 8); prog->InputOuputBase = hieralloc_realloc(prog, prog->InputOuputBase, char, diff --git a/src/glsl/main.cpp b/src/glsl/main.cpp index 3d04b8e..2b06280 100644 --- a/src/glsl/main.cpp +++ b/src/glsl/main.cpp @@ -43,74 +43,6 @@ GGLInterface * ggl = NULL; -extern "C" struct gl_shader * -_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type); - -extern "C" void -_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr, - struct gl_shader *sh); - -/* Copied from shader_api.c for the stand-alone compiler. - */ -void -_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr, - struct gl_shader *sh) -{ - *ptr = sh; -} - -struct gl_shader * -_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type) -{ - struct gl_shader *shader; - - (void) ctx; - - assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER); - shader = hieralloc_zero(NULL, struct gl_shader); - if (shader) { - shader->Type = type; - shader->Name = name; - shader->RefCount = 1; - } - return shader; -} - -static void -initialize_context(struct gl_context *ctx, gl_api api) -{ - memset(ctx, 0, sizeof(*ctx)); - - ctx->API = api; - - ctx->Extensions.ARB_draw_buffers = GL_TRUE; - ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE; - ctx->Extensions.EXT_texture_array = GL_TRUE; - ctx->Extensions.NV_texture_rectangle = GL_TRUE; - - /* 1.10 minimums. */ - ctx->Const.MaxLights = 8; - ctx->Const.MaxClipPlanes = 8; - ctx->Const.MaxTextureUnits = 2; - - /* More than the 1.10 minimum to appease parser tests taken from - * apps that (hopefully) already checked the number of coords. - */ - ctx->Const.MaxTextureCoordUnits = 4; - - ctx->Const.VertexProgram.MaxAttribs = 16; - ctx->Const.VertexProgram.MaxUniformComponents = 512; - ctx->Const.MaxVarying = 8; - ctx->Const.MaxVertexTextureImageUnits = 0; - ctx->Const.MaxCombinedTextureImageUnits = 2; - ctx->Const.MaxTextureImageUnits = 2; - ctx->Const.FragmentProgram.MaxUniformComponents = 64; - - ctx->Const.MaxDrawBuffers = 2; - - ctx->Driver.NewShader = _mesa_new_shader; -} - /* Returned string will have 'ctx' as its hieralloc owner. */ static char * load_text_file(void *ctx, const char *file_name) @@ -188,7 +120,7 @@ usage_fail(const char *name) } -void +extern "C" void compile_shader(struct gl_context *ctx, struct gl_shader *shader) { struct _mesa_glsl_parse_state *state = @@ -564,9 +496,6 @@ main(int argc, char **argv) } //*/ int status = EXIT_SUCCESS; - struct gl_context local_ctx; - struct gl_context *ctx = &local_ctx; - int c; int idx = 0; while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) @@ -576,78 +505,68 @@ main(int argc, char **argv) if (argc <= optind) usage_fail(argv[0]); - initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL); + //initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL); ggl = CreateGGLInterface(); - - struct gl_shader_program *whole_program; - - whole_program = hieralloc_zero (NULL, struct gl_shader_program); - assert(whole_program != NULL); - whole_program->Attributes = hieralloc_zero(whole_program, gl_program_parameter_list); - whole_program->Varying = hieralloc_zero(whole_program, gl_program_parameter_list); + gl_context * ctx = ((GGLContext *)ggl)->glCtx; + struct gl_shader_program * program = ggl->ShaderProgramCreate(ggl); for (/* empty */; argc > optind; optind++) { - whole_program->Shaders = (struct gl_shader **) - hieralloc_realloc(whole_program, whole_program->Shaders, - struct gl_shader *, whole_program->NumShaders + 1); - assert(whole_program->Shaders != NULL); - - struct gl_shader *shader = hieralloc_zero(whole_program, gl_shader); - - whole_program->Shaders[whole_program->NumShaders] = shader; - whole_program->NumShaders++; - const unsigned len = strlen(argv[optind]); if (len < 6) - usage_fail(argv[0]); + usage_fail(argv[0]); const char *const ext = & argv[optind][len - 5]; + GLenum Type; if (strncmp(".vert", ext, 5) == 0) - shader->Type = GL_VERTEX_SHADER; + Type = GL_VERTEX_SHADER; else if (strncmp(".geom", ext, 5) == 0) - shader->Type = GL_GEOMETRY_SHADER; + Type = GL_GEOMETRY_SHADER; else if (strncmp(".frag", ext, 5) == 0) - shader->Type = GL_FRAGMENT_SHADER; + Type = GL_FRAGMENT_SHADER; else - usage_fail(argv[0]); + usage_fail(argv[0]); + + struct gl_shader * shader = ggl->ShaderCreate(ggl, Type); - shader->Source = load_text_file(whole_program, argv[optind]); - if (shader->Source == NULL) { - printf("File \"%s\" does not exist.\n", argv[optind]); - exit(EXIT_FAILURE); + + char * source = load_text_file(program, argv[optind]); + if (source == NULL) { + printf("File \"%s\" does not exist.\n", argv[optind]); + exit(EXIT_FAILURE); } - compile_shader(ctx, shader); - - if (!shader->CompileStatus) { - printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog); - status = EXIT_FAILURE; - break; + char * infoLog = NULL; + if (!ggl->ShaderCompile(ggl, shader, source, &infoLog)) { + printf("Info log for %s:\n%s\n", argv[optind], infoLog); + status = EXIT_FAILURE; + break; } + hieralloc_free(source); + ggl->ShaderAttach(ggl, program, shader); } puts("link"); if ((status == EXIT_SUCCESS) && do_link) { - link_shaders(ctx, whole_program); - status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; - - if (strlen(whole_program->InfoLog) > 0) - printf("Info log for linking:\n%s\n", whole_program->InfoLog); + ggl->ShaderProgramLink(ggl, program, NULL); + status = (program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; + assert(program->LinkStatus); + if (strlen(program->InfoLog) > 0) + printf("Info log for linking:\n%s\n", program->InfoLog); } - for (unsigned i = 0; i < whole_program->Attributes->NumParameters; i++) + for (unsigned i = 0; i < program->Attributes->NumParameters; i++) { - const gl_program_parameter & attribute = whole_program->Attributes->Parameters[i]; + const gl_program_parameter & attribute = program->Attributes->Parameters[i]; printf("attribute '%s': location=%d slots=%d \n", attribute.Name, attribute.Location, attribute.Slots); } - for (unsigned i = 0; i < whole_program->Varying->NumParameters; i++) + for (unsigned i = 0; i < program->Varying->NumParameters; i++) { - const gl_program_parameter & varying = whole_program->Varying->Parameters[i]; + const gl_program_parameter & varying = program->Varying->Parameters[i]; printf("varying '%s': vs_location=%d fs_location=%d \n", varying.Name, varying.BindLocation, varying.Location); } - for (unsigned i = 0; i < whole_program->Uniforms->NumUniforms; i++) + for (unsigned i = 0; i < program->Uniforms->NumUniforms; i++) { - const gl_uniform & uniform = whole_program->Uniforms->Uniforms[i]; + const gl_uniform & uniform = program->Uniforms->Uniforms[i]; printf("uniform '%s': location=%d type=%s \n", uniform.Name, uniform.Pos, uniform.Type->name); } @@ -667,7 +586,7 @@ main(int argc, char **argv) GGLTexture cubeTexture = {GL_TEXTURE_CUBE_MAP, GGL_PIXEL_FORMAT_RGBA_8888, 1, 1, 1, cubeTextureSurface, 1, 2, 1, 1}; for (unsigned i = 0; do_jit && i < MESA_SHADER_TYPES; i++) { - struct gl_shader *shader = whole_program->_LinkedShaders[i]; + struct gl_shader *shader = program->_LinkedShaders[i]; if (!shader) continue; ir_variable * sampler = NULL; @@ -688,21 +607,17 @@ main(int argc, char **argv) shader->module = module; puts("\n *** Module for JIT *** \n"); //module->dump(); - jit(shader, whole_program, (GGLContext *)ggl); + jit(shader, program, (GGLContext *)ggl); puts("jitted"); } free(texture.levels); - DestroyGGLInterface((GGLInterface *)ggl); - for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) - hieralloc_free(whole_program->_LinkedShaders[i]); - - hieralloc_free(whole_program); - - _mesa_glsl_release_types(); - _mesa_glsl_release_functions(); + + ggl->ShaderProgramDelete(ggl, program); + DestroyGGLInterface((GGLInterface *)ggl); + printf("mesa exit(%d) \n", status); hieralloc_report_brief(NULL, stdout); return status; diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h index 3012396..095835c 100644 --- a/src/mesa/main/mtypes.h +++ b/src/mesa/main/mtypes.h @@ -3072,6 +3072,7 @@ struct gl_context /** State possibly shared with other contexts in the address space */ // struct gl_shared_state *Shared; + struct gl_shader_program * CurrentProgram; /**< currently active for rendering ř*/ /** \name API function pointer tables */ /*@{*/ gl_api API; diff --git a/src/mesa/program/prog_parameter.cpp b/src/mesa/program/prog_parameter.cpp index fef639a..f3526a9 100644 --- a/src/mesa/program/prog_parameter.cpp +++ b/src/mesa/program/prog_parameter.cpp @@ -8,6 +8,10 @@ typedef unsigned int size_t; extern GLint _mesa_add_parameter(struct gl_program_parameter_list * paramList, const char * name) { + int index = _mesa_get_parameter(paramList, name); + if (index >= 0) + return index; + paramList->NumParameters++; if (paramList->NumParameters > paramList->Size) { paramList->Size = paramList->NumParameters + 4; diff --git a/src/pixelflinger2/pixelflinger2.h b/src/pixelflinger2/pixelflinger2.h index e78890b..54e7a29 100644 --- a/src/pixelflinger2/pixelflinger2.h +++ b/src/pixelflinger2/pixelflinger2.h @@ -56,7 +56,7 @@ struct GGLContext GGLSurface depthSurface; GGLSurface stencilSurface; - struct __GLcontextRec * glCtx; // mesa constants and others used for shader compiling and executing + struct gl_context * glCtx; // hieralloc; mesa constants and others used for shader compiling and executing llvm::LLVMContext * llvmCtx; struct diff --git a/src/pixelflinger2/shader.cpp b/src/pixelflinger2/shader.cpp index e201096..ef171f2 100644 --- a/src/pixelflinger2/shader.cpp +++ b/src/pixelflinger2/shader.cpp @@ -17,74 +17,151 @@ #include <assert.h> #include <stdio.h> +#include <string.h> #include <llvm/LLVMContext.h> +#include "src/talloc/hieralloc.h" +#include "src/mesa/main/mtypes.h" +#include "src/mesa/program/prog_parameter.h" +#include "src/mesa/program/prog_uniform.h" +#include "src/glsl/glsl_types.h" +#include "src/glsl/ir.h" + +extern void link_shaders(struct gl_context *ctx, struct gl_shader_program *prog); + +extern "C" void _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr, + struct gl_shader *sh) +{ + *ptr = sh; +} + +extern "C" gl_shader * _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type) +{ + struct gl_shader *shader; + assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER); + shader = hieralloc_zero(ctx, struct gl_shader); + if (shader) { + shader->Type = type; + shader->Name = name; + shader->RefCount = 1; + } + return shader; +} + +extern "C" void _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *shader) +{ + if (!shader) + return; + if (1 == shader->RefCount) + hieralloc_free(shader); + else + shader->DeletePending = true; +} + static gl_shader * ShaderCreate(const GGLInterface * iface, GLenum type) { - if (GL_VERTEX_SHADER != type && GL_FRAGMENT_SHADER != type) - { - gglError(GL_INVALID_ENUM); - return NULL; - } -// gl_shader * shader = _mesa_new_shader(0, type); -// if(!shader) -// gglError(GL_OUT_OF_MEMORY); -// return shader; - return NULL; + GGL_GET_CONST_CONTEXT(ctx, iface); + if (GL_VERTEX_SHADER != type && GL_FRAGMENT_SHADER != type) { + gglError(GL_INVALID_ENUM); + return NULL; + } + gl_shader * shader = _mesa_new_shader(ctx->glCtx, 0, type); + if (!shader) + gglError(GL_OUT_OF_MEMORY); + assert(1 == shader->RefCount); + return shader; } +extern "C" void compile_shader(struct gl_context *ctx, struct gl_shader *shader); + static GLboolean ShaderCompile(const GGLInterface * iface, gl_shader * shader, const char * glsl, char ** infoLog) { - GGL_GET_CONST_CONTEXT(ctx, iface); - if (!glsl) - { - gglError(GL_INVALID_VALUE); - return GL_FALSE; - } -// shader->Source = glsl; -// _slang_compile(ctx->glCtx, shader); -// shader->Source = NULL; -// if (infoLog) -// *infoLog = shader->InfoLog; -// return shader->CompileStatus; - return GL_FALSE; + GGL_GET_CONST_CONTEXT(ctx, iface); + if (!glsl) { + gglError(GL_INVALID_VALUE); + return GL_FALSE; + } + shader->Source = glsl; + compile_shader(ctx->glCtx, shader); + shader->Source = NULL; + if (infoLog) + *infoLog = shader->InfoLog; + return shader->CompileStatus; } -static void ShaderFree(const GGLInterface * iface, gl_shader * shader) +static void ShaderDelete(const GGLInterface * iface, gl_shader * shader) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// _mesa_free_shader(ctx->glCtx, shader); + GGL_GET_CONST_CONTEXT(ctx, iface); + _mesa_delete_shader(ctx->glCtx, shader); } static gl_shader_program * ShaderProgramCreate(const GGLInterface * iface) { -// gl_shader_program * program = _mesa_new_shader_program(0); -// if (!program) -// gglError(GL_OUT_OF_MEMORY); -// return program; - return NULL; + GGL_GET_CONST_CONTEXT(ctx, iface); + gl_shader_program * program = hieralloc_zero(ctx->glCtx, struct gl_shader_program); + if (!program) { + gglError(GL_OUT_OF_MEMORY); + return NULL; + } + program->Attributes = hieralloc_zero(program, gl_program_parameter_list); + if (!program->Attributes) { + hieralloc_free(program); + gglError(GL_OUT_OF_MEMORY); + return NULL; + } + program->Varying = hieralloc_zero(program, gl_program_parameter_list); + if (!program->Varying) { + hieralloc_free(program); + gglError(GL_OUT_OF_MEMORY); + return NULL; + } + return program; +} + +static void ShaderAttach(const GGLInterface * iface, gl_shader_program * program, + gl_shader * shader) +{ + for (unsigned i = 0; i < program->NumShaders; i++) + if (program->Shaders[i]->Type == shader->Type || program->Shaders[i] == shader) + return gglError(GL_INVALID_OPERATION); + + program->Shaders = (gl_shader **)hieralloc_realloc + (program, program->Shaders, gl_shader *, program->NumShaders + 1); + if (!program->Shaders) { + gglError(GL_OUT_OF_MEMORY); + assert(0); + return; + } + program->Shaders[program->NumShaders] = shader; + program->NumShaders++; + shader->RefCount++; +} + +static void ShaderDetach(const GGLInterface * iface, gl_shader_program * program, + gl_shader * shader) +{ + for (unsigned i = 0; i < program->NumShaders; i++) + if (program->Shaders[i] == shader) { + program->NumShaders--; + program->Shaders[i] = program->Shaders[program->NumShaders]; + shader->RefCount--; + if (1 == shader->RefCount && shader->DeletePending) + iface->ShaderDelete(iface, shader); + return; + } + gglError(GL_INVALID_OPERATION); } static GLboolean ShaderProgramLink(const GGLInterface * iface, gl_shader_program * program, - const unsigned count, gl_shader ** shaders, char ** infoLog) -{ - GGL_GET_CONST_CONTEXT(ctx, iface); - -// program->NumShaders = count; -// program->Shaders = shaders; -// -// _slang_link(ctx->glCtx, program); -// -// program->NumShaders = 0; -// program->Shaders = NULL; -// -// if (infoLog) -// *infoLog = program->InfoLog; -// -// return program->LinkStatus; - return GL_FALSE; + char ** infoLog) +{ + GGL_GET_CONST_CONTEXT(ctx, iface); + link_shaders(ctx->glCtx, program); + if (infoLog) + *infoLog = program->InfoLog; + return program->LinkStatus; } struct gl_program; @@ -100,7 +177,7 @@ static void GetShaderKey(const GGLContext * ctx, const gl_program * shader, Shad // key->scanLineKey.bufferState = ctx->bufferState; // key->scanLineKey.blendState = ctx->blendState; // } -// +// // for (unsigned i = 0; i < GGL_MAXCOMBINEDTEXTUREIMAGEUNITS; i++) // if (shader->SamplersUsed & (1 << i)) // { @@ -117,9 +194,9 @@ static void GetShaderKey(const GGLContext * ctx, const gl_program * shader, Shad // } } -static inline char HexDigit(unsigned char d) +static inline char HexDigit(unsigned char d) { - return (d > 9 ? d + 'A' - 10 : d + '0'); + return (d > 9 ? d + 'A' - 10 : d + '0'); } static const unsigned SHADER_KEY_STRING_LEN = GGL_MAXCOMBINEDTEXTUREIMAGEUNITS * 4 + 2; @@ -150,7 +227,7 @@ static void GetShaderKeyString(const GLenum type, const ShaderKey * key, //static const unsigned SCANLINE_KEY_STRING_LEN = 2 * sizeof(((ShaderKey *)0)->scanLineKey) + // 3 + SHADER_KEY_STRING_LEN; -static char * GetScanlineKeyString(const ShaderKey * key, char * buffer, +static char * GetScanlineKeyString(const ShaderKey * key, char * buffer, const unsigned bufferSize) { // assert(1 == sizeof(char)); @@ -172,16 +249,15 @@ static char * GetScanlineKeyString(const ShaderKey * key, char * buffer, static void ShaderUse(GGLInterface * iface, gl_shader_program * program) { - GGL_GET_CONST_CONTEXT(ctx, iface); - assert(program); - if (!program) - { -// ctx->glCtx->Shader.CurrentProgram = NULL; - // so drawing calls will do nothing until ShaderUse with a program - SetShaderVerifyFunctions(iface); - return; - } - + GGL_GET_CONST_CONTEXT(ctx, iface); + assert(program); + if (!program) { + ctx->glCtx->CurrentProgram = NULL; + // so drawing calls will do nothing until ShaderUse with a program + SetShaderVerifyFunctions(iface); + return; + } + // if (program->VertexProgram) // { // if (!program->STVP) @@ -190,9 +266,9 @@ static void ShaderUse(GGLInterface * iface, gl_shader_program * program) // program->STVP->Base = *program->VertexProgram; // st_translate_vertex_program(ctx->glCtx, program->STVP, NULL, NULL, NULL); // } -// +// // _mesa_update_shader_textures_used(program->VertexProgram); -// +// // ShaderKey shaderKey; // GetShaderKey(ctx, program->VertexProgram, &shaderKey); // ShaderFunction_t function = NULL; @@ -201,7 +277,7 @@ static void ShaderUse(GGLInterface * iface, gl_shader_program * program) // char shaderName [SHADER_KEY_STRING_LEN] = {0}; // GetShaderKeyString(GL_VERTEX_SHADER, &shaderKey, shaderName, Elements(shaderName)); // create_program(program->STVP->state.tokens, GALLIVM_VS, &program->GLVMVP, -// &ctx->glCtx->Shader.cpu, ctx, program->VertexProgram, +// &ctx->glCtx->Shader.cpu, ctx, program->VertexProgram, // shaderName, NULL); // program->GLVMVP->functions[shaderKey] = program->GLVMVP->function; // debug_printf("jit new vertex shader %p \n", program->GLVMVP->function); //getchar(); @@ -221,9 +297,9 @@ static void ShaderUse(GGLInterface * iface, gl_shader_program * program) // program->STFP->Base = *program->FragmentProgram; // st_translate_fragment_program(ctx->glCtx, program->STFP, NULL); // } -// +// // _mesa_update_shader_textures_used(program->FragmentProgram); -// +// // ShaderKey shaderKey; // GetShaderKey(ctx, program->FragmentProgram, &shaderKey); // ShaderFunction_t function = NULL; @@ -231,7 +307,7 @@ static void ShaderUse(GGLInterface * iface, gl_shader_program * program) // { // char shaderName [SHADER_KEY_STRING_LEN] = {0}; // GetShaderKeyString(GL_FRAGMENT_SHADER, &shaderKey, shaderName, Elements(shaderName)); -// +// // char scanlineName [SCANLINE_KEY_STRING_LEN] = {0}; // GetScanlineKeyString(&shaderKey, scanlineName, Elements(scanlineName)); // create_program(program->STFP->state.tokens, GALLIVM_FS, &program->GLVMFP, @@ -247,48 +323,51 @@ static void ShaderUse(GGLInterface * iface, gl_shader_program * program) // } // ctx->PickScanLine(iface); // } -// ctx->glCtx->Shader.CurrentProgram = program; +// ctx->glCtx->CurrentProgram = program; } -static void ShaderProgramFree(const GGLInterface * iface, gl_shader_program * program) +static void ShaderProgramDelete(const GGLInterface * iface, gl_shader_program * program) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.CurrentProgram == program) -// { -// ctx->glCtx->Shader.CurrentProgram = NULL; -// SetShaderVerifyFunctions(const_cast<GGLInterface *>(iface)); -// } -// assert(program); -// if (program->GLVMVP) -// gallivm_prog_delete(ctx->glCtx->Shader.cpu, program->GLVMVP); -// program->GLVMVP = NULL; -// if (program->GLVMFP) -// gallivm_prog_delete(ctx->glCtx->Shader.cpu, program->GLVMFP); -// program->GLVMFP = NULL; -// SAFE_FREE(program->STVP); -// SAFE_FREE(program->STFP); -// _mesa_free_shader_program(ctx->glCtx, program); + GGL_GET_CONST_CONTEXT(ctx, iface); + if (ctx->glCtx->CurrentProgram == program) { + ctx->glCtx->CurrentProgram = NULL; + SetShaderVerifyFunctions(const_cast<GGLInterface *>(iface)); + } + + for (unsigned i = 0; i < program->NumShaders; i++) { + iface->ShaderDelete(iface, program->Shaders[i]); + iface->ShaderDetach(iface, program, program->Shaders[i]); + } + + for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) + iface->ShaderDelete(iface, program->_LinkedShaders[i]); } -static void ShaderAttributeBind(const GGLInterface * iface, const gl_shader_program * program, - GLuint index, const GLchar * name) +static void ShaderAttributeBind(const GGLInterface * iface, const gl_shader_program * program, + GLuint index, const GLchar * name) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// _mesa_bind_attrib_location(ctx->glCtx, program, index, name, DEFAULTP); + GGL_GET_CONST_CONTEXT(ctx, iface); + int i = _mesa_add_parameter(program->Attributes, name); + program->Attributes->Parameters[i].BindLocation = index; } static GLint ShaderAttributeLocation(const GGLInterface * iface, const gl_shader_program * program, const char * name) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// return _mesa_get_attrib_location(ctx->glCtx, program, name); + GGL_GET_CONST_CONTEXT(ctx, iface); + int i = _mesa_get_parameter(program->Attributes, name); + if (i >= 0) + return program->Attributes->Parameters[i].Location; return -2; } static GLint ShaderUniformLocation(const GGLInterface * iface, const gl_shader_program * program, const char * name) { - GGL_GET_CONST_CONTEXT(ctx, iface); + GGL_GET_CONST_CONTEXT(ctx, iface); + for (unsigned i = 0; i < program->Uniforms->NumUniforms; i++) + if (!strcmp(program->Uniforms->Uniforms[i].Name, name)) + return program->Uniforms->Uniforms[i].Pos; // return _mesa_get_shader_uniform_location(ctx->glCtx, program, name); return -2; } @@ -296,23 +375,25 @@ static GLint ShaderUniformLocation(const GGLInterface * iface, const gl_shader_p static void ShaderUniformGetfv(const GGLInterface * iface, gl_shader_program * program, GLint location, GLfloat * params) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// _mesa_get_uniformfv(ctx->glCtx, program, location, params); + GGL_GET_CONST_CONTEXT(ctx, iface); + memcpy(params, program->ValuesUniform + location, sizeof(*program->ValuesUniform)); } static void ShaderUniformGetiv(const GGLInterface * iface, gl_shader_program * program, GLint location, GLint * params) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// _mesa_get_uniformiv(ctx->glCtx, program, location, params); + // TODO: sampler uniform + GGL_GET_CONST_CONTEXT(ctx, iface); + memcpy(params, program->ValuesUniform + location, sizeof(*program->ValuesUniform)); } static GLint ShaderUniform(const GGLInterface * iface, gl_shader_program * program, - GLint location, GLsizei count, const GLvoid *values, GLenum type) + GLint location, GLsizei count, const GLvoid *values, GLenum type) { - GGL_GET_CONST_CONTEXT(ctx, iface); + // TODO: sampler uniform + GGL_GET_CONST_CONTEXT(ctx, iface); // if (!program) -// { +// { // gglError(GL_INVALID_OPERATION); // return -2; // } @@ -324,7 +405,7 @@ static void ShaderUniformMatrix(const GGLInterface * iface, gl_shader_program * GLint cols, GLint rows, GLint location, GLsizei count, GLboolean transpose, const GLfloat *values) { - GGL_GET_CONST_CONTEXT(ctx, iface); + GGL_GET_CONST_CONTEXT(ctx, iface); // if (!program) // return gglError(GL_INVALID_OPERATION); // _mesa_uniform_matrix(ctx->glCtx, program, cols, rows, location, count, transpose, values); @@ -333,102 +414,132 @@ static void ShaderUniformMatrix(const GGLInterface * iface, gl_shader_program * static void ShaderVerifyProcessVertex(const GGLInterface * iface, const VertexInput * input, VertexOutput * output) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.CurrentProgram) -// { -// ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->Shader.CurrentProgram); -// if (ShaderVerifyProcessVertex != iface->ProcessVertex) -// iface->ProcessVertex(iface, input, output); -// } + GGL_GET_CONST_CONTEXT(ctx, iface); + if (ctx->glCtx->CurrentProgram) { + ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->CurrentProgram); + if (ShaderVerifyProcessVertex != iface->ProcessVertex) + iface->ProcessVertex(iface, input, output); + } } -static void ShaderVerifyDrawTriangle(const GGLInterface * iface, const VertexInput * v0, +static void ShaderVerifyDrawTriangle(const GGLInterface * iface, const VertexInput * v0, const VertexInput * v1, const VertexInput * v2) { -// GGL_GET_CONST_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.CurrentProgram) -// { -// ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->Shader.CurrentProgram); -// if (ShaderVerifyDrawTriangle != iface->DrawTriangle) -// iface->DrawTriangle(iface, v0, v1, v2); -// } + GGL_GET_CONST_CONTEXT(ctx, iface); + if (ctx->glCtx->CurrentProgram) { + ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->CurrentProgram); + if (ShaderVerifyDrawTriangle != iface->DrawTriangle) + iface->DrawTriangle(iface, v0, v1, v2); + } } -static void ShaderVerifyRasterTriangle(const GGLInterface * iface, const VertexOutput * v1, +static void ShaderVerifyRasterTriangle(const GGLInterface * iface, const VertexOutput * v1, const VertexOutput * v2, const VertexOutput * v3) { -// GGL_GET_CONST_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.CurrentProgram) -// { -// ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->Shader.CurrentProgram); -// if (ShaderVerifyRasterTriangle != iface->RasterTriangle) -// iface->RasterTriangle(iface, v1, v2, v3); -// } + GGL_GET_CONST_CONTEXT(ctx, iface); + if (ctx->glCtx->CurrentProgram) { + ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->CurrentProgram); + if (ShaderVerifyRasterTriangle != iface->RasterTriangle) + iface->RasterTriangle(iface, v1, v2, v3); + } } -static void ShaderVerifyRasterTrapezoid(const GGLInterface * iface, const VertexOutput * tl, +static void ShaderVerifyRasterTrapezoid(const GGLInterface * iface, const VertexOutput * tl, const VertexOutput * tr, const VertexOutput * bl, const VertexOutput * br) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.CurrentProgram) -// { -// ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->Shader.CurrentProgram); -// if (ShaderVerifyRasterTrapezoid != iface->RasterTrapezoid) -// iface->RasterTrapezoid(iface, tl, tr, bl, br); -// } + GGL_GET_CONST_CONTEXT(ctx, iface); + if (ctx->glCtx->CurrentProgram) { + ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->CurrentProgram); + if (ShaderVerifyRasterTrapezoid != iface->RasterTrapezoid) + iface->RasterTrapezoid(iface, tl, tr, bl, br); + } } static void ShaderVerifyScanLine(const GGLInterface * iface, const VertexOutput * v1, const VertexOutput * v2) { - GGL_GET_CONST_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.CurrentProgram) -// { -// ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->Shader.CurrentProgram); -// if (ShaderVerifyScanLine != iface->ScanLine) -// iface->ScanLine(iface, v1, v2); -// } + GGL_GET_CONST_CONTEXT(ctx, iface); + if (ctx->glCtx->CurrentProgram) { + ShaderUse(const_cast<GGLInterface *>(iface), ctx->glCtx->CurrentProgram); + if (ShaderVerifyScanLine != iface->ScanLine) + iface->ScanLine(iface, v1, v2); + } } // called after state changes so that drawing calls will trigger JIT void SetShaderVerifyFunctions(struct GGLInterface * iface) { - iface->ProcessVertex = ShaderVerifyProcessVertex; - iface->DrawTriangle = ShaderVerifyDrawTriangle; - iface->RasterTriangle = ShaderVerifyRasterTriangle; - iface->RasterTrapezoid = ShaderVerifyRasterTrapezoid; - iface->ScanLine = ShaderVerifyScanLine; + iface->ProcessVertex = ShaderVerifyProcessVertex; + iface->DrawTriangle = ShaderVerifyDrawTriangle; + iface->RasterTriangle = ShaderVerifyRasterTriangle; + iface->RasterTrapezoid = ShaderVerifyRasterTrapezoid; + iface->ScanLine = ShaderVerifyScanLine; +} + +static void InitializeGLContext(struct gl_context *ctx) +{ + memset(ctx, 0, sizeof(*ctx)); + ctx->API = API_OPENGLES2; + ctx->Extensions.ARB_draw_buffers = GL_TRUE; + ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE; + ctx->Extensions.EXT_texture_array = GL_TRUE; + ctx->Extensions.NV_texture_rectangle = GL_TRUE; + + /* 1.10 minimums. */ + ctx->Const.MaxLights = 8; + ctx->Const.MaxClipPlanes = 8; + ctx->Const.MaxTextureUnits = 2; + + /* More than the 1.10 minimum to appease parser tests taken from + * apps that (hopefully) already checked the number of coords. + */ + ctx->Const.MaxTextureCoordUnits = 4; + + ctx->Const.VertexProgram.MaxAttribs = 16; + ctx->Const.VertexProgram.MaxUniformComponents = 512; + ctx->Const.MaxVarying = 8; + ctx->Const.MaxVertexTextureImageUnits = 0; + ctx->Const.MaxCombinedTextureImageUnits = 2; + ctx->Const.MaxTextureImageUnits = 2; + ctx->Const.FragmentProgram.MaxUniformComponents = 64; + + ctx->Const.MaxDrawBuffers = 2; + + ctx->Driver.NewShader = _mesa_new_shader; + ctx->Driver.DeleteShader = _mesa_delete_shader; } void InitializeShaderFunctions(struct GGLInterface * iface) { - GGL_GET_CONTEXT(ctx, iface); - ctx->llvmCtx = new llvm::LLVMContext(); - - iface->ShaderCreate = ShaderCreate; - iface->ShaderCompile = ShaderCompile; - iface->ShaderFree = ShaderFree; - iface->ShaderProgramCreate = ShaderProgramCreate; - iface->ShaderProgramLink = ShaderProgramLink; - iface->ShaderUse = ShaderUse; - iface->ShaderProgramFree = ShaderProgramFree; - iface->ShaderAttributeBind = ShaderAttributeBind; - iface->ShaderAttributeLocation = ShaderAttributeLocation; - iface->ShaderUniformLocation = ShaderUniformLocation; - iface->ShaderUniformGetfv = ShaderUniformGetfv; - iface->ShaderUniformGetiv = ShaderUniformGetiv; - iface->ShaderUniform = ShaderUniform; - iface->ShaderUniformMatrix = ShaderUniformMatrix; + GGL_GET_CONTEXT(ctx, iface); + ctx->llvmCtx = new llvm::LLVMContext(); + ctx->glCtx = hieralloc(NULL, gl_context); + InitializeGLContext(ctx->glCtx); + + iface->ShaderCreate = ShaderCreate; + iface->ShaderCompile = ShaderCompile; + iface->ShaderDelete = ShaderDelete; + iface->ShaderProgramCreate = ShaderProgramCreate; + iface->ShaderAttach = ShaderAttach; + iface->ShaderDetach = ShaderDetach; + iface->ShaderProgramLink = ShaderProgramLink; + iface->ShaderUse = ShaderUse; + iface->ShaderProgramDelete = ShaderProgramDelete; + iface->ShaderAttributeBind = ShaderAttributeBind; + iface->ShaderAttributeLocation = ShaderAttributeLocation; + iface->ShaderUniformLocation = ShaderUniformLocation; + iface->ShaderUniformGetfv = ShaderUniformGetfv; + iface->ShaderUniformGetiv = ShaderUniformGetiv; + iface->ShaderUniform = ShaderUniform; + iface->ShaderUniformMatrix = ShaderUniformMatrix; } void DestroyShaderFunctions(GGLInterface * iface) { - GGL_GET_CONTEXT(ctx, iface); -// if (ctx->glCtx->Shader.cpu) -// { -// gallivm_cpu_engine_delete(ctx->glCtx->Shader.cpu); -// ctx->glCtx->Shader.cpu = NULL; -// } -// SAFE_DELETE(ctx->llvmCtx); -}
\ No newline at end of file + GGL_GET_CONTEXT(ctx, iface); + _mesa_glsl_release_types(); + _mesa_glsl_release_functions(); + hieralloc_free(ctx->glCtx); + delete ctx->llvmCtx; +} |