Merge branch 'mesa_7_5_branch'
[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 intel->internal_viewport_call = GL_TRUE;
188 _mesa_Viewport(0, 0, ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
189 intel->internal_viewport_call = GL_FALSE;
190
191 _mesa_MatrixMode(GL_PROJECTION);
192 _mesa_PushMatrix();
193 _mesa_LoadIdentity();
194 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
195
196 _mesa_MatrixMode(GL_MODELVIEW);
197 _mesa_PushMatrix();
198 _mesa_LoadIdentity();
199 }
200
201 void
202 intel_meta_restore_transform(struct intel_context *intel)
203 {
204 _mesa_MatrixMode(GL_PROJECTION);
205 _mesa_PopMatrix();
206 _mesa_MatrixMode(GL_MODELVIEW);
207 _mesa_PopMatrix();
208
209 _mesa_MatrixMode(intel->meta.saved_matrix_mode);
210
211 intel->internal_viewport_call = GL_TRUE;
212 _mesa_Viewport(intel->meta.saved_vp_x, intel->meta.saved_vp_y,
213 intel->meta.saved_vp_width, intel->meta.saved_vp_height);
214 intel->internal_viewport_call = GL_FALSE;
215 }
216
217 /**
218 * Set up a vertex program to pass through the position and first texcoord
219 * for pixel path.
220 */
221 void
222 intel_meta_set_passthrough_vertex_program(struct intel_context *intel)
223 {
224 GLcontext *ctx = &intel->ctx;
225 static const char *vp =
226 "!!ARBvp1.0\n"
227 "TEMP vertexClip;\n"
228 "DP4 vertexClip.x, state.matrix.mvp.row[0], vertex.position;\n"
229 "DP4 vertexClip.y, state.matrix.mvp.row[1], vertex.position;\n"
230 "DP4 vertexClip.z, state.matrix.mvp.row[2], vertex.position;\n"
231 "DP4 vertexClip.w, state.matrix.mvp.row[3], vertex.position;\n"
232 "MOV result.position, vertexClip;\n"
233 "MOV result.texcoord[0], vertex.texcoord[0];\n"
234 "MOV result.color, vertex.color;\n"
235 "END\n";
236
237 assert(intel->meta.saved_vp == NULL);
238
239 _mesa_reference_vertprog(ctx, &intel->meta.saved_vp,
240 ctx->VertexProgram.Current);
241 if (intel->meta.passthrough_vp == NULL) {
242 GLuint prog_name;
243 _mesa_GenPrograms(1, &prog_name);
244 _mesa_BindProgram(GL_VERTEX_PROGRAM_ARB, prog_name);
245 _mesa_ProgramStringARB(GL_VERTEX_PROGRAM_ARB,
246 GL_PROGRAM_FORMAT_ASCII_ARB,
247 strlen(vp), (const GLubyte *)vp);
248 _mesa_reference_vertprog(ctx, &intel->meta.passthrough_vp,
249 ctx->VertexProgram.Current);
250 _mesa_DeletePrograms(1, &prog_name);
251 }
252
253 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
254 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
255 intel->meta.passthrough_vp);
256 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
257 &intel->meta.passthrough_vp->Base);
258
259 intel->meta.saved_vp_enable = ctx->VertexProgram.Enabled;
260 _mesa_Enable(GL_VERTEX_PROGRAM_ARB);
261 }
262
263 /**
264 * Restores the previous vertex program after
265 * intel_meta_set_passthrough_vertex_program()
266 */
267 void
268 intel_meta_restore_vertex_program(struct intel_context *intel)
269 {
270 GLcontext *ctx = &intel->ctx;
271
272 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
273 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
274 intel->meta.saved_vp);
275 _mesa_reference_vertprog(ctx, &intel->meta.saved_vp, NULL);
276 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
277 &ctx->VertexProgram.Current->Base);
278
279 if (!intel->meta.saved_vp_enable)
280 _mesa_Disable(GL_VERTEX_PROGRAM_ARB);
281 }
282
283 /**
284 * Binds the given program string to GL_FRAGMENT_PROGRAM_ARB, caching the
285 * program object.
286 */
287 void
288 intel_meta_set_fragment_program(struct intel_context *intel,
289 struct gl_fragment_program **prog,
290 const char *prog_string)
291 {
292 GLcontext *ctx = &intel->ctx;
293 assert(intel->meta.saved_fp == NULL);
294
295 _mesa_reference_fragprog(ctx, &intel->meta.saved_fp,
296 ctx->FragmentProgram.Current);
297 if (*prog == NULL) {
298 GLuint prog_name;
299 _mesa_GenPrograms(1, &prog_name);
300 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, prog_name);
301 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
302 GL_PROGRAM_FORMAT_ASCII_ARB,
303 strlen(prog_string), (const GLubyte *)prog_string);
304 _mesa_reference_fragprog(ctx, prog, ctx->FragmentProgram.Current);
305 /* Note that DeletePrograms unbinds the program on us */
306 _mesa_DeletePrograms(1, &prog_name);
307 }
308
309 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
310 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, *prog);
311 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, &((*prog)->Base));
312
313 intel->meta.saved_fp_enable = ctx->FragmentProgram.Enabled;
314 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB);
315 }
316
317 /**
318 * Restores the previous fragment program after
319 * intel_meta_set_fragment_program()
320 */
321 void
322 intel_meta_restore_fragment_program(struct intel_context *intel)
323 {
324 GLcontext *ctx = &intel->ctx;
325
326 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
327 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
328 intel->meta.saved_fp);
329 _mesa_reference_fragprog(ctx, &intel->meta.saved_fp, NULL);
330 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
331 &ctx->FragmentProgram.Current->Base);
332
333 if (!intel->meta.saved_fp_enable)
334 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB);
335 }
336
337 void
338 intelInitPixelFuncs(struct dd_function_table *functions)
339 {
340 functions->Accum = _swrast_Accum;
341 if (!getenv("INTEL_NO_BLIT")) {
342 functions->Bitmap = intelBitmap;
343 functions->CopyPixels = intelCopyPixels;
344 functions->DrawPixels = intelDrawPixels;
345 #ifdef I915
346 functions->ReadPixels = intelReadPixels;
347 #endif
348 }
349 }
350
351 void
352 intel_free_pixel_state(struct intel_context *intel)
353 {
354 GLcontext *ctx = &intel->ctx;
355
356 _mesa_reference_vertprog(ctx, &intel->meta.passthrough_vp, NULL);
357 _mesa_reference_fragprog(ctx, &intel->meta.bitmap_fp, NULL);
358 }
359