1 /**************************************************************************
3 * Copyright 2007 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 portions
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 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "shader/program.h"
39 #include "shader/prog_parameter.h"
40 #include "shader/prog_print.h"
42 #include "st_context.h"
44 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_drawpixels.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_cb_texture.h"
52 #include "st_format.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "st_texture.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_defines.h"
57 #include "pipe/p_inlines.h"
58 #include "util/p_tile.h"
59 #include "util/u_draw_quad.h"
60 #include "shader/prog_instruction.h"
61 #include "cso_cache/cso_context.h"
65 * Check if the given program is:
66 * 0: MOVE result.color, fragment.color;
70 is_passthrough_program(const struct gl_fragment_program
*prog
)
72 if (prog
->Base
.NumInstructions
== 2) {
73 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
74 if (inst
[0].Opcode
== OPCODE_MOV
&&
75 inst
[1].Opcode
== OPCODE_END
&&
76 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
77 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
78 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
79 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
80 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
81 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
91 * Make fragment shader for glDraw/CopyPixels. This shader is made
92 * by combining the pixel transfer shader with the user-defined shader.
94 static struct st_fragment_program
*
95 combined_drawpix_fragment_program(GLcontext
*ctx
)
97 struct st_context
*st
= ctx
->st
;
98 struct st_fragment_program
*stfp
;
100 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
101 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
102 /* the pixel tranfer program has not changed and the user-defined
103 * program has not changed, so re-use the combined program.
105 stfp
= st
->pixel_xfer
.combined_prog
;
108 /* Concatenate the pixel transfer program with the current user-
111 if (is_passthrough_program(&st
->fp
->Base
)) {
112 stfp
= (struct st_fragment_program
*)
113 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
117 printf("Base program:\n");
118 _mesa_print_program(&st
->fp
->Base
.Base
);
119 printf("DrawPix program:\n");
120 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
122 stfp
= (struct st_fragment_program
*)
123 _mesa_combine_programs(ctx
,
124 &st
->pixel_xfer
.program
->Base
.Base
,
130 struct gl_program
*p
= &stfp
->Base
.Base
;
131 printf("Combined DrawPixels program:\n");
132 _mesa_print_program(p
);
133 printf("InputsRead: 0x%x\n", p
->InputsRead
);
134 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
135 _mesa_print_parameter_list(p
->Parameters
);
139 /* translate to TGSI tokens */
140 st_translate_fragment_program(st
, stfp
, NULL
);
142 /* save new program, update serial numbers */
143 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
144 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
145 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
146 st
->pixel_xfer
.combined_prog
= stfp
;
149 /* Ideally we'd have updated the pipe constants during the normal
150 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
152 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
159 * Create fragment shader that does a TEX() instruction to get a Z
160 * value, then writes to FRAG_RESULT_DEPR.
161 * Pass fragment color through as-is.
163 static struct st_fragment_program
*
164 make_fragment_shader_z(struct st_context
*st
)
166 GLcontext
*ctx
= st
->ctx
;
167 /* only make programs once and re-use */
168 static struct st_fragment_program
*stfp
= NULL
;
169 struct gl_program
*p
;
175 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
179 p
->NumInstructions
= 3;
181 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
182 if (!p
->Instructions
) {
183 ctx
->Driver
.DeleteProgram(ctx
, p
);
186 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
188 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
189 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
190 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
191 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPR
;
192 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
193 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
194 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
195 p
->Instructions
[ic
].TexSrcUnit
= 0;
196 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
199 /* MOV result.color, fragment.color */
200 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
201 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
202 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
203 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
204 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
208 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
210 assert(ic
== p
->NumInstructions
);
212 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
213 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
214 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
216 stfp
= (struct st_fragment_program
*) p
;
217 st_translate_fragment_program(st
, stfp
, NULL
);
225 * Create a simple vertex shader that just passes through the
226 * vertex position and texcoord (and optionally, color).
228 static struct st_vertex_program
*
229 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
231 /* only make programs once and re-use */
232 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
233 GLcontext
*ctx
= st
->ctx
;
234 struct st_vertex_program
*stvp
;
235 struct gl_program
*p
;
238 if (progs
[passColor
])
239 return progs
[passColor
];
241 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
246 p
->NumInstructions
= 4;
248 p
->NumInstructions
= 3;
250 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
251 if (!p
->Instructions
) {
252 ctx
->Driver
.DeleteProgram(ctx
, p
);
255 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
256 /* MOV result.pos, vertex.pos; */
257 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
258 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
259 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
260 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
261 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
262 /* MOV result.texcoord0, vertex.texcoord0; */
263 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
264 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
265 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
266 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
267 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
270 /* MOV result.color0, vertex.color0; */
271 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
272 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
273 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
274 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
275 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
280 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
283 assert(ic
== p
->NumInstructions
);
285 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
286 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
287 (1 << VERT_RESULT_HPOS
));
289 p
->InputsRead
|= VERT_BIT_COLOR0
;
290 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
293 stvp
= (struct st_vertex_program
*) p
;
294 st_translate_vertex_program(st
, stvp
, NULL
);
296 progs
[passColor
] = stvp
;
303 _mesa_base_format(GLenum format
)
306 case GL_DEPTH_COMPONENT
:
307 return GL_DEPTH_COMPONENT
;
308 case GL_STENCIL_INDEX
:
309 return GL_STENCIL_INDEX
;
317 * Make texture containing an image for glDrawPixels image.
318 * If 'pixels' is NULL, leave the texture image data undefined.
320 static struct pipe_texture
*
321 make_texture(struct st_context
*st
,
322 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
323 const struct gl_pixelstore_attrib
*unpack
,
324 const GLvoid
*pixels
)
326 GLcontext
*ctx
= st
->ctx
;
327 struct pipe_context
*pipe
= st
->pipe
;
328 struct pipe_screen
*screen
= pipe
->screen
;
329 const struct gl_texture_format
*mformat
;
330 struct pipe_texture
*pt
;
331 enum pipe_format pipeFormat
;
335 baseFormat
= _mesa_base_format(format
);
337 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
340 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
342 cpp
= st_sizeof_format(pipeFormat
);
344 pixels
= _mesa_map_drawpix_pbo(ctx
, unpack
, pixels
);
348 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, width
, height
,
350 PIPE_TEXTURE_USAGE_SAMPLER
);
352 _mesa_unmap_drawpix_pbo(ctx
, unpack
);
357 struct pipe_surface
*surface
;
358 static const GLuint dstImageOffsets
= 0;
361 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
363 /* we'll do pixel transfer in a fragment shader */
364 ctx
->_ImageTransferState
= 0x0;
366 surface
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
367 PIPE_BUFFER_USAGE_CPU_WRITE
);
369 /* map texture surface */
370 dest
= screen
->surface_map(screen
, surface
,
371 PIPE_BUFFER_USAGE_CPU_WRITE
);
373 /* Put image into texture surface.
374 * Note that the image is actually going to be upside down in
375 * the texture. We deal with that with texcoords.
377 success
= mformat
->StoreImage(ctx
, 2, /* dims */
378 baseFormat
, /* baseInternalFormat */
379 mformat
, /* gl_texture_format */
381 0, 0, 0, /* dstX/Y/Zoffset */
382 surface
->pitch
* cpp
, /* dstRowStride, bytes */
383 &dstImageOffsets
, /* dstImageOffsets */
384 width
, height
, 1, /* size */
385 format
, type
, /* src format/type */
386 pixels
, /* data source */
390 screen
->surface_unmap(screen
, surface
);
391 pipe_surface_reference(&surface
, NULL
);
396 ctx
->_ImageTransferState
= imageTransferStateSave
;
399 _mesa_unmap_drawpix_pbo(ctx
, unpack
);
406 * Draw quad with texcoords and optional color.
407 * Coords are window coords with y=0=bottom.
408 * \param color may be null
409 * \param invertTex if true, flip texcoords vertically
412 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
413 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
416 struct st_context
*st
= ctx
->st
;
417 struct pipe_context
*pipe
= ctx
->st
->pipe
;
418 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
420 /* setup vertex data */
422 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
423 const GLfloat fb_width
= fb
->Width
;
424 const GLfloat fb_height
= fb
->Height
;
425 const GLfloat clip_x0
= x0
/ fb_width
* 2.0 - 1.0;
426 const GLfloat clip_y0
= y0
/ fb_height
* 2.0 - 1.0;
427 const GLfloat clip_x1
= x1
/ fb_width
* 2.0 - 1.0;
428 const GLfloat clip_y1
= y1
/ fb_height
* 2.0 - 1.0;
429 const GLfloat sLeft
= 0.0F
, sRight
= 1.0F
;
430 const GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
434 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
435 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
438 verts
[1][0][0] = clip_x1
;
439 verts
[1][0][1] = clip_y0
;
442 verts
[2][0][0] = clip_x1
;
443 verts
[2][0][1] = clip_y1
;
446 verts
[3][0][0] = clip_x0
;
447 verts
[3][0][1] = clip_y1
;
450 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
451 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
452 verts
[1][tex
][0] = sRight
;
453 verts
[1][tex
][1] = tTop
;
454 verts
[2][tex
][0] = sRight
;
455 verts
[2][tex
][1] = tBot
;
456 verts
[3][tex
][0] = sLeft
;
457 verts
[3][tex
][1] = tBot
;
459 /* same for all verts: */
461 for (i
= 0; i
< 4; i
++) {
462 verts
[i
][0][2] = z
; /*Z*/
463 verts
[i
][0][3] = 1.0; /*W*/
464 verts
[i
][1][0] = color
[0];
465 verts
[i
][1][1] = color
[1];
466 verts
[i
][1][2] = color
[2];
467 verts
[i
][1][3] = color
[3];
468 verts
[i
][2][2] = 0.0; /*R*/
469 verts
[i
][2][3] = 1.0; /*Q*/
473 for (i
= 0; i
< 4; i
++) {
474 verts
[i
][0][2] = z
; /*Z*/
475 verts
[i
][0][3] = 1.0; /*W*/
476 verts
[i
][1][2] = 0.0; /*R*/
477 verts
[i
][1][3] = 1.0; /*Q*/
483 struct pipe_buffer
*buf
;
486 /* allocate/load buffer object with vertex data */
487 buf
= pipe_buffer_create(pipe
, 32, PIPE_BUFFER_USAGE_VERTEX
,
489 map
= pipe_buffer_map(pipe
, buf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
490 memcpy(map
, verts
, sizeof(verts
));
491 pipe_buffer_unmap(pipe
, buf
);
493 util_draw_vertex_buffer(pipe
, buf
,
496 3); /* attribs/vert */
497 pipe_buffer_reference(pipe
->winsys
, &buf
, NULL
);
504 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
505 GLsizei width
, GLsizei height
,
506 GLfloat zoomX
, GLfloat zoomY
,
507 struct pipe_texture
*pt
,
508 struct st_vertex_program
*stvp
,
509 struct st_fragment_program
*stfp
,
510 const GLfloat
*color
,
513 struct st_context
*st
= ctx
->st
;
514 struct pipe_context
*pipe
= ctx
->st
->pipe
;
515 struct cso_context
*cso
= ctx
->st
->cso_context
;
516 GLfloat x0
, y0
, x1
, y1
;
520 /* XXX if DrawPixels image is larger than max texture size, break
523 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
524 assert(width
<= maxSize
);
525 assert(height
<= maxSize
);
527 cso_save_rasterizer(cso
);
528 cso_save_viewport(cso
);
529 cso_save_samplers(cso
);
530 cso_save_sampler_textures(cso
);
531 cso_save_fragment_shader(cso
);
532 cso_save_vertex_shader(cso
);
534 /* rasterizer state: just scissor */
536 struct pipe_rasterizer_state rasterizer
;
537 memset(&rasterizer
, 0, sizeof(rasterizer
));
538 rasterizer
.gl_rasterization_rules
= 1;
539 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
540 cso_set_rasterizer(cso
, &rasterizer
);
543 /* fragment shader state: TEX lookup program */
544 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
546 /* vertex shader state: position + texcoord pass-through */
547 cso_set_vertex_shader_handle(cso
, stvp
->driver_shader
);
550 /* texture sampling state: */
552 struct pipe_sampler_state sampler
;
553 memset(&sampler
, 0, sizeof(sampler
));
554 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
555 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
556 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
557 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
558 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
559 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
560 sampler
.normalized_coords
= 1;
562 cso_single_sampler(cso
, 0, &sampler
);
563 if (st
->pixel_xfer
.pixelmap_enabled
) {
564 cso_single_sampler(cso
, 1, &sampler
);
566 cso_single_sampler_done(cso
);
569 /* viewport state: viewport matching window dims */
571 const float width
= ctx
->DrawBuffer
->Width
;
572 const float height
= ctx
->DrawBuffer
->Height
;
573 struct pipe_viewport_state vp
;
574 vp
.scale
[0] = 0.5 * width
;
575 vp
.scale
[1] = -0.5 * height
;
578 vp
.translate
[0] = 0.5 * width
;
579 vp
.translate
[1] = 0.5 * height
;
580 vp
.translate
[2] = 0.0;
581 vp
.translate
[3] = 0.0;
582 cso_set_viewport(cso
, &vp
);
586 if (st
->pixel_xfer
.pixelmap_enabled
) {
587 struct pipe_texture
*textures
[2];
589 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
590 pipe
->set_sampler_textures(pipe
, 2, textures
);
593 pipe
->set_sampler_textures(pipe
, 1, &pt
);
596 /* Compute window coords (y=0=bottom) with pixel zoom.
597 * Recall that these coords are transformed by the current
598 * vertex shader and viewport transformation.
601 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
603 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
605 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
607 //printf("skip draw quad\n");
609 cso_restore_rasterizer(cso
);
610 cso_restore_viewport(cso
);
611 cso_restore_samplers(cso
);
612 cso_restore_sampler_textures(cso
);
613 cso_restore_fragment_shader(cso
);
614 cso_restore_vertex_shader(cso
);
619 * Check if a GL format/type combination is a match to the given pipe format.
620 * XXX probably move this to a re-usable place.
623 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
625 static const GLuint one
= 1;
626 GLubyte littleEndian
= *((GLubyte
*) &one
);
628 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
630 type
== GL_UNSIGNED_BYTE
&&
634 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
635 format
== GL_ABGR_EXT
&&
636 type
== GL_UNSIGNED_BYTE
&&
640 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
642 type
== GL_UNSIGNED_BYTE
&&
646 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
648 type
== GL_UNSIGNED_SHORT_5_6_5
) {
649 /* endian don't care */
652 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
654 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
655 /* endian don't care */
658 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
659 format
== GL_STENCIL_INDEX
&&
660 type
== GL_UNSIGNED_BYTE
) {
663 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
664 format
== GL_DEPTH_COMPONENT
&&
665 type
== GL_UNSIGNED_INT
) {
668 /* XXX add more cases */
676 * Check if any per-fragment ops are enabled.
677 * XXX probably move this to a re-usable place.
680 any_fragment_ops(const struct st_context
*st
)
682 if (st
->state
.depth_stencil
.alpha
.enabled
||
683 st
->state
.depth_stencil
.depth
.enabled
||
684 st
->state
.blend
.blend_enable
||
685 st
->state
.blend
.logicop_enable
)
686 /* XXX more checks */
694 * Check if any pixel transfer ops are enabled.
695 * XXX probably move this to a re-usable place.
698 any_pixel_transfer_ops(const struct st_context
*st
)
700 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
701 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
702 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
703 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
704 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
705 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
706 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
707 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
708 st
->ctx
->Pixel
.MapColorFlag
)
709 /* XXX more checks */
717 * Draw image with a blit, or other non-textured quad method.
720 draw_blit(struct st_context
*st
,
721 GLsizei width
, GLsizei height
,
722 GLenum format
, GLenum type
, const GLvoid
*pixels
)
730 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
731 GLsizei width
, GLsizei height
, GLenum type
,
732 const struct gl_pixelstore_attrib
*unpack
,
733 const GLvoid
*pixels
)
735 struct st_context
*st
= ctx
->st
;
736 struct pipe_context
*pipe
= st
->pipe
;
737 struct pipe_screen
*screen
= pipe
->screen
;
738 struct st_renderbuffer
*strb
;
739 struct pipe_surface
*ps
;
740 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
744 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
746 strb
= st_renderbuffer(ctx
->DrawBuffer
->
747 Attachment
[BUFFER_STENCIL
].Renderbuffer
);
748 ps
= screen
->get_tex_surface(screen
, strb
->texture
, 0, 0, 0,
749 PIPE_BUFFER_USAGE_CPU_WRITE
);
751 /* map the stencil buffer */
752 stmap
= screen
->surface_map(screen
, ps
,
753 PIPE_BUFFER_USAGE_CPU_WRITE
);
755 /* if width > MAX_WIDTH, have to process image in chunks */
757 while (skipPixels
< width
) {
758 const GLint spanX
= x
+ skipPixels
;
759 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
761 for (row
= 0; row
< height
; row
++) {
762 GLint spanY
= y
+ row
;
763 GLubyte values
[MAX_WIDTH
];
764 GLenum destType
= GL_UNSIGNED_BYTE
;
765 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
767 GL_COLOR_INDEX
, type
,
769 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
770 type
, source
, unpack
,
771 ctx
->_ImageTransferState
);
774 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
775 spanX, spanY, values);
779 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
780 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
783 switch (ps
->format
) {
784 case PIPE_FORMAT_U_S8
:
786 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
787 memcpy(dest
, values
, spanWidth
);
790 case PIPE_FORMAT_S8Z24_UNORM
:
792 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
794 for (k
= 0; k
< spanWidth
; k
++) {
796 p
= (p
& 0xffffff) | (values
[k
] << 24);
806 skipPixels
+= spanWidth
;
809 /* unmap the stencil buffer */
810 screen
->surface_unmap(screen
, ps
);
811 pipe_surface_reference(&ps
, NULL
);
816 * Called via ctx->Driver.DrawPixels()
819 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
820 GLenum format
, GLenum type
,
821 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
823 struct st_fragment_program
*stfp
;
824 struct st_vertex_program
*stvp
;
825 struct st_context
*st
= ctx
->st
;
826 struct pipe_surface
*ps
;
828 const GLfloat
*color
;
830 if (format
== GL_STENCIL_INDEX
) {
831 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
835 st_validate_state(st
);
837 if (format
== GL_DEPTH_COMPONENT
) {
838 ps
= st
->state
.framebuffer
.zsbuf
;
839 stfp
= make_fragment_shader_z(ctx
->st
);
840 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
841 color
= ctx
->Current
.RasterColor
;
843 else if (format
== GL_STENCIL_INDEX
) {
844 ps
= st
->state
.framebuffer
.zsbuf
;
845 /* XXX special case - can't use texture map */
849 ps
= st
->state
.framebuffer
.cbufs
[0];
850 stfp
= combined_drawpix_fragment_program(ctx
);
851 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
855 bufferFormat
= ps
->format
;
857 if (1/*any_fragment_ops(st) ||
858 any_pixel_transfer_ops(st) ||
859 !compatible_formats(format, type, ps->format)*/) {
861 struct pipe_texture
*pt
862 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
864 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
865 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
866 pt
, stvp
, stfp
, color
, GL_FALSE
);
867 pipe_texture_reference(&pt
, NULL
);
872 draw_blit(st
, width
, height
, format
, type
, pixels
);
879 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
880 GLsizei width
, GLsizei height
,
881 GLint dstx
, GLint dsty
)
883 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
884 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
885 struct pipe_surface
*psDraw
;
890 buffer
= malloc(width
* height
* sizeof(ubyte
));
892 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
896 /* this will do stencil pixel transfer ops */
897 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
898 &ctx
->DefaultPacking
, buffer
);
900 psDraw
= screen
->get_tex_surface(screen
, rbDraw
->texture
, 0, 0, 0,
901 PIPE_BUFFER_USAGE_CPU_WRITE
);
903 /* map the stencil buffer */
904 drawMap
= screen
->surface_map(screen
, psDraw
, PIPE_BUFFER_USAGE_CPU_WRITE
);
907 /* XXX PixelZoom not handled yet */
908 for (i
= 0; i
< height
; i
++) {
915 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
916 y
= ctx
->DrawBuffer
->Height
- y
- 1;
919 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
920 src
= buffer
+ i
* width
;
922 switch (psDraw
->format
) {
923 case PIPE_FORMAT_S8Z24_UNORM
:
925 uint
*dst4
= (uint
*) dst
;
927 for (j
= 0; j
< width
; j
++) {
928 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
933 case PIPE_FORMAT_U_S8
:
934 memcpy(dst
, src
, width
);
943 /* unmap the stencil buffer */
944 screen
->surface_unmap(screen
, psDraw
);
945 pipe_surface_reference(&psDraw
, NULL
);
950 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
951 GLsizei width
, GLsizei height
,
952 GLint dstx
, GLint dsty
, GLenum type
)
954 struct st_context
*st
= ctx
->st
;
955 struct pipe_context
*pipe
= st
->pipe
;
956 struct pipe_screen
*screen
= pipe
->screen
;
957 struct st_renderbuffer
*rbRead
;
958 struct st_vertex_program
*stvp
;
959 struct st_fragment_program
*stfp
;
960 struct pipe_surface
*psTex
;
961 struct pipe_texture
*pt
;
963 enum pipe_format srcFormat
, texFormat
;
965 /* make sure rendering has completed */
966 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
968 st_validate_state(st
);
970 if (type
== GL_STENCIL
) {
971 /* can't use texturing to do stencil */
972 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
976 if (type
== GL_COLOR
) {
977 rbRead
= st_get_color_read_renderbuffer(ctx
);
979 stfp
= combined_drawpix_fragment_program(ctx
);
980 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
983 assert(type
== GL_DEPTH
);
984 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
985 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
986 stfp
= make_fragment_shader_z(ctx
->st
);
987 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
990 srcFormat
= rbRead
->texture
->format
;
992 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE
)) {
993 texFormat
= srcFormat
;
996 /* srcFormat can't be used as a texture format */
997 if (type
== GL_DEPTH
) {
998 texFormat
= st_choose_format(pipe
, GL_DEPTH_COMPONENT
, PIPE_TEXTURE
);
999 assert(texFormat
!= PIPE_FORMAT_NONE
); /* XXX no depth texture formats??? */
1002 /* default color format */
1003 texFormat
= st_choose_format(pipe
, GL_RGBA
, PIPE_TEXTURE
);
1004 assert(texFormat
!= PIPE_FORMAT_NONE
);
1008 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1009 width
, height
, 1, 0,
1010 PIPE_TEXTURE_USAGE_SAMPLER
);
1014 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1015 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1018 if (srcFormat
== texFormat
) {
1019 /* copy source framebuffer surface into mipmap/texture */
1020 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1021 rbRead
->texture
, 0, 0, 0,
1022 PIPE_BUFFER_USAGE_GPU_READ
);
1023 psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1024 PIPE_BUFFER_USAGE_GPU_WRITE
);
1025 pipe
->surface_copy(pipe
,
1030 srcx
, srcy
, width
, height
);
1031 pipe_surface_reference(&psRead
, NULL
);
1034 /* CPU-based fallback/conversion */
1035 struct pipe_surface
*psRead
= screen
->get_tex_surface(screen
,
1036 rbRead
->texture
, 0, 0, 0,
1037 PIPE_BUFFER_USAGE_CPU_READ
);
1039 psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
1040 PIPE_BUFFER_USAGE_CPU_WRITE
);
1042 if (type
== GL_COLOR
) {
1043 /* alternate path using get/put_tile() */
1044 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1046 pipe_get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1047 pipe_put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1053 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1054 pipe_get_tile_z(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1055 pipe_put_tile_z(pipe
, psTex
, 0, 0, width
, height
, buf
);
1058 pipe_surface_reference(&psRead
, NULL
);
1061 pipe_surface_reference(&psTex
, NULL
);
1063 /* draw textured quad */
1064 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1065 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1066 pt
, stvp
, stfp
, color
, GL_TRUE
);
1068 pipe_texture_reference(&pt
, NULL
);
1073 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1075 functions
->DrawPixels
= st_DrawPixels
;
1076 functions
->CopyPixels
= st_CopyPixels
;