Merge commit 'fj/mesa-next'
[mesa.git] / src / mesa / drivers / dri / r600 / r700_oglprog.c
1 /*
2 * Copyright (C) 2008-2009 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /*
23 * Authors:
24 * Richard Li <RichardZ.Li@amd.com>, <richardradeon@gmail.com>
25 */
26
27 #include <string.h>
28
29 #include "main/glheader.h"
30 #include "main/imports.h"
31
32 #include "shader/program.h"
33 #include "tnl/tnl.h"
34
35 #include "r600_context.h"
36 #include "r600_emit.h"
37
38 #include "r700_oglprog.h"
39 #include "r700_fragprog.h"
40 #include "r700_vertprog.h"
41
42
43 static void freeVertProgCache(GLcontext *ctx, struct r700_vertex_program_cont *cache)
44 {
45 struct r700_vertex_program *tmp, *vp = cache->progs;
46
47 while (vp) {
48 tmp = vp->next;
49 /* Release DMA region */
50 r600DeleteShader(ctx, vp->shaderbo);
51 /* Clean up */
52 Clean_Up_Assembler(&(vp->r700AsmCode));
53 Clean_Up_Shader(&(vp->r700Shader));
54
55 _mesa_reference_vertprog(ctx, &vp->mesa_program, NULL);
56 _mesa_free(vp);
57 vp = tmp;
58 }
59 }
60
61 static struct gl_program *r700NewProgram(GLcontext * ctx,
62 GLenum target,
63 GLuint id)
64 {
65 struct gl_program *pProgram = NULL;
66
67 struct r700_vertex_program_cont *vpc;
68 struct r700_fragment_program *fp;
69
70 radeon_print(RADEON_SHADER, RADEON_VERBOSE,
71 "%s %u, %u\n", __func__, target, id);
72
73 switch (target)
74 {
75 case GL_VERTEX_STATE_PROGRAM_NV:
76 case GL_VERTEX_PROGRAM_ARB:
77 vpc = CALLOC_STRUCT(r700_vertex_program_cont);
78 pProgram = _mesa_init_vertex_program(ctx,
79 &vpc->mesa_program,
80 target,
81 id);
82 break;
83 case GL_FRAGMENT_PROGRAM_NV:
84 case GL_FRAGMENT_PROGRAM_ARB:
85 fp = CALLOC_STRUCT(r700_fragment_program);
86 pProgram = _mesa_init_fragment_program(ctx,
87 &fp->mesa_program,
88 target,
89 id);
90 fp->translated = GL_FALSE;
91 fp->loaded = GL_FALSE;
92
93 fp->shaderbo = NULL;
94
95 break;
96 default:
97 _mesa_problem(ctx, "Bad target in r700NewProgram");
98 }
99
100 return pProgram;
101 }
102
103 static void r700DeleteProgram(GLcontext * ctx, struct gl_program *prog)
104 {
105 struct r700_vertex_program_cont *vpc = (struct r700_vertex_program_cont *)prog;
106 struct r700_fragment_program * fp;
107
108 radeon_print(RADEON_SHADER, RADEON_VERBOSE,
109 "%s %p\n", __func__, prog);
110
111 switch (prog->Target)
112 {
113 case GL_VERTEX_STATE_PROGRAM_NV:
114 case GL_VERTEX_PROGRAM_ARB:
115 freeVertProgCache(ctx, vpc);
116 break;
117 case GL_FRAGMENT_PROGRAM_NV:
118 case GL_FRAGMENT_PROGRAM_ARB:
119 fp = (struct r700_fragment_program*)prog;
120 /* Release DMA region */
121
122 r600DeleteShader(ctx, fp->shaderbo);
123
124 /* Clean up */
125 Clean_Up_Assembler(&(fp->r700AsmCode));
126 Clean_Up_Shader(&(fp->r700Shader));
127 break;
128 default:
129 _mesa_problem(ctx, "Bad target in r700NewProgram");
130 }
131
132 _mesa_delete_program(ctx, prog);
133 }
134
135 static GLboolean
136 r700ProgramStringNotify(GLcontext * ctx, GLenum target, struct gl_program *prog)
137 {
138 struct r700_vertex_program_cont *vpc = (struct r700_vertex_program_cont *)prog;
139 struct r700_fragment_program * fp = (struct r700_fragment_program*)prog;
140
141 switch (target) {
142 case GL_VERTEX_PROGRAM_ARB:
143 freeVertProgCache(ctx, vpc);
144 vpc->progs = NULL;
145 break;
146 case GL_FRAGMENT_PROGRAM_ARB:
147 r600DeleteShader(ctx, fp->shaderbo);
148 Clean_Up_Assembler(&(fp->r700AsmCode));
149 Clean_Up_Shader(&(fp->r700Shader));
150 fp->translated = GL_FALSE;
151 fp->loaded = GL_FALSE;
152 fp->shaderbo = NULL;
153 break;
154 }
155
156 /* XXX check if program is legal, within limits */
157 return GL_TRUE;
158 }
159
160 static GLboolean r700IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
161 {
162
163 return GL_TRUE;
164 }
165
166 void r700InitShaderFuncs(struct dd_function_table *functions)
167 {
168 functions->NewProgram = r700NewProgram;
169 functions->DeleteProgram = r700DeleteProgram;
170 functions->ProgramStringNotify = r700ProgramStringNotify;
171 functions->IsProgramNative = r700IsProgramNative;
172 }