implement gl2_3dlabs_shhandle interface; resolve few TODOs
authorMichal Krol <mjkrol@gmail.org>
Fri, 25 Mar 2005 12:16:53 +0000 (12:16 +0000)
committerMichal Krol <mjkrol@gmail.org>
Fri, 25 Mar 2005 12:16:53 +0000 (12:16 +0000)
src/mesa/shader/shaderobjects_3dlabs.c

index 370670fdfc2a76400ee8826338395500ad1f3f9d..ce56ddfa67c5c5cc73bc9baf0e49e76a64da2b3d 100755 (executable)
@@ -35,7 +35,8 @@
 #include "context.h"\r
 #include "macros.h"\r
 #include "hash.h"\r
-\r
+#include "slang_mesa.h"\r
+#include "Public/ShaderLang.h"\r
 \r
 struct gl2_unknown_obj\r
 {\r
@@ -100,6 +101,59 @@ _unknown_constructor (struct gl2_unknown_impl *impl)
        impl->_obj._destructor = _unknown_destructor;\r
 }\r
 \r
+struct gl2_unkinner_obj\r
+{\r
+       struct gl2_unknown_intf **unkouter;\r
+};\r
+\r
+struct gl2_unkinner_impl\r
+{\r
+       struct gl2_unknown_intf *_vftbl;\r
+       struct gl2_unkinner_obj _obj;\r
+};\r
+\r
+static void\r
+_unkinner_destructor (struct gl2_unknown_intf **intf)\r
+{\r
+}\r
+\r
+static void\r
+_unkinner_AddRef (struct gl2_unknown_intf **intf)\r
+{\r
+       struct gl2_unkinner_impl *impl = (struct gl2_unkinner_impl *) intf;\r
+\r
+       (**impl->_obj.unkouter).AddRef (impl->_obj.unkouter);\r
+}\r
+\r
+static void\r
+_unkinner_Release (struct gl2_unknown_intf **intf)\r
+{\r
+       struct gl2_unkinner_impl *impl = (struct gl2_unkinner_impl *) intf;\r
+\r
+       (**impl->_obj.unkouter).Release (impl->_obj.unkouter);\r
+}\r
+\r
+static struct gl2_unknown_intf **\r
+_unkinner_QueryInterface (struct gl2_unknown_intf **intf, enum gl2_uiid uiid)\r
+{\r
+       struct gl2_unkinner_impl *impl = (struct gl2_unkinner_impl *) intf;\r
+\r
+       return (**impl->_obj.unkouter).QueryInterface (impl->_obj.unkouter, uiid);\r
+}\r
+\r
+static struct gl2_unknown_intf _unkinner_vftbl = {\r
+       _unkinner_AddRef,\r
+       _unkinner_Release,\r
+       _unkinner_QueryInterface\r
+};\r
+\r
+static void\r
+_unkinner_constructor (struct gl2_unkinner_impl *impl, struct gl2_unknown_intf **outer)\r
+{\r
+       impl->_vftbl = &_unkinner_vftbl;\r
+       impl->_obj.unkouter = outer;\r
+}\r
+\r
 struct gl2_generic_obj\r
 {\r
        struct gl2_unknown_obj _unknown;\r
@@ -183,7 +237,7 @@ static struct gl2_generic_intf _generic_vftbl = {
                _generic_QueryInterface\r
        },\r
        _generic_Delete,\r
-       NULL,\r
+       NULL,           /* abstract GetType */\r
        _generic_GetName,\r
        _generic_GetDeleteStatus,\r
        _generic_GetInfoLog\r
@@ -314,18 +368,18 @@ _container_GetAttached (struct gl2_container_intf **intf, GLuint index)
 }\r
 \r
 static struct gl2_container_intf _container_vftbl = {\r
-   {\r
-      {\r
-       _unknown_AddRef,\r
-       _unknown_Release,\r
-       _container_QueryInterface,\r
-      },\r
-       _generic_Delete,\r
-       NULL,\r
-       _generic_GetName,\r
-       _generic_GetDeleteStatus,\r
-       _generic_GetInfoLog,\r
-   },\r
+       {\r
+               {\r
+                       _unknown_AddRef,\r
+                       _unknown_Release,\r
+                       _container_QueryInterface\r
+               },\r
+               _generic_Delete,\r
+               NULL,           /* abstract GetType */\r
+               _generic_GetName,\r
+               _generic_GetDeleteStatus,\r
+               _generic_GetInfoLog\r
+       },\r
        _container_Attach,\r
        _container_Detach,\r
        _container_GetAttachedCount,\r
@@ -342,9 +396,56 @@ _container_constructor (struct gl2_container_impl *impl)
        impl->_obj.attached_count = 0;\r
 }\r
 \r
+struct gl2_3dlabs_shhandle_obj\r
+{\r
+       struct gl2_unkinner_obj _unknown;\r
+       ShHandle handle;\r
+};\r
+\r
+struct gl2_3dlabs_shhandle_impl\r
+{\r
+       struct gl2_3dlabs_shhandle_intf *_vftbl;\r
+       struct gl2_3dlabs_shhandle_obj _obj;\r
+};\r
+\r
+static void\r
+_3dlabs_shhandle_destructor (struct gl2_unknown_intf **intf)\r
+{\r
+       struct gl2_3dlabs_shhandle_impl *impl = (struct gl2_3dlabs_shhandle_impl *) intf;\r
+\r
+       ShDestruct (impl->_obj.handle);\r
+       _unkinner_destructor (intf);\r
+}\r
+\r
+static GLvoid *\r
+_3dlabs_shhandle_GetShHandle (struct gl2_3dlabs_shhandle_intf **intf)\r
+{\r
+       struct gl2_3dlabs_shhandle_impl *impl = (struct gl2_3dlabs_shhandle_impl *) intf;\r
+\r
+       return impl->_obj.handle;\r
+}\r
+\r
+static struct gl2_3dlabs_shhandle_intf _3dlabs_shhandle_vftbl = {\r
+       {\r
+               _unkinner_AddRef,\r
+               _unkinner_Release,\r
+               _unkinner_QueryInterface\r
+       },\r
+       _3dlabs_shhandle_GetShHandle\r
+};\r
+\r
+static void\r
+_3dlabs_shhandle_constructor (struct gl2_3dlabs_shhandle_impl *impl, struct gl2_unknown_intf **outer)\r
+{\r
+       _unkinner_constructor ((struct gl2_unkinner_impl *) impl, outer);\r
+       impl->_vftbl = &_3dlabs_shhandle_vftbl;\r
+       impl->_obj.handle = NULL;\r
+}\r
+\r
 struct gl2_shader_obj\r
 {\r
        struct gl2_generic_obj _generic;\r
+       struct gl2_3dlabs_shhandle_impl _3dlabs_shhandle;\r
        GLboolean compile_status;\r
        GLcharARB *source;\r
        GLint *offsets;\r
@@ -364,17 +465,25 @@ _shader_destructor (struct gl2_unknown_intf **intf)
 \r
        _mesa_free ((void *) impl->_obj.source);\r
        _mesa_free ((void *) impl->_obj.offsets);\r
+       _3dlabs_shhandle_destructor ((struct gl2_unknown_intf **) &impl->_obj._3dlabs_shhandle._vftbl);\r
        _generic_destructor (intf);\r
 }\r
 \r
 static struct gl2_unknown_intf **\r
 _shader_QueryInterface (struct gl2_unknown_intf **intf, enum gl2_uiid uiid)\r
 {\r
+       struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;\r
+\r
        if (uiid == UIID_SHADER)\r
        {\r
                (**intf).AddRef (intf);\r
                return intf;\r
        }\r
+       if (uiid == UIID_3DLABS_SHHANDLE)\r
+       {\r
+               (**intf).AddRef (intf);\r
+               return (struct gl2_unknown_intf **) &impl->_obj._3dlabs_shhandle._vftbl;\r
+       }\r
        return _generic_QueryInterface (intf, uiid);\r
 }\r
 \r
@@ -416,28 +525,93 @@ static GLvoid
 _shader_Compile (struct gl2_shader_intf **intf)\r
 {\r
        struct gl2_shader_impl *impl = (struct gl2_shader_impl *) intf;\r
+       char **strings;\r
+       TBuiltInResource res;\r
 \r
        impl->_obj.compile_status = GL_FALSE;\r
        _mesa_free ((void *) impl->_obj._generic.info_log);\r
        impl->_obj._generic.info_log = NULL;\r
 \r
-       /* TODO compile */\r
+       /* 3dlabs compiler expects us to feed it with null-terminated string array,\r
+       we've got only one big string with offsets, so we must split it; but when\r
+       there's only one string to deal with, we pass its address directly */\r
+\r
+       if (impl->_obj.offset_count <= 1)\r
+               strings = &impl->_obj.source;\r
+       else\r
+       {\r
+               GLsizei i, offset = 0;\r
+\r
+               strings = (char **) _mesa_malloc (impl->_obj.offset_count * sizeof (char *));\r
+               if (strings == NULL)\r
+                       return;\r
+\r
+               for (i = 0; i < impl->_obj.offset_count; i++)\r
+               {\r
+                       GLsizei size = impl->_obj.offsets[i] - offset;\r
+\r
+                       strings[i] = (char *) _mesa_malloc ((size + 1) * sizeof (char));\r
+                       if (strings[i] == NULL)\r
+                       {\r
+                               GLsizei j;\r
+\r
+                               for (j = 0; j < i; j++)\r
+                                       _mesa_free (strings[j]);\r
+                               _mesa_free (strings);\r
+                               return;\r
+                       }\r
+\r
+                       _mesa_memcpy (strings[i], impl->_obj.source + offset, size * sizeof (char));\r
+                       strings[i][size] = '\0';\r
+                       offset = impl->_obj.offsets[i];\r
+               }\r
+       }\r
+\r
+       /* TODO set these fields to some REAL numbers */\r
+       res.maxLights = 8;\r
+       res.maxClipPlanes = 6;\r
+       res.maxTextureUnits = 2;\r
+       res.maxTextureCoords = 2;\r
+       res.maxVertexAttribs = 8;\r
+       res.maxVertexUniformComponents = 64;\r
+       res.maxVaryingFloats = 8;\r
+       res.maxVertexTextureImageUnits = 2;\r
+       res.maxCombinedTextureImageUnits = 2;\r
+       res.maxTextureImageUnits = 2;\r
+       res.maxFragmentUniformComponents = 64;\r
+       res.maxDrawBuffers = 1;\r
+\r
+       if (ShCompile (impl->_obj._3dlabs_shhandle._obj.handle, strings, impl->_obj.offset_count,\r
+                       EShOptFull, &res, 0))\r
+               impl->_obj.compile_status = GL_TRUE;\r
+\r
+       if (impl->_obj.offset_count > 1)\r
+       {\r
+               GLsizei i;\r
+\r
+               for (i = 0; i < impl->_obj.offset_count; i++)\r
+                       _mesa_free (strings[i]);\r
+               _mesa_free (strings);\r
+       }\r
+\r
+       impl->_obj._generic.info_log = _mesa_strdup (ShGetInfoLog (\r
+               impl->_obj._3dlabs_shhandle._obj.handle));\r
 }\r
 \r
 static struct gl2_shader_intf _shader_vftbl = {\r
-   {\r
-      {\r
-       _unknown_AddRef,\r
-       _unknown_Release,\r
-       _shader_QueryInterface,\r
-      },\r
-       _generic_Delete,\r
-       _shader_GetType,\r
-       _generic_GetName,\r
-       _generic_GetDeleteStatus,\r
-       _generic_GetInfoLog,\r
-   },\r
-       NULL,\r
+       {\r
+               {\r
+                       _unknown_AddRef,\r
+                       _unknown_Release,\r
+                       _shader_QueryInterface\r
+               },\r
+               _generic_Delete,\r
+               _shader_GetType,\r
+               _generic_GetName,\r
+               _generic_GetDeleteStatus,\r
+               _generic_GetInfoLog\r
+       },\r
+       NULL,           /* abstract GetSubType */\r
        _shader_GetCompileStatus,\r
        _shader_SetSource,\r
        _shader_GetSource,\r
@@ -448,6 +622,8 @@ static void
 _shader_constructor (struct gl2_shader_impl *impl)\r
 {\r
        _generic_constructor ((struct gl2_generic_impl *) impl);\r
+       _3dlabs_shhandle_constructor (&impl->_obj._3dlabs_shhandle, (struct gl2_unknown_intf **)\r
+               &impl->_vftbl);\r
        impl->_vftbl = &_shader_vftbl;\r
        impl->_obj._generic._unknown._destructor = _shader_destructor;\r
        impl->_obj.compile_status = GL_FALSE;\r
@@ -461,6 +637,8 @@ struct gl2_program_obj
        struct gl2_container_obj _container;\r
        GLboolean link_status;\r
        GLboolean validate_status;\r
+       ShHandle linker;\r
+       ShHandle uniforms;\r
 };\r
 \r
 struct gl2_program_impl\r
@@ -473,7 +651,9 @@ static void
 _program_destructor (struct gl2_unknown_intf **intf)\r
 {\r
        struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;\r
-       (void) impl;\r
+\r
+       ShDestruct (impl->_obj.linker);\r
+       ShDestruct (impl->_obj.uniforms);\r
        _container_destructor (intf);\r
 }\r
 \r
@@ -531,12 +711,41 @@ static GLvoid
 _program_Link (struct gl2_program_intf **intf)\r
 {\r
        struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;\r
+       ShHandle *handles;\r
+       GLuint i;\r
 \r
        impl->_obj.link_status = GL_FALSE;\r
        _mesa_free ((void *) impl->_obj._container._generic.info_log);\r
        impl->_obj._container._generic.info_log = NULL;\r
 \r
-       /* TODO link */\r
+       handles = (ShHandle *) _mesa_malloc (impl->_obj._container.attached_count * sizeof (ShHandle));\r
+       if (handles == NULL)\r
+               return;\r
+\r
+       for (i = 0; i < impl->_obj._container.attached_count; i++)\r
+       {\r
+               struct gl2_generic_intf **gen = impl->_obj._container.attached[i];\r
+               struct gl2_3dlabs_shhandle_intf **sh;\r
+\r
+               sh = (struct gl2_3dlabs_shhandle_intf **) (**gen)._unknown.QueryInterface (\r
+                       (struct gl2_unknown_intf **) gen, UIID_3DLABS_SHHANDLE);\r
+               if (sh != NULL)\r
+               {\r
+                       handles[i] = (**sh).GetShHandle (sh);\r
+                       (**sh)._unknown.Release ((struct gl2_unknown_intf **) sh);\r
+               }\r
+               else\r
+               {\r
+                       _mesa_free (handles);\r
+                       return;\r
+               }\r
+       }\r
+\r
+       if (ShLink (impl->_obj.linker, handles, impl->_obj._container.attached_count,\r
+                       impl->_obj.uniforms, NULL, NULL))\r
+               impl->_obj.link_status = GL_TRUE;\r
+\r
+       impl->_obj._container._generic.info_log = _mesa_strdup (ShGetInfoLog (impl->_obj.linker));\r
 }\r
 \r
 static GLvoid\r
@@ -545,6 +754,8 @@ _program_Validate (struct gl2_program_intf **intf)
        struct gl2_program_impl *impl = (struct gl2_program_impl *) intf;\r
 \r
        impl->_obj.validate_status = GL_FALSE;\r
+       _mesa_free ((void *) impl->_obj._container._generic.info_log);\r
+       impl->_obj._container._generic.info_log = NULL;\r
 \r
        /* TODO validate */\r
 }\r
@@ -555,18 +766,18 @@ static struct gl2_program_intf _program_vftbl = {
                        {\r
                                _unknown_AddRef,\r
                                _unknown_Release,\r
-                               _program_QueryInterface,\r
+                               _program_QueryInterface\r
                        },\r
                        _generic_Delete,\r
                        _program_GetType,\r
                        _generic_GetName,\r
                        _generic_GetDeleteStatus,\r
-                       _generic_GetInfoLog,\r
+                       _generic_GetInfoLog\r
                },\r
                _program_Attach,\r
                _container_Detach,\r
                _container_GetAttachedCount,\r
-               _container_GetAttached,\r
+               _container_GetAttached\r
        },\r
        _program_GetLinkStatus,\r
        _program_GetValidateStatus,\r
@@ -582,6 +793,8 @@ _program_constructor (struct gl2_program_impl *impl)
        impl->_obj._container._generic._unknown._destructor = _program_destructor;\r
        impl->_obj.link_status = GL_FALSE;\r
        impl->_obj.validate_status = GL_FALSE;\r
+       impl->_obj.linker = ShConstructLinker (EShExVertexFragment, 0);\r
+       impl->_obj.uniforms = ShConstructUniformMap ();\r
 }\r
 \r
 struct gl2_fragment_shader_obj\r
@@ -599,6 +812,7 @@ static void
 _fragment_shader_destructor (struct gl2_unknown_intf **intf)\r
 {\r
        struct gl2_fragment_shader_impl *impl = (struct gl2_fragment_shader_impl *) intf;\r
+\r
        (void) impl;\r
        /* TODO free fragment shader data */\r
 \r
@@ -628,13 +842,13 @@ static struct gl2_fragment_shader_intf _fragment_shader_vftbl = {
                        {\r
                                _unknown_AddRef,\r
                                _unknown_Release,\r
-                               _fragment_shader_QueryInterface,\r
+                               _fragment_shader_QueryInterface\r
                        },\r
                        _generic_Delete,\r
                        _shader_GetType,\r
                        _generic_GetName,\r
                        _generic_GetDeleteStatus,\r
-                       _generic_GetInfoLog,\r
+                       _generic_GetInfoLog\r
                },\r
                _fragment_shader_GetSubType,\r
                _shader_GetCompileStatus,\r
@@ -650,6 +864,7 @@ _fragment_shader_constructor (struct gl2_fragment_shader_impl *impl)
        _shader_constructor ((struct gl2_shader_impl *) impl);\r
        impl->_vftbl = &_fragment_shader_vftbl;\r
        impl->_obj._shader._generic._unknown._destructor = _fragment_shader_destructor;\r
+       impl->_obj._shader._3dlabs_shhandle._obj.handle = ShConstructCompiler (EShLangFragment, 0);\r
 }\r
 \r
 struct gl2_vertex_shader_obj\r
@@ -667,6 +882,7 @@ static void
 _vertex_shader_destructor (struct gl2_unknown_intf **intf)\r
 {\r
        struct gl2_vertex_shader_impl *impl = (struct gl2_vertex_shader_impl *) intf;\r
+\r
        (void) impl;\r
        /* TODO free vertex shader data */\r
 \r
@@ -696,13 +912,13 @@ static struct gl2_vertex_shader_intf _vertex_shader_vftbl = {
                        {\r
                                _unknown_AddRef,\r
                                _unknown_Release,\r
-                               _vertex_shader_QueryInterface,\r
+                               _vertex_shader_QueryInterface\r
                        },\r
                        _generic_Delete,\r
                        _shader_GetType,\r
                        _generic_GetName,\r
                        _generic_GetDeleteStatus,\r
-                       _generic_GetInfoLog,\r
+                       _generic_GetInfoLog\r
                },\r
                _vertex_shader_GetSubType,\r
                _shader_GetCompileStatus,\r
@@ -718,19 +934,18 @@ _vertex_shader_constructor (struct gl2_vertex_shader_impl *impl)
        _shader_constructor ((struct gl2_shader_impl *) impl);\r
        impl->_vftbl = &_vertex_shader_vftbl;\r
        impl->_obj._shader._generic._unknown._destructor = _vertex_shader_destructor;\r
+       impl->_obj._shader._3dlabs_shhandle._obj.handle = ShConstructCompiler (EShLangVertex, 0);\r
 }\r
 \r
 GLhandleARB\r
 _mesa_3dlabs_create_shader_object (GLenum shaderType)\r
 {\r
-       GET_CURRENT_CONTEXT(ctx);\r
-       (void) ctx;\r
        switch (shaderType)\r
        {\r
        case GL_FRAGMENT_SHADER_ARB:\r
                {\r
-                       struct gl2_fragment_shader_impl *x = (struct gl2_fragment_shader_impl *) _mesa_malloc (\r
-                               sizeof (struct gl2_fragment_shader_impl));\r
+                       struct gl2_fragment_shader_impl *x = (struct gl2_fragment_shader_impl *)\r
+                               _mesa_malloc (sizeof (struct gl2_fragment_shader_impl));\r
 \r
                        if (x != NULL)\r
                        {\r
@@ -741,8 +956,8 @@ _mesa_3dlabs_create_shader_object (GLenum shaderType)
                break;\r
        case GL_VERTEX_SHADER_ARB:\r
                {\r
-                       struct gl2_vertex_shader_impl *x = (struct gl2_vertex_shader_impl *) _mesa_malloc (\r
-                               sizeof (struct gl2_vertex_shader_impl));\r
+                       struct gl2_vertex_shader_impl *x = (struct gl2_vertex_shader_impl *)\r
+                               _mesa_malloc (sizeof (struct gl2_vertex_shader_impl));\r
 \r
                        if (x != NULL)\r
                        {\r
@@ -759,11 +974,9 @@ _mesa_3dlabs_create_shader_object (GLenum shaderType)
 GLhandleARB\r
 _mesa_3dlabs_create_program_object (void)\r
 {\r
-       GET_CURRENT_CONTEXT(ctx);\r
-       struct gl2_program_impl *x = (struct gl2_program_impl *)\r
+       struct gl2_program_impl *x = (struct gl2_program_impl *) \r
                _mesa_malloc (sizeof (struct gl2_program_impl));\r
 \r
-       (void) ctx;\r
        if (x != NULL)\r
        {\r
                _program_constructor (x);\r
@@ -773,3 +986,10 @@ _mesa_3dlabs_create_program_object (void)
        return 0;\r
 }\r
 \r
+void\r
+_mesa_init_shaderobjects_3dlabs (GLcontext *ctx)\r
+{\r
+       _glslang_3dlabs_InitProcess ();\r
+       _glslang_3dlabs_ShInitialize ();\r
+}\r
+\r