Merge remote branch 'origin/mesa_7_7_branch'
[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 rc_constants_destroy(&fp->code.constants);
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 rc_constants_destroy(&vp->code.constants);
54 _mesa_reference_vertprog(ctx, &vp->Base, NULL);
55 _mesa_free(vp);
56 vp = tmp;
57 }
58 }
59
60 static struct gl_program *r300NewProgram(GLcontext * ctx, GLenum target,
61 GLuint id)
62 {
63 struct r300_vertex_program_cont *vp;
64 struct r300_fragment_program_cont *fp;
65
66 switch (target) {
67 case GL_VERTEX_STATE_PROGRAM_NV:
68 case GL_VERTEX_PROGRAM_ARB:
69 vp = CALLOC_STRUCT(r300_vertex_program_cont);
70 return _mesa_init_vertex_program(ctx, &vp->mesa_program, target, id);
71
72 case GL_FRAGMENT_PROGRAM_NV:
73 case GL_FRAGMENT_PROGRAM_ARB:
74 fp = CALLOC_STRUCT(r300_fragment_program_cont);
75 return _mesa_init_fragment_program(ctx, &fp->Base, target, id);
76
77 default:
78 _mesa_problem(ctx, "Bad target in r300NewProgram");
79 }
80
81 return NULL;
82 }
83
84 static void r300DeleteProgram(GLcontext * ctx, struct gl_program *prog)
85 {
86 struct r300_vertex_program_cont *vp = (struct r300_vertex_program_cont *)prog;
87 struct r300_fragment_program_cont *fp = (struct r300_fragment_program_cont *)prog;
88
89 switch (prog->Target) {
90 case GL_VERTEX_PROGRAM_ARB:
91 freeVertProgCache(ctx, vp);
92 break;
93 case GL_FRAGMENT_PROGRAM_ARB:
94 freeFragProgCache(ctx, fp);
95 break;
96 }
97
98 _mesa_delete_program(ctx, prog);
99 }
100
101 static void
102 r300ProgramStringNotify(GLcontext * ctx, GLenum target, struct gl_program *prog)
103 {
104 struct r300_vertex_program_cont *vp = (struct r300_vertex_program_cont *)prog;
105 struct r300_fragment_program_cont *fp = (struct r300_fragment_program_cont *)prog;
106
107 switch (target) {
108 case GL_VERTEX_PROGRAM_ARB:
109 freeVertProgCache(ctx, vp);
110 vp->progs = NULL;
111 break;
112 case GL_FRAGMENT_PROGRAM_ARB:
113 freeFragProgCache(ctx, fp);
114 fp->progs = NULL;
115 break;
116 }
117
118 /* need this for tcl fallbacks */
119 _tnl_program_string(ctx, target, prog);
120 }
121
122 static GLboolean
123 r300IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
124 {
125 if (target == GL_FRAGMENT_PROGRAM_ARB) {
126 struct r300_fragment_program *fp = r300SelectAndTranslateFragmentShader(ctx);
127
128 return !fp->error;
129 } else {
130 struct r300_vertex_program *vp = r300SelectAndTranslateVertexShader(ctx);
131
132 return !vp->error;
133 }
134 }
135
136 void r300InitShaderFuncs(struct dd_function_table *functions)
137 {
138 functions->NewProgram = r300NewProgram;
139 functions->DeleteProgram = r300DeleteProgram;
140 functions->ProgramStringNotify = r300ProgramStringNotify;
141 functions->IsProgramNative = r300IsProgramNative;
142 }