mesa: implement clamping controls (ARB_color_buffer_float)
[mesa.git] / src / mesa / main / shaderobj.c
index 59198d788bd093ca3df8e55a414863e3c89e39f9..33d91ad594d1ef87ee060aef62273677067f2ec1 100644 (file)
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/hash.h"
+#include "main/mfeatures.h"
+#include "main/mtypes.h"
 #include "main/shaderobj.h"
 #include "program/program.h"
 #include "program/prog_parameter.h"
 #include "program/prog_uniform.h"
-#include "talloc.h"
+#include "ralloc.h"
 
 /**********************************************************************/
 /*** Shader object functions                                        ***/
@@ -50,7 +52,7 @@
  * Then set ptr to point to sh, incrementing its refcount.
  */
 void
-_mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
+_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
                        struct gl_shader *sh)
 {
    assert(ptr);
@@ -70,7 +72,8 @@ _mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
       deleteFlag = (old->RefCount == 0);
 
       if (deleteFlag) {
-         _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
+        if (old->Name != 0)
+           _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
          ctx->Driver.DeleteShader(ctx, old);
       }
 
@@ -88,7 +91,7 @@ _mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
 }
 
 void
-_mesa_init_shader(GLcontext *ctx, struct gl_shader *shader)
+_mesa_init_shader(struct gl_context *ctx, struct gl_shader *shader)
 {
    shader->RefCount = 1;
 }
@@ -98,12 +101,12 @@ _mesa_init_shader(GLcontext *ctx, struct gl_shader *shader)
  * Called via ctx->Driver.NewShader()
  */
 struct gl_shader *
-_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
+_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
 {
    struct gl_shader *shader;
    assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER ||
           type == GL_GEOMETRY_SHADER_ARB);
-   shader = talloc_zero(NULL, struct gl_shader);
+   shader = rzalloc(NULL, struct gl_shader);
    if (shader) {
       shader->Type = type;
       shader->Name = name;
@@ -118,12 +121,12 @@ _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
  * Called via ctx->Driver.DeleteShader().
  */
 static void
-_mesa_delete_shader(GLcontext *ctx, struct gl_shader *sh)
+_mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
 {
    if (sh->Source)
       free((void *) sh->Source);
    _mesa_reference_program(ctx, &sh->Program, NULL);
-   talloc_free(sh);
+   ralloc_free(sh);
 }
 
 
@@ -131,7 +134,7 @@ _mesa_delete_shader(GLcontext *ctx, struct gl_shader *sh)
  * Lookup a GLSL shader object.
  */
 struct gl_shader *
-_mesa_lookup_shader(GLcontext *ctx, GLuint name)
+_mesa_lookup_shader(struct gl_context *ctx, GLuint name)
 {
    if (name) {
       struct gl_shader *sh = (struct gl_shader *)
@@ -153,21 +156,21 @@ _mesa_lookup_shader(GLcontext *ctx, GLuint name)
  * As above, but record an error if shader is not found.
  */
 struct gl_shader *
-_mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
+_mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
 {
    if (!name) {
-      _mesa_error(ctx, GL_INVALID_VALUE, caller);
+      _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
       return NULL;
    }
    else {
       struct gl_shader *sh = (struct gl_shader *)
          _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
       if (!sh) {
-         _mesa_error(ctx, GL_INVALID_VALUE, caller);
+         _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
          return NULL;
       }
       if (sh->Type == GL_SHADER_PROGRAM_MESA) {
-         _mesa_error(ctx, GL_INVALID_OPERATION, caller);
+         _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
          return NULL;
       }
       return sh;
@@ -188,7 +191,7 @@ _mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
  * Then set ptr to point to shProg, incrementing its refcount.
  */
 void
-_mesa_reference_shader_program(GLcontext *ctx,
+_mesa_reference_shader_program(struct gl_context *ctx,
                                struct gl_shader_program **ptr,
                                struct gl_shader_program *shProg)
 {
@@ -211,7 +214,8 @@ _mesa_reference_shader_program(GLcontext *ctx,
       deleteFlag = (old->RefCount == 0);
 
       if (deleteFlag) {
-         _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
+        if (old->Name != 0)
+           _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
          ctx->Driver.DeleteShaderProgram(ctx, old);
       }
 
@@ -230,7 +234,7 @@ _mesa_reference_shader_program(GLcontext *ctx,
 }
 
 void
-_mesa_init_shader_program(GLcontext *ctx, struct gl_shader_program *prog)
+_mesa_init_shader_program(struct gl_context *ctx, struct gl_shader_program *prog)
 {
    prog->Type = GL_SHADER_PROGRAM_MESA;
    prog->RefCount = 1;
@@ -247,10 +251,10 @@ _mesa_init_shader_program(GLcontext *ctx, struct gl_shader_program *prog)
  * Called via ctx->Driver.NewShaderProgram()
  */
 static struct gl_shader_program *
-_mesa_new_shader_program(GLcontext *ctx, GLuint name)
+_mesa_new_shader_program(struct gl_context *ctx, GLuint name)
 {
    struct gl_shader_program *shProg;
-   shProg = talloc_zero(NULL, struct gl_shader_program);
+   shProg = rzalloc(NULL, struct gl_shader_program);
    if (shProg) {
       shProg->Name = name;
       _mesa_init_shader_program(ctx, shProg);
@@ -263,7 +267,7 @@ _mesa_new_shader_program(GLcontext *ctx, GLuint name)
  * Clear (free) the shader program state that gets produced by linking.
  */
 void
-_mesa_clear_shader_program_data(GLcontext *ctx,
+_mesa_clear_shader_program_data(struct gl_context *ctx,
                                 struct gl_shader_program *shProg)
 {
    _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
@@ -287,10 +291,11 @@ _mesa_clear_shader_program_data(GLcontext *ctx,
  * object itself.
  */
 void
-_mesa_free_shader_program_data(GLcontext *ctx,
+_mesa_free_shader_program_data(struct gl_context *ctx,
                                struct gl_shader_program *shProg)
 {
    GLuint i;
+   gl_shader_type sh;
 
    assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
 
@@ -313,7 +318,7 @@ _mesa_free_shader_program_data(GLcontext *ctx,
    }
 
    if (shProg->InfoLog) {
-      talloc_free(shProg->InfoLog);
+      ralloc_free(shProg->InfoLog);
       shProg->InfoLog = NULL;
    }
 
@@ -326,10 +331,12 @@ _mesa_free_shader_program_data(GLcontext *ctx,
    shProg->TransformFeedback.NumVarying = 0;
 
 
-   for (i = 0; i < shProg->_NumLinkedShaders; i++) {
-      ctx->Driver.DeleteShader(ctx, shProg->_LinkedShaders[i]);
+   for (sh = 0; sh < MESA_SHADER_TYPES; sh++) {
+      if (shProg->_LinkedShaders[sh] != NULL) {
+        ctx->Driver.DeleteShader(ctx, shProg->_LinkedShaders[sh]);
+        shProg->_LinkedShaders[sh] = NULL;
+      }
    }
-   shProg->_NumLinkedShaders = 0;
 }
 
 
@@ -338,11 +345,11 @@ _mesa_free_shader_program_data(GLcontext *ctx,
  * Called via ctx->Driver.DeleteShaderProgram().
  */
 static void
-_mesa_delete_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
+_mesa_delete_shader_program(struct gl_context *ctx, struct gl_shader_program *shProg)
 {
    _mesa_free_shader_program_data(ctx, shProg);
 
-   talloc_free(shProg);
+   ralloc_free(shProg);
 }
 
 
@@ -350,7 +357,7 @@ _mesa_delete_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
  * Lookup a GLSL program object.
  */
 struct gl_shader_program *
-_mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
+_mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
 {
    struct gl_shader_program *shProg;
    if (name) {
@@ -373,22 +380,22 @@ _mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
  * As above, but record an error if program is not found.
  */
 struct gl_shader_program *
-_mesa_lookup_shader_program_err(GLcontext *ctx, GLuint name,
+_mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
                                 const char *caller)
 {
    if (!name) {
-      _mesa_error(ctx, GL_INVALID_VALUE, caller);
+      _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
       return NULL;
    }
    else {
       struct gl_shader_program *shProg = (struct gl_shader_program *)
          _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
       if (!shProg) {
-         _mesa_error(ctx, GL_INVALID_VALUE, caller);
+         _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
          return NULL;
       }
       if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
-         _mesa_error(ctx, GL_INVALID_OPERATION, caller);
+         _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
          return NULL;
       }
       return shProg;
@@ -403,6 +410,5 @@ _mesa_init_shader_object_functions(struct dd_function_table *driver)
    driver->DeleteShader = _mesa_delete_shader;
    driver->NewShaderProgram = _mesa_new_shader_program;
    driver->DeleteShaderProgram = _mesa_delete_shader_program;
-   driver->CompileShader = _mesa_ir_compile_shader;
    driver->LinkShader = _mesa_ir_link_shader;
 }