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 "pipe/p_winsys.h"
59 #include "util/p_tile.h"
60 #include "util/u_draw_quad.h"
61 #include "shader/prog_instruction.h"
62 #include "cso_cache/cso_context.h"
66 * Check if the given program is:
67 * 0: MOVE result.color, fragment.color;
71 is_passthrough_program(const struct gl_fragment_program
*prog
)
73 if (prog
->Base
.NumInstructions
== 2) {
74 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
75 if (inst
[0].Opcode
== OPCODE_MOV
&&
76 inst
[1].Opcode
== OPCODE_END
&&
77 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
78 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
79 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
80 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
81 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
82 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
92 * Make fragment shader for glDraw/CopyPixels. This shader is made
93 * by combining the pixel transfer shader with the user-defined shader.
95 static struct st_fragment_program
*
96 combined_drawpix_fragment_program(GLcontext
*ctx
)
98 struct st_context
*st
= ctx
->st
;
99 struct st_fragment_program
*stfp
;
101 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
102 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
103 /* the pixel tranfer program has not changed and the user-defined
104 * program has not changed, so re-use the combined program.
106 stfp
= st
->pixel_xfer
.combined_prog
;
109 /* Concatenate the pixel transfer program with the current user-
112 if (is_passthrough_program(&st
->fp
->Base
)) {
113 stfp
= (struct st_fragment_program
*)
114 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
118 printf("Base program:\n");
119 _mesa_print_program(&st
->fp
->Base
.Base
);
120 printf("DrawPix program:\n");
121 _mesa_print_program(&st
->pixel_xfer
.program
->Base
.Base
);
123 stfp
= (struct st_fragment_program
*)
124 _mesa_combine_programs(ctx
,
125 &st
->pixel_xfer
.program
->Base
.Base
,
131 struct gl_program
*p
= &stfp
->Base
.Base
;
132 printf("Combined DrawPixels program:\n");
133 _mesa_print_program(p
);
134 printf("InputsRead: 0x%x\n", p
->InputsRead
);
135 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
136 _mesa_print_parameter_list(p
->Parameters
);
140 /* translate to TGSI tokens */
141 st_translate_fragment_program(st
, stfp
, NULL
);
143 /* save new program, update serial numbers */
144 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
145 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
146 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
147 st
->pixel_xfer
.combined_prog
= stfp
;
150 /* Ideally we'd have updated the pipe constants during the normal
151 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
153 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
160 * Create fragment shader that does a TEX() instruction to get a Z
161 * value, then writes to FRAG_RESULT_DEPR.
162 * Pass fragment color through as-is.
164 static struct st_fragment_program
*
165 make_fragment_shader_z(struct st_context
*st
)
167 GLcontext
*ctx
= st
->ctx
;
168 /* only make programs once and re-use */
169 static struct st_fragment_program
*stfp
= NULL
;
170 struct gl_program
*p
;
176 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
180 p
->NumInstructions
= 3;
182 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
183 if (!p
->Instructions
) {
184 ctx
->Driver
.DeleteProgram(ctx
, p
);
187 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
189 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
190 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
191 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
192 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPR
;
193 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
194 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
195 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
196 p
->Instructions
[ic
].TexSrcUnit
= 0;
197 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
200 /* MOV result.color, fragment.color */
201 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
202 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
203 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
204 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
205 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
209 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
211 assert(ic
== p
->NumInstructions
);
213 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
214 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
215 p
->SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
217 stfp
= (struct st_fragment_program
*) p
;
218 st_translate_fragment_program(st
, stfp
, NULL
);
226 * Create a simple vertex shader that just passes through the
227 * vertex position and texcoord (and optionally, color).
229 static struct st_vertex_program
*
230 st_make_passthrough_vertex_shader(struct st_context
*st
, GLboolean passColor
)
232 /* only make programs once and re-use */
233 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
234 GLcontext
*ctx
= st
->ctx
;
235 struct st_vertex_program
*stvp
;
236 struct gl_program
*p
;
239 if (progs
[passColor
])
240 return progs
[passColor
];
242 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
247 p
->NumInstructions
= 4;
249 p
->NumInstructions
= 3;
251 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
252 if (!p
->Instructions
) {
253 ctx
->Driver
.DeleteProgram(ctx
, p
);
256 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
257 /* MOV result.pos, vertex.pos; */
258 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
259 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
260 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
261 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
262 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
263 /* MOV result.texcoord0, vertex.texcoord0; */
264 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
265 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
266 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
267 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
268 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
271 /* MOV result.color0, vertex.color0; */
272 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
273 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
274 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
275 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
276 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
281 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
284 assert(ic
== p
->NumInstructions
);
286 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
287 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
288 (1 << VERT_RESULT_HPOS
));
290 p
->InputsRead
|= VERT_BIT_COLOR0
;
291 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
294 stvp
= (struct st_vertex_program
*) p
;
295 st_translate_vertex_program(st
, stvp
, NULL
);
297 progs
[passColor
] = stvp
;
304 _mesa_base_format(GLenum format
)
307 case GL_DEPTH_COMPONENT
:
308 return GL_DEPTH_COMPONENT
;
309 case GL_STENCIL_INDEX
:
310 return GL_STENCIL_INDEX
;
318 * Make texture containing an image for glDrawPixels image.
319 * If 'pixels' is NULL, leave the texture image data undefined.
321 static struct pipe_texture
*
322 make_texture(struct st_context
*st
,
323 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
324 const struct gl_pixelstore_attrib
*unpack
,
325 const GLvoid
*pixels
)
327 GLcontext
*ctx
= st
->ctx
;
328 struct pipe_context
*pipe
= st
->pipe
;
329 struct pipe_screen
*screen
= pipe
->screen
;
330 const struct gl_texture_format
*mformat
;
331 struct pipe_texture
*pt
;
332 enum pipe_format pipeFormat
;
336 baseFormat
= _mesa_base_format(format
);
338 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
341 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
343 cpp
= st_sizeof_format(pipeFormat
);
345 pixels
= _mesa_map_drawpix_pbo(ctx
, unpack
, pixels
);
349 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, pipeFormat
, 0, width
, height
,
352 _mesa_unmap_drapix_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);
368 /* map texture surface */
369 dest
= pipe_surface_map(surface
);
371 /* Put image into texture surface.
372 * Note that the image is actually going to be upside down in
373 * the texture. We deal with that with texcoords.
375 success
= mformat
->StoreImage(ctx
, 2, /* dims */
376 baseFormat
, /* baseInternalFormat */
377 mformat
, /* gl_texture_format */
379 0, 0, 0, /* dstX/Y/Zoffset */
380 surface
->pitch
* cpp
, /* dstRowStride, bytes */
381 &dstImageOffsets
, /* dstImageOffsets */
382 width
, height
, 1, /* size */
383 format
, type
, /* src format/type */
384 pixels
, /* data source */
388 pipe_surface_unmap(surface
);
389 pipe_surface_reference(&surface
, NULL
);
390 pipe
->texture_update(pipe
, pt
, 0, 0x1);
395 ctx
->_ImageTransferState
= imageTransferStateSave
;
398 _mesa_unmap_drapix_pbo(ctx
, unpack
);
405 * Draw quad with texcoords and optional color.
406 * Coords are window coords with y=0=bottom.
407 * \param color may be null
408 * \param invertTex if true, flip texcoords vertically
411 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
412 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
415 struct st_context
*st
= ctx
->st
;
416 struct pipe_context
*pipe
= ctx
->st
->pipe
;
417 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
419 /* setup vertex data */
421 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
422 const GLfloat fb_width
= fb
->Width
;
423 const GLfloat fb_height
= fb
->Height
;
424 const GLfloat clip_x0
= x0
/ fb_width
* 2.0 - 1.0;
425 const GLfloat clip_y0
= y0
/ fb_height
* 2.0 - 1.0;
426 const GLfloat clip_x1
= x1
/ fb_width
* 2.0 - 1.0;
427 const GLfloat clip_y1
= y1
/ fb_height
* 2.0 - 1.0;
428 GLfloat bias
= ctx
->st
->bitmap_texcoord_bias
;
429 GLfloat xBias
= 0*bias
/ (x1
-x0
);
430 GLfloat yBias
= 0*bias
/ (y1
-y0
);
431 GLfloat sLeft
= 0.0 + xBias
, sRight
= 1.0 + xBias
;
432 GLfloat tTop
= invertTex
- yBias
, tBot
= 1.0 - tTop
- yBias
;
436 verts
[0][0][0] = clip_x0
; /* v[0].attr[0].x */
437 verts
[0][0][1] = clip_y0
; /* v[0].attr[0].y */
440 verts
[1][0][0] = clip_x1
;
441 verts
[1][0][1] = clip_y0
;
444 verts
[2][0][0] = clip_x1
;
445 verts
[2][0][1] = clip_y1
;
448 verts
[3][0][0] = clip_x0
;
449 verts
[3][0][1] = clip_y1
;
452 verts
[0][tex
][0] = sLeft
; /* v[0].attr[tex].s */
453 verts
[0][tex
][1] = tTop
; /* v[0].attr[tex].t */
454 verts
[1][tex
][0] = sRight
;
455 verts
[1][tex
][1] = tTop
;
456 verts
[2][tex
][0] = sRight
;
457 verts
[2][tex
][1] = tBot
;
458 verts
[3][tex
][0] = sLeft
;
459 verts
[3][tex
][1] = tBot
;
461 /* same for all verts: */
463 for (i
= 0; i
< 4; i
++) {
464 verts
[i
][0][2] = z
; /*Z*/
465 verts
[i
][0][3] = 1.0; /*W*/
466 verts
[i
][1][0] = color
[0];
467 verts
[i
][1][1] = color
[1];
468 verts
[i
][1][2] = color
[2];
469 verts
[i
][1][3] = color
[3];
470 verts
[i
][2][2] = 0.0; /*R*/
471 verts
[i
][2][3] = 1.0; /*Q*/
475 for (i
= 0; i
< 4; i
++) {
476 verts
[i
][0][2] = z
; /*Z*/
477 verts
[i
][0][3] = 1.0; /*W*/
478 verts
[i
][1][2] = 0.0; /*R*/
479 verts
[i
][1][3] = 1.0; /*Q*/
485 struct pipe_buffer
*buf
;
488 /* allocate/load buffer object with vertex data */
489 buf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32,
490 PIPE_BUFFER_USAGE_VERTEX
,
492 map
= pipe
->winsys
->buffer_map(pipe
->winsys
, buf
,
493 PIPE_BUFFER_USAGE_CPU_WRITE
);
494 memcpy(map
, verts
, sizeof(verts
));
495 pipe
->winsys
->buffer_unmap(pipe
->winsys
, buf
);
497 util_draw_vertex_buffer(pipe
, buf
,
500 3); /* attribs/vert */
502 pipe
->winsys
->buffer_destroy(pipe
->winsys
, buf
);
509 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
510 GLsizei width
, GLsizei height
,
511 GLfloat zoomX
, GLfloat zoomY
,
512 struct pipe_texture
*pt
,
513 struct st_vertex_program
*stvp
,
514 struct st_fragment_program
*stfp
,
515 const GLfloat
*color
,
518 struct st_context
*st
= ctx
->st
;
519 struct pipe_context
*pipe
= ctx
->st
->pipe
;
520 struct cso_context
*cso
= ctx
->st
->cso_context
;
521 GLfloat x0
, y0
, x1
, y1
;
525 /* XXX if DrawPixels image is larger than max texture size, break
528 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
529 assert(width
<= maxSize
);
530 assert(height
<= maxSize
);
532 cso_save_rasterizer(cso
);
533 cso_save_viewport(cso
);
534 cso_save_samplers(cso
);
535 cso_save_sampler_textures(cso
);
536 cso_save_fragment_shader(cso
);
537 cso_save_vertex_shader(cso
);
539 /* rasterizer state: just scissor */
541 struct pipe_rasterizer_state rasterizer
;
542 memset(&rasterizer
, 0, sizeof(rasterizer
));
543 rasterizer
.gl_rasterization_rules
= 1;
544 rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
545 cso_set_rasterizer(cso
, &rasterizer
);
548 /* fragment shader state: TEX lookup program */
549 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
551 /* vertex shader state: position + texcoord pass-through */
552 cso_set_vertex_shader_handle(cso
, stvp
->driver_shader
);
555 /* texture sampling state: */
557 struct pipe_sampler_state sampler
;
558 memset(&sampler
, 0, sizeof(sampler
));
559 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
560 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
561 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
562 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
563 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
564 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
565 sampler
.normalized_coords
= 1;
567 cso_single_sampler(cso
, 0, &sampler
);
568 if (st
->pixel_xfer
.pixelmap_enabled
) {
569 cso_single_sampler(cso
, 1, &sampler
);
571 cso_single_sampler_done(cso
);
574 /* viewport state: viewport matching window dims */
576 const float width
= ctx
->DrawBuffer
->Width
;
577 const float height
= ctx
->DrawBuffer
->Height
;
578 struct pipe_viewport_state vp
;
579 vp
.scale
[0] = 0.5 * width
;
580 vp
.scale
[1] = -0.5 * height
;
583 vp
.translate
[0] = 0.5 * width
;
584 vp
.translate
[1] = 0.5 * height
;
585 vp
.translate
[2] = 0.0;
586 vp
.translate
[3] = 0.0;
587 cso_set_viewport(cso
, &vp
);
591 if (st
->pixel_xfer
.pixelmap_enabled
) {
592 struct pipe_texture
*textures
[2];
594 textures
[1] = st
->pixel_xfer
.pixelmap_texture
;
595 pipe
->set_sampler_textures(pipe
, 2, textures
);
598 pipe
->set_sampler_textures(pipe
, 1, &pt
);
601 /* Compute window coords (y=0=bottom) with pixel zoom.
602 * Recall that these coords are transformed by the current
603 * vertex shader and viewport transformation.
606 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
608 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
609 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
612 cso_restore_rasterizer(cso
);
613 cso_restore_viewport(cso
);
614 cso_restore_samplers(cso
);
615 cso_restore_sampler_textures(cso
);
616 cso_restore_fragment_shader(cso
);
617 cso_restore_vertex_shader(cso
);
622 * Check if a GL format/type combination is a match to the given pipe format.
623 * XXX probably move this to a re-usable place.
626 compatible_formats(GLenum format
, GLenum type
, enum pipe_format pipeFormat
)
628 static const GLuint one
= 1;
629 GLubyte littleEndian
= *((GLubyte
*) &one
);
631 if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
633 type
== GL_UNSIGNED_BYTE
&&
637 else if (pipeFormat
== PIPE_FORMAT_R8G8B8A8_UNORM
&&
638 format
== GL_ABGR_EXT
&&
639 type
== GL_UNSIGNED_BYTE
&&
643 else if (pipeFormat
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
645 type
== GL_UNSIGNED_BYTE
&&
649 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
651 type
== GL_UNSIGNED_SHORT_5_6_5
) {
652 /* endian don't care */
655 else if (pipeFormat
== PIPE_FORMAT_R5G6B5_UNORM
&&
657 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
658 /* endian don't care */
661 else if (pipeFormat
== PIPE_FORMAT_S8_UNORM
&&
662 format
== GL_STENCIL_INDEX
&&
663 type
== GL_UNSIGNED_BYTE
) {
666 else if (pipeFormat
== PIPE_FORMAT_Z32_UNORM
&&
667 format
== GL_DEPTH_COMPONENT
&&
668 type
== GL_UNSIGNED_INT
) {
671 /* XXX add more cases */
679 * Check if any per-fragment ops are enabled.
680 * XXX probably move this to a re-usable place.
683 any_fragment_ops(const struct st_context
*st
)
685 if (st
->state
.depth_stencil
.alpha
.enabled
||
686 st
->state
.depth_stencil
.depth
.enabled
||
687 st
->state
.blend
.blend_enable
||
688 st
->state
.blend
.logicop_enable
)
689 /* XXX more checks */
697 * Check if any pixel transfer ops are enabled.
698 * XXX probably move this to a re-usable place.
701 any_pixel_transfer_ops(const struct st_context
*st
)
703 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
704 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
705 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
706 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
707 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
708 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
709 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
710 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
711 st
->ctx
->Pixel
.MapColorFlag
)
712 /* XXX more checks */
720 * Draw image with a blit, or other non-textured quad method.
723 draw_blit(struct st_context
*st
,
724 GLsizei width
, GLsizei height
,
725 GLenum format
, GLenum type
, const GLvoid
*pixels
)
733 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
734 GLsizei width
, GLsizei height
, GLenum type
,
735 const struct gl_pixelstore_attrib
*unpack
,
736 const GLvoid
*pixels
)
738 struct st_context
*st
= ctx
->st
;
739 struct pipe_context
*pipe
= st
->pipe
;
740 struct pipe_surface
*ps
= st
->state
.framebuffer
.zsbuf
;
741 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
745 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
747 /* map the stencil buffer */
748 stmap
= pipe_surface_map(ps
);
750 /* if width > MAX_WIDTH, have to process image in chunks */
752 while (skipPixels
< width
) {
753 const GLint spanX
= x
+ skipPixels
;
754 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
756 for (row
= 0; row
< height
; row
++) {
757 GLint spanY
= y
+ row
;
758 GLubyte values
[MAX_WIDTH
];
759 GLenum destType
= GL_UNSIGNED_BYTE
;
760 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
762 GL_COLOR_INDEX
, type
,
764 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
765 type
, source
, unpack
,
766 ctx
->_ImageTransferState
);
769 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
770 spanX, spanY, values);
774 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
775 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
778 switch (ps
->format
) {
779 case PIPE_FORMAT_U_S8
:
781 ubyte
*dest
= stmap
+ spanY
* ps
->pitch
+ spanX
;
782 memcpy(dest
, values
, spanWidth
);
785 case PIPE_FORMAT_S8Z24_UNORM
:
787 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->pitch
+ spanX
;
789 for (k
= 0; k
< spanWidth
; k
++) {
791 p
= (p
& 0xffffff) | (values
[k
] << 24);
801 skipPixels
+= spanWidth
;
804 /* unmap the stencil buffer */
805 pipe_surface_unmap(ps
);
810 * Called via ctx->Driver.DrawPixels()
813 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
814 GLenum format
, GLenum type
,
815 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
817 struct st_fragment_program
*stfp
;
818 struct st_vertex_program
*stvp
;
819 struct st_context
*st
= ctx
->st
;
820 struct pipe_surface
*ps
;
822 const GLfloat
*color
;
824 if (format
== GL_STENCIL_INDEX
) {
825 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
829 st_validate_state(st
);
831 if (format
== GL_DEPTH_COMPONENT
) {
832 ps
= st
->state
.framebuffer
.zsbuf
;
833 stfp
= make_fragment_shader_z(ctx
->st
);
834 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
835 color
= ctx
->Current
.RasterColor
;
837 else if (format
== GL_STENCIL_INDEX
) {
838 ps
= st
->state
.framebuffer
.zsbuf
;
839 /* XXX special case - can't use texture map */
843 ps
= st
->state
.framebuffer
.cbufs
[0];
844 stfp
= combined_drawpix_fragment_program(ctx
);
845 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
849 bufferFormat
= ps
->format
;
851 if (1/*any_fragment_ops(st) ||
852 any_pixel_transfer_ops(st) ||
853 !compatible_formats(format, type, ps->format)*/) {
855 struct pipe_texture
*pt
856 = make_texture(ctx
->st
, width
, height
, format
, type
, unpack
, pixels
);
858 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
859 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
860 pt
, stvp
, stfp
, color
, GL_FALSE
);
861 pipe_texture_reference(&pt
, NULL
);
866 draw_blit(st
, width
, height
, format
, type
, pixels
);
873 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
874 GLsizei width
, GLsizei height
,
875 GLint dstx
, GLint dsty
)
877 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
878 struct pipe_surface
*psDraw
= rbDraw
->surface
;
883 buffer
= malloc(width
* height
* sizeof(ubyte
));
885 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
889 /* this will do stencil pixel transfer ops */
890 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
891 &ctx
->DefaultPacking
, buffer
);
893 /* map the stencil buffer */
894 drawMap
= pipe_surface_map(psDraw
);
897 /* XXX PixelZoom not handled yet */
898 for (i
= 0; i
< height
; i
++) {
905 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
906 y
= ctx
->DrawBuffer
->Height
- y
- 1;
909 dst
= drawMap
+ (y
* psDraw
->pitch
+ dstx
) * psDraw
->cpp
;
910 src
= buffer
+ i
* width
;
912 switch (psDraw
->format
) {
913 case PIPE_FORMAT_S8Z24_UNORM
:
915 uint
*dst4
= (uint
*) dst
;
917 for (j
= 0; j
< width
; j
++) {
918 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
923 case PIPE_FORMAT_U_S8
:
924 memcpy(dst
, src
, width
);
933 /* unmap the stencil buffer */
934 pipe_surface_unmap(psDraw
);
939 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
940 GLsizei width
, GLsizei height
,
941 GLint dstx
, GLint dsty
, GLenum type
)
943 struct st_context
*st
= ctx
->st
;
944 struct pipe_context
*pipe
= st
->pipe
;
945 struct pipe_screen
*screen
= pipe
->screen
;
946 struct st_renderbuffer
*rbRead
;
947 struct st_vertex_program
*stvp
;
948 struct st_fragment_program
*stfp
;
949 struct pipe_surface
*psRead
;
950 struct pipe_surface
*psTex
;
951 struct pipe_texture
*pt
;
953 enum pipe_format srcFormat
, texFormat
;
955 /* make sure rendering has completed */
956 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
958 st_validate_state(st
);
960 if (type
== GL_STENCIL
) {
961 /* can't use texturing to do stencil */
962 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
966 if (type
== GL_COLOR
) {
967 rbRead
= st_get_color_read_renderbuffer(ctx
);
969 stfp
= combined_drawpix_fragment_program(ctx
);
970 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_FALSE
);
973 assert(type
== GL_DEPTH
);
974 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
975 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
976 stfp
= make_fragment_shader_z(ctx
->st
);
977 stvp
= st_make_passthrough_vertex_shader(ctx
->st
, GL_TRUE
);
980 psRead
= rbRead
->surface
;
981 srcFormat
= psRead
->format
;
983 if (screen
->is_format_supported(screen
, srcFormat
, PIPE_TEXTURE
)) {
984 texFormat
= srcFormat
;
987 /* srcFormat can't be used as a texture format */
988 if (type
== GL_DEPTH
) {
989 static const enum pipe_format zFormats
[] = {
990 PIPE_FORMAT_Z16_UNORM
,
991 PIPE_FORMAT_Z32_UNORM
,
992 PIPE_FORMAT_S8Z24_UNORM
,
993 PIPE_FORMAT_Z24S8_UNORM
997 for (i
= 0; i
< Elements(zFormats
); i
++) {
998 if (screen
->is_format_supported(screen
, zFormats
[i
],
1000 texFormat
= zFormats
[i
];
1004 assert(texFormat
); /* XXX no depth texture formats??? */
1012 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, texFormat
, 0,
1013 width
, height
, 1, 0);
1017 psTex
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0);
1019 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1020 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1023 if (srcFormat
== texFormat
) {
1024 /* copy source framebuffer surface into mipmap/texture */
1025 pipe
->surface_copy(pipe
,
1030 srcx
, srcy
, width
, height
);
1032 else if (type
== GL_COLOR
) {
1033 /* alternate path using get/put_tile() */
1034 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1036 pipe_get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1037 pipe_put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1043 GLuint
*buf
= (GLuint
*) malloc(width
* height
* sizeof(GLuint
));
1044 pipe_get_tile_z(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1045 pipe_put_tile_z(pipe
, psTex
, 0, 0, width
, height
, buf
);
1049 /* draw textured quad */
1050 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1051 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1052 pt
, stvp
, stfp
, color
, GL_TRUE
);
1054 pipe_surface_reference(&psTex
, NULL
);
1055 pipe_texture_reference(&pt
, NULL
);
1060 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1062 functions
->DrawPixels
= st_DrawPixels
;
1063 functions
->CopyPixels
= st_CopyPixels
;