ARB prog parser: Delete the old parser
[profile/ivi/mesa.git] / src / mesa / drivers / dri / r300 / r300_shader.c
1 /*
2  * Copyright 2009 Maciej Cencora <m.cencora@gmail.com>
3  *
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial
16  * portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27
28 #include "main/glheader.h"
29
30 #include "shader/program.h"
31 #include "tnl/tnl.h"
32 #include "r300_context.h"
33 #include "r300_fragprog_common.h"
34
35 static void freeFragProgCache(GLcontext *ctx, struct r300_fragment_program_cont *cache)
36 {
37         struct r300_fragment_program *tmp, *fp = cache->progs;
38
39         while (fp) {
40                 tmp = fp->next;
41                 _mesa_reference_program(ctx, &fp->Base, NULL);
42                 _mesa_free(fp);
43                 fp = tmp;
44         }
45 }
46
47 static void freeVertProgCache(GLcontext *ctx, struct r300_vertex_program_cont *cache)
48 {
49         struct r300_vertex_program *tmp, *vp = cache->progs;
50
51         while (vp) {
52                 tmp = vp->next;
53                 _mesa_reference_vertprog(ctx, &vp->Base, NULL);
54                 _mesa_free(vp);
55                 vp = tmp;
56         }
57 }
58
59 static struct gl_program *r300NewProgram(GLcontext * ctx, GLenum target,
60                                          GLuint id)
61 {
62         struct r300_vertex_program_cont *vp;
63         struct r300_fragment_program_cont *fp;
64
65         switch (target) {
66         case GL_VERTEX_STATE_PROGRAM_NV:
67         case GL_VERTEX_PROGRAM_ARB:
68                 vp = CALLOC_STRUCT(r300_vertex_program_cont);
69                 return _mesa_init_vertex_program(ctx, &vp->mesa_program, target, id);
70
71         case GL_FRAGMENT_PROGRAM_NV:
72         case GL_FRAGMENT_PROGRAM_ARB:
73                 fp = CALLOC_STRUCT(r300_fragment_program_cont);
74                 return _mesa_init_fragment_program(ctx, &fp->Base, target, id);
75
76         default:
77                 _mesa_problem(ctx, "Bad target in r300NewProgram");
78         }
79
80         return NULL;
81 }
82
83 static void r300DeleteProgram(GLcontext * ctx, struct gl_program *prog)
84 {
85         struct r300_vertex_program_cont *vp = (struct r300_vertex_program_cont *)prog;
86         struct r300_fragment_program_cont *fp = (struct r300_fragment_program_cont *)prog;
87
88         switch (prog->Target) {
89                 case GL_VERTEX_PROGRAM_ARB:
90                         freeVertProgCache(ctx, vp);
91                         break;
92                 case GL_FRAGMENT_PROGRAM_ARB:
93                         freeFragProgCache(ctx, fp);
94                         break;
95         }
96
97         _mesa_delete_program(ctx, prog);
98 }
99
100 static void
101 r300ProgramStringNotify(GLcontext * ctx, GLenum target, struct gl_program *prog)
102 {
103         struct r300_vertex_program_cont *vp = (struct r300_vertex_program_cont *)prog;
104         struct r300_fragment_program_cont *fp = (struct r300_fragment_program_cont *)prog;
105
106         switch (target) {
107         case GL_VERTEX_PROGRAM_ARB:
108                 freeVertProgCache(ctx, vp);
109                 vp->progs = NULL;
110                 break;
111         case GL_FRAGMENT_PROGRAM_ARB:
112                 freeFragProgCache(ctx, fp);
113                 fp->progs = NULL;
114                 break;
115         }
116
117         /* need this for tcl fallbacks */
118         _tnl_program_string(ctx, target, prog);
119 }
120
121 static GLboolean
122 r300IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
123 {
124         if (target == GL_FRAGMENT_PROGRAM_ARB) {
125                 struct r300_fragment_program *fp = r300SelectFragmentShader(ctx);
126                 if (!fp->translated)
127                         r300TranslateFragmentShader(ctx, fp);
128
129                 return !fp->error;
130         } else {
131                 struct r300_vertex_program *vp = r300SelectVertexShader(ctx);
132                 if (!vp->translated)
133                         r300TranslateVertexShader(vp);
134
135                 return !vp->error;
136         }
137 }
138
139 void r300InitShaderFuncs(struct dd_function_table *functions)
140 {
141         functions->NewProgram = r300NewProgram;
142         functions->DeleteProgram = r300DeleteProgram;
143         functions->ProgramStringNotify = r300ProgramStringNotify;
144         functions->IsProgramNative = r300IsProgramNative;
145 }