i965: Use GTT maps when available to upload vertex arrays and system VBOs.
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portionsalloc
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/enums.h"
29 #include "main/state.h"
30 #include "main/context.h"
31 #include "main/enable.h"
32 #include "main/matrix.h"
33 #include "main/viewport.h"
34 #include "swrast/swrast.h"
35 #include "shader/arbprogram.h"
36 #include "shader/program.h"
37
38 #include "intel_context.h"
39 #include "intel_pixel.h"
40 #include "intel_regions.h"
41
42 #define FILE_DEBUG_FLAG DEBUG_PIXEL
43
44 static GLenum
45 effective_func(GLenum func, GLboolean src_alpha_is_one)
46 {
47 if (src_alpha_is_one) {
48 if (func == GL_SRC_ALPHA)
49 return GL_ONE;
50 if (func == GL_ONE_MINUS_SRC_ALPHA)
51 return GL_ZERO;
52 }
53
54 return func;
55 }
56
57 /**
58 * Check if any fragment operations are in effect which might effect
59 * glDraw/CopyPixels.
60 */
61 GLboolean
62 intel_check_blit_fragment_ops(GLcontext * ctx, GLboolean src_alpha_is_one)
63 {
64 if (ctx->NewState)
65 _mesa_update_state(ctx);
66
67 if (ctx->FragmentProgram._Enabled) {
68 DBG("fallback due to fragment program\n");
69 return GL_FALSE;
70 }
71
72 if (ctx->Color.BlendEnabled &&
73 (effective_func(ctx->Color.BlendSrcRGB, src_alpha_is_one) != GL_ONE ||
74 effective_func(ctx->Color.BlendDstRGB, src_alpha_is_one) != GL_ZERO ||
75 ctx->Color.BlendEquationRGB != GL_FUNC_ADD ||
76 effective_func(ctx->Color.BlendSrcA, src_alpha_is_one) != GL_ONE ||
77 effective_func(ctx->Color.BlendDstA, src_alpha_is_one) != GL_ZERO ||
78 ctx->Color.BlendEquationA != GL_FUNC_ADD)) {
79 DBG("fallback due to blend\n");
80 return GL_FALSE;
81 }
82
83 if (ctx->Texture._EnabledUnits) {
84 DBG("fallback due to texturing\n");
85 return GL_FALSE;
86 }
87
88 if (!(ctx->Color.ColorMask[0] &&
89 ctx->Color.ColorMask[1] &&
90 ctx->Color.ColorMask[2] &&
91 ctx->Color.ColorMask[3])) {
92 DBG("fallback due to color masking\n");
93 return GL_FALSE;
94 }
95
96 if (ctx->Color.AlphaEnabled) {
97 DBG("fallback due to alpha\n");
98 return GL_FALSE;
99 }
100
101 if (ctx->Depth.Test) {
102 DBG("fallback due to depth test\n");
103 return GL_FALSE;
104 }
105
106 if (ctx->Fog.Enabled) {
107 DBG("fallback due to fog\n");
108 return GL_FALSE;
109 }
110
111 if (ctx->_ImageTransferState) {
112 DBG("fallback due to image transfer\n");
113 return GL_FALSE;
114 }
115
116 if (ctx->Stencil._Enabled) {
117 DBG("fallback due to image stencil\n");
118 return GL_FALSE;
119 }
120
121 if (ctx->RenderMode != GL_RENDER) {
122 DBG("fallback due to render mode\n");
123 return GL_FALSE;
124 }
125
126 return GL_TRUE;
127 }
128
129
130 GLboolean
131 intel_check_meta_tex_fragment_ops(GLcontext * ctx)
132 {
133 if (ctx->NewState)
134 _mesa_update_state(ctx);
135
136 /* Some of _ImageTransferState (scale, bias) could be done with
137 * fragment programs on i915.
138 */
139 return !(ctx->_ImageTransferState || ctx->Fog.Enabled || /* not done yet */
140 ctx->Texture._EnabledUnits || ctx->FragmentProgram._Enabled);
141 }
142
143 /* The intel_region struct doesn't really do enough to capture the
144 * format of the pixels in the region. For now this code assumes that
145 * the region is a display surface and hence is either ARGB8888 or
146 * RGB565.
147 * XXX FBO: If we'd pass in the intel_renderbuffer instead of region, we'd
148 * know the buffer's pixel format.
149 *
150 * \param format as given to glDraw/ReadPixels
151 * \param type as given to glDraw/ReadPixels
152 */
153 GLboolean
154 intel_check_blit_format(struct intel_region * region,
155 GLenum format, GLenum type)
156 {
157 if (region->cpp == 4 &&
158 (type == GL_UNSIGNED_INT_8_8_8_8_REV ||
159 type == GL_UNSIGNED_BYTE) && format == GL_BGRA) {
160 return GL_TRUE;
161 }
162
163 if (region->cpp == 2 &&
164 type == GL_UNSIGNED_SHORT_5_6_5_REV && format == GL_BGR) {
165 return GL_TRUE;
166 }
167
168 if (INTEL_DEBUG & DEBUG_PIXEL)
169 fprintf(stderr, "%s: bad format for blit (cpp %d, type %s format %s)\n",
170 __FUNCTION__, region->cpp,
171 _mesa_lookup_enum_by_nr(type), _mesa_lookup_enum_by_nr(format));
172
173 return GL_FALSE;
174 }
175
176 void
177 intel_meta_set_passthrough_transform(struct intel_context *intel)
178 {
179 GLcontext *ctx = &intel->ctx;
180
181 intel->meta.saved_vp_x = ctx->Viewport.X;
182 intel->meta.saved_vp_y = ctx->Viewport.Y;
183 intel->meta.saved_vp_width = ctx->Viewport.Width;
184 intel->meta.saved_vp_height = ctx->Viewport.Height;
185 intel->meta.saved_matrix_mode = ctx->Transform.MatrixMode;
186
187 _mesa_Viewport(0, 0, ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
188
189 _mesa_MatrixMode(GL_PROJECTION);
190 _mesa_PushMatrix();
191 _mesa_LoadIdentity();
192 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
193
194 _mesa_MatrixMode(GL_MODELVIEW);
195 _mesa_PushMatrix();
196 _mesa_LoadIdentity();
197 }
198
199 void
200 intel_meta_restore_transform(struct intel_context *intel)
201 {
202 _mesa_MatrixMode(GL_PROJECTION);
203 _mesa_PopMatrix();
204 _mesa_MatrixMode(GL_MODELVIEW);
205 _mesa_PopMatrix();
206
207 _mesa_MatrixMode(intel->meta.saved_matrix_mode);
208
209 _mesa_Viewport(intel->meta.saved_vp_x, intel->meta.saved_vp_y,
210 intel->meta.saved_vp_width, intel->meta.saved_vp_height);
211 }
212
213 /**
214 * Set up a vertex program to pass through the position and first texcoord
215 * for pixel path.
216 */
217 void
218 intel_meta_set_passthrough_vertex_program(struct intel_context *intel)
219 {
220 GLcontext *ctx = &intel->ctx;
221 static const char *vp =
222 "!!ARBvp1.0\n"
223 "TEMP vertexClip;\n"
224 "DP4 vertexClip.x, state.matrix.mvp.row[0], vertex.position;\n"
225 "DP4 vertexClip.y, state.matrix.mvp.row[1], vertex.position;\n"
226 "DP4 vertexClip.z, state.matrix.mvp.row[2], vertex.position;\n"
227 "DP4 vertexClip.w, state.matrix.mvp.row[3], vertex.position;\n"
228 "MOV result.position, vertexClip;\n"
229 "MOV result.texcoord[0], vertex.texcoord[0];\n"
230 "MOV result.color, vertex.color;\n"
231 "END\n";
232
233 assert(intel->meta.saved_vp == NULL);
234
235 _mesa_reference_vertprog(ctx, &intel->meta.saved_vp,
236 ctx->VertexProgram.Current);
237 if (intel->meta.passthrough_vp == NULL) {
238 GLuint prog_name;
239 _mesa_GenPrograms(1, &prog_name);
240 _mesa_BindProgram(GL_VERTEX_PROGRAM_ARB, prog_name);
241 _mesa_ProgramStringARB(GL_VERTEX_PROGRAM_ARB,
242 GL_PROGRAM_FORMAT_ASCII_ARB,
243 strlen(vp), (const GLubyte *)vp);
244 _mesa_reference_vertprog(ctx, &intel->meta.passthrough_vp,
245 ctx->VertexProgram.Current);
246 _mesa_DeletePrograms(1, &prog_name);
247 }
248
249 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
250 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
251 intel->meta.passthrough_vp);
252 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
253 &intel->meta.passthrough_vp->Base);
254
255 intel->meta.saved_vp_enable = ctx->VertexProgram.Enabled;
256 _mesa_Enable(GL_VERTEX_PROGRAM_ARB);
257 }
258
259 /**
260 * Restores the previous vertex program after
261 * intel_meta_set_passthrough_vertex_program()
262 */
263 void
264 intel_meta_restore_vertex_program(struct intel_context *intel)
265 {
266 GLcontext *ctx = &intel->ctx;
267
268 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
269 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
270 intel->meta.saved_vp);
271 _mesa_reference_vertprog(ctx, &intel->meta.saved_vp, NULL);
272 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
273 &ctx->VertexProgram.Current->Base);
274
275 if (!intel->meta.saved_vp_enable)
276 _mesa_Disable(GL_VERTEX_PROGRAM_ARB);
277 }
278
279 /**
280 * Binds the given program string to GL_FRAGMENT_PROGRAM_ARB, caching the
281 * program object.
282 */
283 void
284 intel_meta_set_fragment_program(struct intel_context *intel,
285 struct gl_fragment_program **prog,
286 const char *prog_string)
287 {
288 GLcontext *ctx = &intel->ctx;
289 assert(intel->meta.saved_fp == NULL);
290
291 _mesa_reference_fragprog(ctx, &intel->meta.saved_fp,
292 ctx->FragmentProgram.Current);
293 if (*prog == NULL) {
294 GLuint prog_name;
295 _mesa_GenPrograms(1, &prog_name);
296 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, prog_name);
297 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
298 GL_PROGRAM_FORMAT_ASCII_ARB,
299 strlen(prog_string), (const GLubyte *)prog_string);
300 _mesa_reference_fragprog(ctx, prog, ctx->FragmentProgram.Current);
301 /* Note that DeletePrograms unbinds the program on us */
302 _mesa_DeletePrograms(1, &prog_name);
303 }
304
305 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
306 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, *prog);
307 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, &((*prog)->Base));
308
309 intel->meta.saved_fp_enable = ctx->FragmentProgram.Enabled;
310 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB);
311 }
312
313 /**
314 * Restores the previous fragment program after
315 * intel_meta_set_fragment_program()
316 */
317 void
318 intel_meta_restore_fragment_program(struct intel_context *intel)
319 {
320 GLcontext *ctx = &intel->ctx;
321
322 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
323 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
324 intel->meta.saved_fp);
325 _mesa_reference_fragprog(ctx, &intel->meta.saved_fp, NULL);
326 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
327 &ctx->FragmentProgram.Current->Base);
328
329 if (!intel->meta.saved_fp_enable)
330 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB);
331 }
332
333 void
334 intelInitPixelFuncs(struct dd_function_table *functions)
335 {
336 functions->Accum = _swrast_Accum;
337 if (!getenv("INTEL_NO_BLIT")) {
338 functions->Bitmap = intelBitmap;
339 functions->CopyPixels = intelCopyPixels;
340 functions->DrawPixels = intelDrawPixels;
341 #ifdef I915
342 functions->ReadPixels = intelReadPixels;
343 #endif
344 }
345 }
346
347 void
348 intel_free_pixel_state(struct intel_context *intel)
349 {
350 GLcontext *ctx = &intel->ctx;
351
352 _mesa_reference_vertprog(ctx, &intel->meta.passthrough_vp, NULL);
353 _mesa_reference_fragprog(ctx, &intel->meta.bitmap_fp, NULL);
354 }
355