i965: update RefCount when using Vertex/Fragment program.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_context.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
33 #include "brw_context.h"
34 #include "brw_defines.h"
35 #include "brw_draw.h"
36 #include "brw_vs.h"
37 #include "imports.h"
38 #include "intel_tex.h"
39 #include "intel_blit.h"
40 #include "intel_batchbuffer.h"
41
42 #include "utils.h"
43 #include "api_noop.h"
44 #include "vtxfmt.h"
45
46 #include "shader/shader_api.h"
47
48 /***************************************
49 * Mesa's Driver Functions
50 ***************************************/
51
52 static const struct dri_extension brw_extensions[] =
53 {
54 { "GL_ARB_depth_texture", NULL },
55 { "GL_ARB_fragment_program", NULL },
56 { "GL_ARB_shadow", NULL },
57 { "GL_EXT_shadow_funcs", NULL },
58 /* ARB extn won't work if not enabled */
59 { "GL_SGIX_depth_texture", NULL },
60 { "GL_ARB_texture_env_crossbar", NULL },
61 { NULL, NULL }
62 };
63
64 static void brwUseProgram(GLcontext *ctx, GLuint program)
65 {
66 struct brw_context *brw = brw_context(ctx);
67 struct gl_shader_program *sh_prog;
68 _mesa_use_program(ctx, program);
69 sh_prog = ctx->Shader.CurrentProgram;
70 if (sh_prog) {
71 if (sh_prog->VertexProgram) {
72 brw->attribs.VertexProgram->Current = sh_prog->VertexProgram;
73 sh_prog->VertexProgram->Base.RefCount++;
74 ctx->VertexProgram.Enabled = GL_TRUE;
75 }else
76 ctx->VertexProgram.Enabled = GL_FALSE;
77
78 if (sh_prog->FragmentProgram) {
79 brw->attribs.FragmentProgram->Current = sh_prog->FragmentProgram;
80 sh_prog->FragmentProgram->Base.RefCount++;
81 ctx->FragmentProgram.Enabled = GL_TRUE;
82 } else
83 ctx->FragmentProgram.Enabled = GL_FALSE;
84 }
85 }
86
87 static void brwInitProgFuncs( struct dd_function_table *functions )
88 {
89 functions->UseProgram = brwUseProgram;
90 }
91 static void brwInitDriverFunctions( struct dd_function_table *functions )
92 {
93 intelInitDriverFunctions( functions );
94 brwInitFragProgFuncs( functions );
95 brwInitProgFuncs( functions );
96 }
97
98
99 static void brw_init_attribs( struct brw_context *brw )
100 {
101 GLcontext *ctx = &brw->intel.ctx;
102
103 brw->attribs.Color = &ctx->Color;
104 brw->attribs.Depth = &ctx->Depth;
105 brw->attribs.Fog = &ctx->Fog;
106 brw->attribs.Hint = &ctx->Hint;
107 brw->attribs.Light = &ctx->Light;
108 brw->attribs.Line = &ctx->Line;
109 brw->attribs.Point = &ctx->Point;
110 brw->attribs.Polygon = &ctx->Polygon;
111 brw->attribs.Scissor = &ctx->Scissor;
112 brw->attribs.Stencil = &ctx->Stencil;
113 brw->attribs.Texture = &ctx->Texture;
114 brw->attribs.Transform = &ctx->Transform;
115 brw->attribs.Viewport = &ctx->Viewport;
116 brw->attribs.VertexProgram = &ctx->VertexProgram;
117 brw->attribs.FragmentProgram = &ctx->FragmentProgram;
118 brw->attribs.PolygonStipple = &ctx->PolygonStipple[0];
119 }
120
121
122 GLboolean brwCreateContext( const __GLcontextModes *mesaVis,
123 __DRIcontextPrivate *driContextPriv,
124 void *sharedContextPrivate)
125 {
126 struct dd_function_table functions;
127 struct brw_context *brw = (struct brw_context *) CALLOC_STRUCT(brw_context);
128 struct intel_context *intel = &brw->intel;
129 GLcontext *ctx = &intel->ctx;
130
131 if (!brw) {
132 _mesa_printf("%s: failed to alloc context\n", __FUNCTION__);
133 return GL_FALSE;
134 }
135
136 brwInitVtbl( brw );
137 brwInitDriverFunctions( &functions );
138
139 if (!intelInitContext( intel, mesaVis, driContextPriv,
140 sharedContextPrivate, &functions )) {
141 _mesa_printf("%s: failed to init intel context\n", __FUNCTION__);
142 FREE(brw);
143 return GL_FALSE;
144 }
145
146 ctx->Const.MaxTextureUnits = BRW_MAX_TEX_UNIT;
147 ctx->Const.MaxTextureImageUnits = BRW_MAX_TEX_UNIT;
148 ctx->Const.MaxTextureCoordUnits = BRW_MAX_TEX_UNIT;
149
150
151 /* Advertise the full hardware capabilities. The new memory
152 * manager should cope much better with overload situations:
153 */
154 ctx->Const.MaxTextureLevels = 12;
155 ctx->Const.Max3DTextureLevels = 9;
156 ctx->Const.MaxCubeTextureLevels = 12;
157 ctx->Const.MaxTextureRectSize = (1<<11);
158 ctx->Const.MaxTextureUnits = BRW_MAX_TEX_UNIT;
159
160 /* ctx->Const.MaxNativeVertexProgramTemps = 32; */
161
162
163 driInitExtensions( ctx, brw_extensions, GL_FALSE );
164
165 brw_init_attribs( brw );
166 brw_init_metaops( brw );
167 brw_init_state( brw );
168
169 brw->state.dirty.mesa = ~0;
170 brw->state.dirty.brw = ~0;
171
172 memset(&brw->wm.bind, ~0, sizeof(brw->wm.bind));
173
174 brw->emit_state_always = 0;
175
176 ctx->FragmentProgram._MaintainTexEnvProgram = 1;
177
178 brw_draw_init( brw );
179
180 brw_ProgramCacheInit( ctx );
181
182 brw_FrameBufferTexInit( brw );
183
184 return GL_TRUE;
185 }
186