Merge commit 'origin/master' into gallium-sw-api-2
[mesa.git] / src / mesa / drivers / dri / i965 / brw_program.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "main/imports.h"
33 #include "main/enums.h"
34 #include "shader/prog_parameter.h"
35 #include "shader/program.h"
36 #include "shader/programopt.h"
37 #include "tnl/tnl.h"
38
39 #include "brw_context.h"
40 #include "brw_wm.h"
41
42 static void brwBindProgram( GLcontext *ctx,
43 GLenum target,
44 struct gl_program *prog )
45 {
46 struct brw_context *brw = brw_context(ctx);
47
48 switch (target) {
49 case GL_VERTEX_PROGRAM_ARB:
50 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
51 break;
52 case GL_FRAGMENT_PROGRAM_ARB:
53 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
54 break;
55 }
56 }
57
58 static struct gl_program *brwNewProgram( GLcontext *ctx,
59 GLenum target,
60 GLuint id )
61 {
62 struct brw_context *brw = brw_context(ctx);
63
64 switch (target) {
65 case GL_VERTEX_PROGRAM_ARB: {
66 struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
67 if (prog) {
68 prog->id = brw->program_id++;
69
70 return _mesa_init_vertex_program( ctx, &prog->program,
71 target, id );
72 }
73 else
74 return NULL;
75 }
76
77 case GL_FRAGMENT_PROGRAM_ARB: {
78 struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
79 if (prog) {
80 prog->id = brw->program_id++;
81
82 return _mesa_init_fragment_program( ctx, &prog->program,
83 target, id );
84 }
85 else
86 return NULL;
87 }
88
89 default:
90 return _mesa_new_program(ctx, target, id);
91 }
92 }
93
94 static void brwDeleteProgram( GLcontext *ctx,
95 struct gl_program *prog )
96 {
97 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
98 struct gl_fragment_program *fp = (struct gl_fragment_program *) prog;
99 struct brw_fragment_program *brw_fp = brw_fragment_program(fp);
100
101 dri_bo_unreference(brw_fp->const_buffer);
102 }
103
104 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
105 struct gl_vertex_program *vp = (struct gl_vertex_program *) prog;
106 struct brw_vertex_program *brw_vp = brw_vertex_program(vp);
107
108 dri_bo_unreference(brw_vp->const_buffer);
109 }
110
111 _mesa_delete_program( ctx, prog );
112 }
113
114
115 static GLboolean brwIsProgramNative( GLcontext *ctx,
116 GLenum target,
117 struct gl_program *prog )
118 {
119 return GL_TRUE;
120 }
121
122
123 static GLboolean brwProgramStringNotify( GLcontext *ctx,
124 GLenum target,
125 struct gl_program *prog )
126 {
127 struct brw_context *brw = brw_context(ctx);
128
129 if (target == GL_FRAGMENT_PROGRAM_ARB) {
130 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
131 struct brw_fragment_program *newFP = brw_fragment_program(fprog);
132 const struct brw_fragment_program *curFP =
133 brw_fragment_program_const(brw->fragment_program);
134
135 if (fprog->FogOption) {
136 _mesa_append_fog_code(ctx, fprog);
137 fprog->FogOption = GL_NONE;
138 }
139
140 if (newFP == curFP)
141 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
142 newFP->id = brw->program_id++;
143 newFP->isGLSL = brw_wm_is_glsl(fprog);
144 }
145 else if (target == GL_VERTEX_PROGRAM_ARB) {
146 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
147 struct brw_vertex_program *newVP = brw_vertex_program(vprog);
148 const struct brw_vertex_program *curVP =
149 brw_vertex_program_const(brw->vertex_program);
150
151 if (newVP == curVP)
152 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
153 if (newVP->program.IsPositionInvariant) {
154 _mesa_insert_mvp_code(ctx, &newVP->program);
155 }
156 newVP->id = brw->program_id++;
157
158 /* Also tell tnl about it:
159 */
160 _tnl_program_string(ctx, target, prog);
161 }
162
163 /* XXX check if program is legal, within limits */
164 return GL_TRUE;
165 }
166
167 void brwInitFragProgFuncs( struct dd_function_table *functions )
168 {
169 assert(functions->ProgramStringNotify == _tnl_program_string);
170
171 functions->BindProgram = brwBindProgram;
172 functions->NewProgram = brwNewProgram;
173 functions->DeleteProgram = brwDeleteProgram;
174 functions->IsProgramNative = brwIsProgramNative;
175 functions->ProgramStringNotify = brwProgramStringNotify;
176 }
177