1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
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.
26 **************************************************************************/
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"
38 #include "intel_context.h"
39 #include "intel_pixel.h"
40 #include "intel_regions.h"
42 #define FILE_DEBUG_FLAG DEBUG_PIXEL
45 effective_func(GLenum func
, GLboolean src_alpha_is_one
)
47 if (src_alpha_is_one
) {
48 if (func
== GL_SRC_ALPHA
)
50 if (func
== GL_ONE_MINUS_SRC_ALPHA
)
58 * Check if any fragment operations are in effect which might effect
62 intel_check_blit_fragment_ops(GLcontext
* ctx
, GLboolean src_alpha_is_one
)
65 _mesa_update_state(ctx
);
67 if (ctx
->FragmentProgram
._Enabled
) {
68 DBG("fallback due to fragment program\n");
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");
83 if (ctx
->Texture
._EnabledUnits
) {
84 DBG("fallback due to texturing\n");
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");
96 if (ctx
->Color
.AlphaEnabled
) {
97 DBG("fallback due to alpha\n");
101 if (ctx
->Depth
.Test
) {
102 DBG("fallback due to depth test\n");
106 if (ctx
->Fog
.Enabled
) {
107 DBG("fallback due to fog\n");
111 if (ctx
->_ImageTransferState
) {
112 DBG("fallback due to image transfer\n");
116 if (ctx
->Stencil
._Enabled
) {
117 DBG("fallback due to image stencil\n");
121 if (ctx
->RenderMode
!= GL_RENDER
) {
122 DBG("fallback due to render mode\n");
131 intel_check_meta_tex_fragment_ops(GLcontext
* ctx
)
134 _mesa_update_state(ctx
);
136 /* Some of _ImageTransferState (scale, bias) could be done with
137 * fragment programs on i915.
139 return !(ctx
->_ImageTransferState
|| ctx
->Fog
.Enabled
|| /* not done yet */
140 ctx
->Texture
._EnabledUnits
|| ctx
->FragmentProgram
._Enabled
);
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
147 * XXX FBO: If we'd pass in the intel_renderbuffer instead of region, we'd
148 * know the buffer's pixel format.
150 * \param format as given to glDraw/ReadPixels
151 * \param type as given to glDraw/ReadPixels
154 intel_check_blit_format(struct intel_region
* region
,
155 GLenum format
, GLenum type
)
157 if (region
->cpp
== 4 &&
158 (type
== GL_UNSIGNED_INT_8_8_8_8_REV
||
159 type
== GL_UNSIGNED_BYTE
) && format
== GL_BGRA
) {
163 if (region
->cpp
== 2 &&
164 type
== GL_UNSIGNED_SHORT_5_6_5_REV
&& format
== GL_BGR
) {
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
));
177 intel_meta_set_passthrough_transform(struct intel_context
*intel
)
179 GLcontext
*ctx
= &intel
->ctx
;
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
;
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
;
191 _mesa_MatrixMode(GL_PROJECTION
);
193 _mesa_LoadIdentity();
194 _mesa_Ortho(0, ctx
->DrawBuffer
->Width
, 0, ctx
->DrawBuffer
->Height
, 1, -1);
196 _mesa_MatrixMode(GL_MODELVIEW
);
198 _mesa_LoadIdentity();
202 intel_meta_restore_transform(struct intel_context
*intel
)
204 _mesa_MatrixMode(GL_PROJECTION
);
206 _mesa_MatrixMode(GL_MODELVIEW
);
209 _mesa_MatrixMode(intel
->meta
.saved_matrix_mode
);
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
;
218 * Set up a vertex program to pass through the position and first texcoord
222 intel_meta_set_passthrough_vertex_program(struct intel_context
*intel
)
224 GLcontext
*ctx
= &intel
->ctx
;
225 static const char *vp
=
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"
237 assert(intel
->meta
.saved_vp
== NULL
);
239 _mesa_reference_vertprog(ctx
, &intel
->meta
.saved_vp
,
240 ctx
->VertexProgram
.Current
);
241 if (intel
->meta
.passthrough_vp
== NULL
) {
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
);
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
);
259 intel
->meta
.saved_vp_enable
= ctx
->VertexProgram
.Enabled
;
260 _mesa_Enable(GL_VERTEX_PROGRAM_ARB
);
264 * Restores the previous vertex program after
265 * intel_meta_set_passthrough_vertex_program()
268 intel_meta_restore_vertex_program(struct intel_context
*intel
)
270 GLcontext
*ctx
= &intel
->ctx
;
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
);
279 if (!intel
->meta
.saved_vp_enable
)
280 _mesa_Disable(GL_VERTEX_PROGRAM_ARB
);
284 * Binds the given program string to GL_FRAGMENT_PROGRAM_ARB, caching the
288 intel_meta_set_fragment_program(struct intel_context
*intel
,
289 struct gl_fragment_program
**prog
,
290 const char *prog_string
)
292 GLcontext
*ctx
= &intel
->ctx
;
293 assert(intel
->meta
.saved_fp
== NULL
);
295 _mesa_reference_fragprog(ctx
, &intel
->meta
.saved_fp
,
296 ctx
->FragmentProgram
.Current
);
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
);
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
));
313 intel
->meta
.saved_fp_enable
= ctx
->FragmentProgram
.Enabled
;
314 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB
);
318 * Restores the previous fragment program after
319 * intel_meta_set_fragment_program()
322 intel_meta_restore_fragment_program(struct intel_context
*intel
)
324 GLcontext
*ctx
= &intel
->ctx
;
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
);
333 if (!intel
->meta
.saved_fp_enable
)
334 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB
);
338 intelInitPixelFuncs(struct dd_function_table
*functions
)
340 functions
->Accum
= _swrast_Accum
;
341 if (!getenv("INTEL_NO_BLIT")) {
342 functions
->Bitmap
= intelBitmap
;
343 functions
->CopyPixels
= intelCopyPixels
;
344 functions
->DrawPixels
= intelDrawPixels
;
346 functions
->ReadPixels
= intelReadPixels
;
350 intel_free_pixel_state(struct intel_context
*intel
)
352 GLcontext
*ctx
= &intel
->ctx
;
354 _mesa_reference_vertprog(ctx
, &intel
->meta
.passthrough_vp
, NULL
);
355 _mesa_reference_fragprog(ctx
, &intel
->meta
.bitmap_fp
, NULL
);