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/macros.h"
36 #include "shader/program.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/prog_print.h"
40 #include "st_context.h"
42 #include "st_atom_constbuf.h"
45 #include "st_program.h"
46 #include "st_cb_drawpixels.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_texture.h"
51 #include "st_format.h"
52 #include "st_mesa_to_tgsi.h"
53 #include "pipe/p_context.h"
54 #include "pipe/p_defines.h"
55 #include "pipe/p_inlines.h"
56 #include "pipe/p_winsys.h"
57 #include "shader/prog_instruction.h"
61 * Check if the given program is:
62 * 0: MOVE result.color, fragment.color;
66 is_passthrough_program(const struct gl_fragment_program
*prog
)
68 if (prog
->Base
.NumInstructions
== 2) {
69 const struct prog_instruction
*inst
= prog
->Base
.Instructions
;
70 if (inst
[0].Opcode
== OPCODE_MOV
&&
71 inst
[1].Opcode
== OPCODE_END
&&
72 inst
[0].DstReg
.File
== PROGRAM_OUTPUT
&&
73 inst
[0].DstReg
.Index
== FRAG_RESULT_COLR
&&
74 inst
[0].DstReg
.WriteMask
== WRITEMASK_XYZW
&&
75 inst
[0].SrcReg
[0].File
== PROGRAM_INPUT
&&
76 inst
[0].SrcReg
[0].Index
== FRAG_ATTRIB_COL0
&&
77 inst
[0].SrcReg
[0].Swizzle
== SWIZZLE_XYZW
) {
86 * Make fragment program for glBitmap:
87 * Sample the texture and kill the fragment if the bit is 0.
88 * This program will be combined with the user's fragment program.
90 static struct st_fragment_program
*
91 make_bitmap_fragment_program(GLcontext
*ctx
)
93 struct st_fragment_program
*stfp
;
97 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
101 p
->NumInstructions
= 5;
103 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
104 if (!p
->Instructions
) {
105 ctx
->Driver
.DeleteProgram(ctx
, p
);
108 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
110 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
111 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
112 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
113 p
->Instructions
[ic
].DstReg
.Index
= 0;
114 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
115 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
116 p
->Instructions
[ic
].TexSrcUnit
= 0;
117 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
120 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
121 p
->Instructions
[ic
].Opcode
= OPCODE_SWZ
;
122 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
123 p
->Instructions
[ic
].DstReg
.Index
= 0;
124 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
125 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
126 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
127 p
->Instructions
[ic
].SrcReg
[0].Swizzle
128 = MAKE_SWIZZLE4(SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
, SWIZZLE_ONE
);
131 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */
132 p
->Instructions
[ic
].Opcode
= OPCODE_SUB
;
133 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
134 p
->Instructions
[ic
].DstReg
.Index
= 0;
135 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
136 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
137 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
138 p
->Instructions
[ic
].SrcReg
[1].File
= PROGRAM_TEMPORARY
;
139 p
->Instructions
[ic
].SrcReg
[1].Index
= 0;
140 p
->Instructions
[ic
].SrcReg
[1].Swizzle
= SWIZZLE_XXXX
; /* 1.0 */
143 /* KIL if tmp0 < 0 */
144 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
145 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
146 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
150 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
152 assert(ic
== p
->NumInstructions
);
154 p
->InputsRead
= FRAG_BIT_TEX0
;
155 p
->OutputsWritten
= 0x0;
157 stfp
= (struct st_fragment_program
*) p
;
158 st_translate_fragment_program(ctx
->st
, stfp
, NULL
,
159 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
166 * Combine basic bitmap fragment program with the user-defined program.
168 static struct st_fragment_program
*
169 combined_bitmap_fragment_program(GLcontext
*ctx
)
171 struct st_context
*st
= ctx
->st
;
172 struct st_fragment_program
*stfp
;
174 if (!st
->bitmap
.program
) {
175 /* create the basic bitmap fragment program */
176 st
->bitmap
.program
= make_bitmap_fragment_program(ctx
);
179 if (st
->bitmap
.user_prog_sn
== st
->fp
->serialNo
) {
181 stfp
= st
->bitmap
.combined_prog
;
184 /* Concatenate the bitmap program with the current user-defined program.
186 stfp
= (struct st_fragment_program
*)
187 _mesa_combine_programs(ctx
,
188 &st
->bitmap
.program
->Base
.Base
,
193 struct gl_program
*p
= &stfp
->Base
.Base
;
194 printf("Combined bitmap program:\n");
195 _mesa_print_program(p
);
196 printf("InputsRead: 0x%x\n", p
->InputsRead
);
197 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
198 _mesa_print_parameter_list(p
->Parameters
);
202 /* translate to TGSI tokens */
203 st_translate_fragment_program(st
, stfp
, NULL
,
204 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
206 /* save new program, update serial numbers */
207 st
->bitmap
.user_prog_sn
= st
->fp
->serialNo
;
208 st
->bitmap
.combined_prog
= stfp
;
211 /* Ideally we'd have updated the pipe constants during the normal
212 * st/atom mechanism. But we can't since this is specific to glBitmap.
214 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
222 * Make fragment shader for glDraw/CopyPixels. This shader is made
223 * by combining the pixel transfer shader with the user-defined shader.
225 static struct st_fragment_program
*
226 combined_drawpix_fragment_program(GLcontext
*ctx
)
228 struct st_context
*st
= ctx
->st
;
229 struct st_fragment_program
*stfp
;
231 if (st
->pixel_xfer
.program
->serialNo
== st
->pixel_xfer
.xfer_prog_sn
232 && st
->fp
->serialNo
== st
->pixel_xfer
.user_prog_sn
) {
233 /* the pixel tranfer program has not changed and the user-defined
234 * program has not changed, so re-use the combined program.
236 stfp
= st
->pixel_xfer
.combined_prog
;
239 /* Concatenate the pixel transfer program with the current user-
242 if (is_passthrough_program(&st
->fp
->Base
)) {
243 stfp
= (struct st_fragment_program
*)
244 _mesa_clone_program(ctx
, &st
->pixel_xfer
.program
->Base
.Base
);
247 stfp
= (struct st_fragment_program
*)
248 _mesa_combine_programs(ctx
,
249 &st
->pixel_xfer
.program
->Base
.Base
,
255 struct gl_program
*p
= &stfp
->Base
.Base
;
256 printf("Combined DrawPixels program:\n");
257 _mesa_print_program(p
);
258 printf("InputsRead: 0x%x\n", p
->InputsRead
);
259 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
260 _mesa_print_parameter_list(p
->Parameters
);
264 /* translate to TGSI tokens */
265 st_translate_fragment_program(st
, stfp
, NULL
,
266 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
268 /* save new program, update serial numbers */
269 st
->pixel_xfer
.xfer_prog_sn
= st
->pixel_xfer
.program
->serialNo
;
270 st
->pixel_xfer
.user_prog_sn
= st
->fp
->serialNo
;
271 st
->pixel_xfer
.combined_prog_sn
= stfp
->serialNo
;
272 st
->pixel_xfer
.combined_prog
= stfp
;
275 /* Ideally we'd have updated the pipe constants during the normal
276 * st/atom mechanism. But we can't since this is specific to glDrawPixels.
278 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
285 * Create fragment shader that does a TEX() instruction to get a Z
286 * value, then writes to FRAG_RESULT_DEPR.
287 * Pass fragment color through as-is.
289 static struct st_fragment_program
*
290 make_fragment_shader_z(struct st_context
*st
)
292 GLcontext
*ctx
= st
->ctx
;
293 /* only make programs once and re-use */
294 static struct st_fragment_program
*stfp
= NULL
;
295 struct gl_program
*p
;
301 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
305 p
->NumInstructions
= 3;
307 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
308 if (!p
->Instructions
) {
309 ctx
->Driver
.DeleteProgram(ctx
, p
);
312 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
314 /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
315 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
316 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
317 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_DEPR
;
318 p
->Instructions
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
319 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
320 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
321 p
->Instructions
[ic
].TexSrcUnit
= 0;
322 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
325 /* MOV result.color, fragment.color */
326 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
327 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
328 p
->Instructions
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
329 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
330 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
334 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
336 assert(ic
== p
->NumInstructions
);
338 p
->InputsRead
= FRAG_BIT_TEX0
| FRAG_BIT_COL0
;
339 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_DEPR
);
341 stfp
= (struct st_fragment_program
*) p
;
342 st_translate_fragment_program(st
, stfp
, NULL
,
343 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
351 * Create a simple vertex shader that just passes through the
352 * vertex position and texcoord (and optionally, color).
354 static struct st_vertex_program
*
355 make_vertex_shader(struct st_context
*st
, GLboolean passColor
)
357 /* only make programs once and re-use */
358 static struct st_vertex_program
*progs
[2] = { NULL
, NULL
};
359 GLcontext
*ctx
= st
->ctx
;
360 struct st_vertex_program
*stvp
;
361 struct gl_program
*p
;
364 if (progs
[passColor
])
365 return progs
[passColor
];
367 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
372 p
->NumInstructions
= 4;
374 p
->NumInstructions
= 3;
376 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
377 if (!p
->Instructions
) {
378 ctx
->Driver
.DeleteProgram(ctx
, p
);
381 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
382 /* MOV result.pos, vertex.pos; */
383 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
384 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
385 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
386 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
387 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
388 /* MOV result.texcoord0, vertex.texcoord0; */
389 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
390 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
391 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_TEX0
;
392 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
393 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_TEX0
;
396 /* MOV result.color0, vertex.color0; */
397 p
->Instructions
[ic
].Opcode
= OPCODE_MOV
;
398 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
399 p
->Instructions
[ic
].DstReg
.Index
= VERT_RESULT_COL0
;
400 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
401 p
->Instructions
[ic
].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
406 p
->Instructions
[ic
].Opcode
= OPCODE_END
;
409 assert(ic
== p
->NumInstructions
);
411 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_TEX0
;
412 p
->OutputsWritten
= ((1 << VERT_RESULT_TEX0
) |
413 (1 << VERT_RESULT_HPOS
));
415 p
->InputsRead
|= VERT_BIT_COLOR0
;
416 p
->OutputsWritten
|= (1 << VERT_RESULT_COL0
);
419 stvp
= (struct st_vertex_program
*) p
;
420 st_translate_vertex_program(st
, stvp
, NULL
,
421 stvp
->tokens
, ST_MAX_SHADER_TOKENS
);
423 progs
[passColor
] = stvp
;
430 _mesa_base_format(GLenum format
)
433 case GL_DEPTH_COMPONENT
:
434 return GL_DEPTH_COMPONENT
;
435 case GL_STENCIL_INDEX
:
436 return GL_STENCIL_INDEX
;
444 static struct pipe_mipmap_tree
*
445 alloc_mipmap_tree(struct st_context
*st
,
446 GLsizei width
, GLsizei height
, uint pipeFormat
)
448 const GLbitfield flags
= PIPE_SURFACE_FLAG_TEXTURE
;
449 struct pipe_mipmap_tree
*mt
;
452 mt
= CALLOC_STRUCT(pipe_mipmap_tree
);
456 cpp
= st_sizeof_format(pipeFormat
);
458 /* allocate texture region/storage */
459 mt
->region
= st
->pipe
->winsys
->region_alloc(st
->pipe
->winsys
,
460 cpp
, width
, height
, flags
);
462 mt
->target
= PIPE_TEXTURE_2D
;
463 mt
->internal_format
= GL_RGBA
;
464 mt
->format
= pipeFormat
;
468 mt
->height0
= height
;
472 mt
->pitch
= mt
->region
->pitch
;
474 mt
->total_height
= height
;
475 mt
->level
[0].level_offset
= 0;
476 mt
->level
[0].width
= width
;
477 mt
->level
[0].height
= height
;
478 mt
->level
[0].depth
= 1;
479 mt
->level
[0].nr_images
= 1;
480 mt
->level
[0].image_offset
= NULL
;
488 * Make mipmap tree containing an image for glDrawPixels image.
489 * If 'pixels' is NULL, leave the texture image data undefined.
491 static struct pipe_mipmap_tree
*
492 make_mipmap_tree(struct st_context
*st
,
493 GLsizei width
, GLsizei height
, GLenum format
, GLenum type
,
494 const struct gl_pixelstore_attrib
*unpack
,
495 const GLvoid
*pixels
)
497 GLcontext
*ctx
= st
->ctx
;
498 struct pipe_context
*pipe
= st
->pipe
;
499 const struct gl_texture_format
*mformat
;
500 struct pipe_mipmap_tree
*mt
;
501 GLuint pipeFormat
, cpp
;
504 baseFormat
= _mesa_base_format(format
);
506 mformat
= st_ChooseTextureFormat(ctx
, baseFormat
, format
, type
);
509 pipeFormat
= st_mesa_format_to_pipe_format(mformat
->MesaFormat
);
511 cpp
= st_sizeof_format(pipeFormat
);
513 mt
= alloc_mipmap_tree(st
, width
, height
, pipeFormat
);
517 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
519 mt->region = buffer_object_region(unpack->BufferObj);
521 printf("st_DrawPixels (sourcing from PBO not implemented yet)\n");
525 static const GLuint dstImageOffsets
= 0;
527 GLuint pitch
= mt
->region
->pitch
;
529 const GLbitfield imageTransferStateSave
= ctx
->_ImageTransferState
;
531 /* we'll do pixel transfer in a fragment shader */
532 ctx
->_ImageTransferState
= 0x0;
534 /* map texture region */
535 dest
= pipe
->region_map(pipe
, mt
->region
);
537 /* Put image into texture region.
538 * Note that the image is actually going to be upside down in
539 * the texture. We deal with that with texcoords.
541 success
= mformat
->StoreImage(ctx
, 2, /* dims */
542 baseFormat
, /* baseInternalFormat */
543 mformat
, /* gl_texture_format */
545 0, 0, 0, /* dstX/Y/Zoffset */
546 pitch
* cpp
, /* dstRowStride, bytes */
547 &dstImageOffsets
, /* dstImageOffsets */
548 width
, height
, 1, /* size */
549 format
, type
, /* src format/type */
550 pixels
, /* data source */
554 pipe
->region_unmap(pipe
, mt
->region
);
558 ctx
->_ImageTransferState
= imageTransferStateSave
;
562 mt
->target
= PIPE_TEXTURE_2D
;
563 mt
->internal_format
= GL_RGBA
;
564 mt
->format
= pipeFormat
;
568 mt
->height0
= height
;
572 mt
->pitch
= mt
->region
->pitch
;
574 mt
->total_height
= height
;
575 mt
->level
[0].level_offset
= 0;
576 mt
->level
[0].width
= width
;
577 mt
->level
[0].height
= height
;
578 mt
->level
[0].depth
= 1;
579 mt
->level
[0].nr_images
= 1;
580 mt
->level
[0].image_offset
= NULL
;
588 free_mipmap_tree(struct pipe_context
*pipe
, struct pipe_mipmap_tree
*mt
)
590 pipe
->winsys
->region_release(pipe
->winsys
, &mt
->region
);
596 * Draw textured quad.
597 * Coords are window coords with y=0=bottom.
600 draw_quad(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
601 GLfloat x1
, GLfloat y1
, GLboolean invertTex
)
603 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
605 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
608 verts
[0][0][0] = x0
; /* attr[0].x */
609 verts
[0][0][1] = y0
; /* attr[0].x */
610 verts
[0][1][0] = 0.0; /* attr[1].s */
611 verts
[0][1][1] = tTop
; /* attr[1].t */
616 verts
[1][1][0] = 1.0;
617 verts
[1][1][1] = tTop
;
622 verts
[2][1][0] = 1.0;
623 verts
[2][1][1] = tBot
;
628 verts
[3][1][0] = 0.0;
629 verts
[3][1][1] = tBot
;
631 /* same for all verts: */
632 for (i
= 0; i
< 4; i
++) {
633 verts
[i
][0][2] = z
; /*Z*/
634 verts
[i
][0][3] = 1.0; /*W*/
635 verts
[i
][1][2] = 0.0; /*R*/
636 verts
[i
][1][3] = 1.0; /*Q*/
639 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2);
644 draw_quad_colored(GLcontext
*ctx
, GLfloat x0
, GLfloat y0
, GLfloat z
,
645 GLfloat x1
, GLfloat y1
, const GLfloat
*color
,
648 GLfloat verts
[4][3][4]; /* four verts, three attribs, XYZW */
650 GLfloat tTop
= invertTex
, tBot
= 1.0 - tTop
;
653 verts
[0][0][0] = x0
; /* attr[0].x */
654 verts
[0][0][1] = y0
; /* attr[0].y */
655 verts
[0][2][0] = 0.0; /* attr[2].s */
656 verts
[0][2][1] = tTop
; /* attr[2].t */
661 verts
[1][2][0] = 1.0;
662 verts
[1][2][1] = tTop
;
667 verts
[2][2][0] = 1.0;
668 verts
[2][2][1] = tBot
;
673 verts
[3][2][0] = 0.0;
674 verts
[3][2][1] = tBot
;
676 /* same for all verts: */
677 for (i
= 0; i
< 4; i
++) {
678 verts
[i
][0][2] = z
; /*Z*/
679 verts
[i
][0][3] = 1.0; /*W*/
680 verts
[i
][1][0] = color
[0];
681 verts
[i
][1][1] = color
[1];
682 verts
[i
][1][2] = color
[2];
683 verts
[i
][1][3] = color
[3];
684 verts
[i
][2][2] = 0.0; /*R*/
685 verts
[i
][2][3] = 1.0; /*Q*/
688 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 3);
694 draw_textured_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
695 GLsizei width
, GLsizei height
,
696 GLfloat zoomX
, GLfloat zoomY
,
697 struct pipe_mipmap_tree
*mt
,
698 struct st_vertex_program
*stvp
,
699 struct st_fragment_program
*stfp
,
700 const GLfloat
*color
,
703 const GLuint unit
= 0;
704 struct pipe_context
*pipe
= ctx
->st
->pipe
;
705 GLfloat x0
, y0
, x1
, y1
;
709 /* XXX if DrawPixels image is larger than max texture size, break
712 maxSize
= 1 << (pipe
->get_param(pipe
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
713 assert(width
<= maxSize
);
714 assert(height
<= maxSize
);
716 /* setup state: just scissor */
718 struct pipe_rasterizer_state setup
;
719 const struct cso_rasterizer
*cso
;
720 memset(&setup
, 0, sizeof(setup
));
721 if (ctx
->Scissor
.Enabled
)
723 cso
= st_cached_rasterizer_state(ctx
->st
, &setup
);
724 pipe
->bind_rasterizer_state(pipe
, cso
->data
);
727 /* fragment shader state: TEX lookup program */
728 pipe
->bind_fs_state(pipe
, stfp
->fs
->data
);
730 /* vertex shader state: position + texcoord pass-through */
731 pipe
->bind_vs_state(pipe
, stvp
->vs
->data
);
733 /* texture sampling state: */
735 struct pipe_sampler_state sampler
;
736 const struct cso_sampler
*cso
;
737 memset(&sampler
, 0, sizeof(sampler
));
738 sampler
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
739 sampler
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
740 sampler
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
741 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
742 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
743 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
744 sampler
.normalized_coords
= 1;
745 cso
= st_cached_sampler_state(ctx
->st
, &sampler
);
746 pipe
->bind_sampler_state(pipe
, unit
, cso
->data
);
749 /* viewport state: viewport matching window dims */
751 const float width
= ctx
->DrawBuffer
->Width
;
752 const float height
= ctx
->DrawBuffer
->Height
;
753 struct pipe_viewport_state vp
;
754 vp
.scale
[0] = 0.5 * width
;
755 vp
.scale
[1] = -0.5 * height
;
758 vp
.translate
[0] = 0.5 * width
;
759 vp
.translate
[1] = 0.5 * height
;
760 vp
.translate
[2] = 0.0;
761 vp
.translate
[3] = 0.0;
762 pipe
->set_viewport_state(pipe
, &vp
);
765 /* mipmap tree state: */
767 pipe
->set_texture_state(pipe
, unit
, mt
);
770 /* Compute window coords (y=0=bottom) with pixel zoom.
771 * Recall that these coords are transformed by the current
772 * vertex shader and viewport transformation.
775 x1
= x
+ width
* ctx
->Pixel
.ZoomX
;
777 y1
= y
+ height
* ctx
->Pixel
.ZoomY
;
779 /* draw textured quad */
781 draw_quad_colored(ctx
, x0
, y0
, z
, x1
, y1
, color
, invertTex
);
783 draw_quad(ctx
, x0
, y0
, z
, x1
, y1
, invertTex
);
785 /* restore GL state */
786 pipe
->bind_rasterizer_state(pipe
, ctx
->st
->state
.rasterizer
->data
);
787 pipe
->bind_fs_state(pipe
, ctx
->st
->state
.fs
->data
);
788 pipe
->bind_vs_state(pipe
, ctx
->st
->state
.vs
->data
);
789 pipe
->set_texture_state(pipe
, unit
, ctx
->st
->state
.texture
[unit
]);
790 pipe
->bind_sampler_state(pipe
, unit
, ctx
->st
->state
.sampler
[unit
]->data
);
791 pipe
->set_viewport_state(pipe
, &ctx
->st
->state
.viewport
);
796 * Check if a GL format/type combination is a match to the given pipe format.
797 * XXX probably move this to a re-usable place.
800 compatible_formats(GLenum format
, GLenum type
, GLuint pipeFormat
)
802 static const GLuint one
= 1;
803 GLubyte littleEndian
= *((GLubyte
*) &one
);
805 if (pipeFormat
== PIPE_FORMAT_U_R8_G8_B8_A8
&&
807 type
== GL_UNSIGNED_BYTE
&&
811 else if (pipeFormat
== PIPE_FORMAT_U_R8_G8_B8_A8
&&
812 format
== GL_ABGR_EXT
&&
813 type
== GL_UNSIGNED_BYTE
&&
817 else if (pipeFormat
== PIPE_FORMAT_U_A8_R8_G8_B8
&&
819 type
== GL_UNSIGNED_BYTE
&&
823 else if (pipeFormat
== PIPE_FORMAT_U_R5_G6_B5
&&
825 type
== GL_UNSIGNED_SHORT_5_6_5
) {
826 /* endian don't care */
829 else if (pipeFormat
== PIPE_FORMAT_U_R5_G6_B5
&&
831 type
== GL_UNSIGNED_SHORT_5_6_5_REV
) {
832 /* endian don't care */
835 else if (pipeFormat
== PIPE_FORMAT_U_S8
&&
836 format
== GL_STENCIL_INDEX
&&
837 type
== GL_UNSIGNED_BYTE
) {
840 else if (pipeFormat
== PIPE_FORMAT_U_Z32
&&
841 format
== GL_DEPTH_COMPONENT
&&
842 type
== GL_UNSIGNED_INT
) {
845 /* XXX add more cases */
853 * Check if any per-fragment ops are enabled.
854 * XXX probably move this to a re-usable place.
857 any_fragment_ops(const struct st_context
*st
)
859 if (st
->state
.alpha_test
->state
.enabled
||
860 st
->state
.blend
->state
.blend_enable
||
861 st
->state
.blend
->state
.logicop_enable
||
862 st
->state
.depth_stencil
->state
.depth
.enabled
)
863 /* XXX more checks */
871 * Check if any pixel transfer ops are enabled.
872 * XXX probably move this to a re-usable place.
875 any_pixel_transfer_ops(const struct st_context
*st
)
877 if (st
->ctx
->Pixel
.RedScale
!= 1.0 ||
878 st
->ctx
->Pixel
.RedBias
!= 0.0 ||
879 st
->ctx
->Pixel
.GreenScale
!= 1.0 ||
880 st
->ctx
->Pixel
.GreenBias
!= 0.0 ||
881 st
->ctx
->Pixel
.BlueScale
!= 1.0 ||
882 st
->ctx
->Pixel
.BlueBias
!= 0.0 ||
883 st
->ctx
->Pixel
.AlphaScale
!= 1.0 ||
884 st
->ctx
->Pixel
.AlphaBias
!= 0.0 ||
885 st
->ctx
->Pixel
.MapColorFlag
)
886 /* XXX more checks */
894 * Draw image with a blit, or other non-textured quad method.
897 draw_blit(struct st_context
*st
,
898 GLsizei width
, GLsizei height
,
899 GLenum format
, GLenum type
, const GLvoid
*pixels
)
907 draw_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
908 GLsizei width
, GLsizei height
, GLenum type
,
909 const struct gl_pixelstore_attrib
*unpack
,
910 const GLvoid
*pixels
)
912 struct st_context
*st
= ctx
->st
;
913 struct pipe_context
*pipe
= st
->pipe
;
914 struct pipe_surface
*ps
= st
->state
.framebuffer
.sbuf
;
915 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
919 pipe
->flush(pipe
, 0);
921 /* map the stencil buffer */
922 stmap
= pipe
->region_map(pipe
, ps
->region
);
924 /* if width > MAX_WIDTH, have to process image in chunks */
926 while (skipPixels
< width
) {
927 const GLint spanX
= x
+ skipPixels
;
928 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
930 for (row
= 0; row
< height
; row
++) {
931 GLint spanY
= y
+ row
;
932 GLubyte values
[MAX_WIDTH
];
933 GLenum destType
= GL_UNSIGNED_BYTE
;
934 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
936 GL_COLOR_INDEX
, type
,
938 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
939 type
, source
, unpack
,
940 ctx
->_ImageTransferState
);
943 _swrast_write_zoomed_stencil_span(ctx, x, y, spanWidth,
944 spanX, spanY, values);
948 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
949 spanY
= ctx
->DrawBuffer
->Height
- spanY
- 1;
952 switch (ps
->format
) {
953 case PIPE_FORMAT_U_S8
:
955 ubyte
*dest
= stmap
+ spanY
* ps
->region
->pitch
+ spanX
;
956 memcpy(dest
, values
, spanWidth
);
959 case PIPE_FORMAT_S8_Z24
:
961 uint
*dest
= (uint
*) stmap
+ spanY
* ps
->region
->pitch
+ spanX
;
963 for (k
= 0; k
< spanWidth
; k
++) {
965 p
= (p
& 0xffffff) | (values
[k
] << 24);
975 skipPixels
+= spanWidth
;
978 /* unmap the stencil buffer */
979 pipe
->region_unmap(pipe
, ps
->region
);
984 * Called via ctx->Driver.DrawPixels()
987 st_DrawPixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
988 GLenum format
, GLenum type
,
989 const struct gl_pixelstore_attrib
*unpack
, const GLvoid
*pixels
)
991 struct st_fragment_program
*stfp
;
992 struct st_vertex_program
*stvp
;
993 struct st_context
*st
= ctx
->st
;
994 struct pipe_surface
*ps
;
996 const GLfloat
*color
;
998 if (format
== GL_STENCIL_INDEX
) {
999 draw_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
1003 st_validate_state(st
);
1005 if (format
== GL_DEPTH_COMPONENT
) {
1006 ps
= st
->state
.framebuffer
.zbuf
;
1007 stfp
= make_fragment_shader_z(ctx
->st
);
1008 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1009 color
= ctx
->Current
.RasterColor
;
1011 else if (format
== GL_STENCIL_INDEX
) {
1012 ps
= st
->state
.framebuffer
.sbuf
;
1013 /* XXX special case - can't use texture map */
1017 ps
= st
->state
.framebuffer
.cbufs
[0];
1018 stfp
= combined_drawpix_fragment_program(ctx
);
1019 stvp
= make_vertex_shader(ctx
->st
, GL_FALSE
);
1023 bufferFormat
= ps
->format
;
1025 if (any_fragment_ops(st
) ||
1026 any_pixel_transfer_ops(st
) ||
1027 !compatible_formats(format
, type
, ps
->format
)) {
1029 struct pipe_mipmap_tree
*mt
1030 = make_mipmap_tree(ctx
->st
, width
, height
, format
, type
,
1033 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1034 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1035 mt
, stvp
, stfp
, color
, GL_FALSE
);
1036 free_mipmap_tree(st
->pipe
, mt
);
1041 draw_blit(st
, width
, height
, format
, type
, pixels
);
1048 * Create a texture which represents a bitmap image.
1050 static struct pipe_mipmap_tree
*
1051 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
1052 const struct gl_pixelstore_attrib
*unpack
,
1053 const GLubyte
*bitmap
)
1055 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1056 const uint flags
= PIPE_SURFACE_FLAG_TEXTURE
;
1057 uint format
= 0, cpp
, comp
, pitch
;
1059 struct pipe_mipmap_tree
*mt
;
1062 /* find a texture format we know */
1063 if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_I8
)) {
1064 format
= PIPE_FORMAT_U_I8
;
1068 else if (pipe
->is_format_supported( pipe
, PIPE_FORMAT_U_A8_R8_G8_B8
)) {
1069 format
= PIPE_FORMAT_U_A8_R8_G8_B8
;
1071 comp
= 3; /* alpha channel */ /*XXX little-endian dependency */
1074 /* XXX support more formats */
1079 * Create a mipmap tree.
1081 mt
= CALLOC_STRUCT(pipe_mipmap_tree
);
1085 if (unpack
->BufferObj
&& unpack
->BufferObj
->Name
) {
1087 mt->region = buffer_object_region(unpack->BufferObj);
1089 printf("st_Bitmap (sourcing from PBO not implemented yet)\n");
1093 /* allocate texture region/storage */
1094 mt
->region
= pipe
->winsys
->region_alloc(pipe
->winsys
,
1095 cpp
, width
, height
, flags
);
1096 pitch
= mt
->region
->pitch
;
1098 /* map texture region */
1099 dest
= pipe
->region_map(pipe
, mt
->region
);
1101 printf("st_Bitmap region_map() failed!?!");
1105 /* Put image into texture region.
1106 * Note that the image is actually going to be upside down in
1107 * the texture. We deal with that with texcoords.
1110 for (row
= 0; row
< height
; row
++) {
1111 const GLubyte
*src
= (const GLubyte
*) _mesa_image_address2d(unpack
,
1112 bitmap
, width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
1113 ubyte
*destRow
= dest
+ row
* pitch
* cpp
;
1115 if (unpack
->LsbFirst
) {
1117 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1118 for (col
= 0; col
< width
; col
++) {
1120 /* set texel to 255 if bit is set */
1121 destRow
[comp
] = (*src
& mask
) ? 255 : 0;
1133 /* get ready for next row */
1139 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1140 for (col
= 0; col
< width
; col
++) {
1142 /* set texel to 255 if bit is set */
1143 destRow
[comp
] =(*src
& mask
) ? 255 : 0;
1155 /* get ready for next row */
1163 pipe
->region_unmap(pipe
, mt
->region
);
1165 mt
->target
= PIPE_TEXTURE_2D
;
1166 mt
->internal_format
= GL_RGBA
;
1167 mt
->format
= format
;
1168 mt
->first_level
= 0;
1171 mt
->height0
= height
;
1175 mt
->pitch
= mt
->region
->pitch
;
1176 mt
->depth_pitch
= 0;
1177 mt
->total_height
= height
;
1178 mt
->level
[0].level_offset
= 0;
1179 mt
->level
[0].width
= width
;
1180 mt
->level
[0].height
= height
;
1181 mt
->level
[0].depth
= 1;
1182 mt
->level
[0].nr_images
= 1;
1183 mt
->level
[0].image_offset
= NULL
;
1192 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1193 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
1195 struct st_fragment_program
*stfp
;
1196 struct st_vertex_program
*stvp
;
1197 struct st_context
*st
= ctx
->st
;
1198 struct pipe_mipmap_tree
*mt
;
1200 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1201 stfp
= combined_bitmap_fragment_program(ctx
);
1203 st_validate_state(st
);
1205 mt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
1207 draw_textured_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
1208 width
, height
, 1.0, 1.0,
1210 ctx
->Current
.RasterColor
, GL_FALSE
);
1212 free_mipmap_tree(st
->pipe
, mt
);
1218 copy_stencil_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1219 GLsizei width
, GLsizei height
,
1220 GLint dstx
, GLint dsty
)
1222 struct st_context
*st
= ctx
->st
;
1223 struct pipe_context
*pipe
= st
->pipe
;
1224 struct st_renderbuffer
*rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_StencilBuffer
);
1225 struct st_renderbuffer
*rbDraw
= st_renderbuffer(ctx
->DrawBuffer
->_StencilBuffer
);
1226 struct pipe_surface
*psRead
= rbRead
->surface
;
1227 struct pipe_surface
*psDraw
= rbDraw
->surface
;
1228 ubyte
*readMap
, *drawMap
;
1232 buffer
= malloc(width
* height
* sizeof(ubyte
));
1234 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels(stencil)");
1238 /* map the stencil buffers */
1239 readMap
= pipe
->region_map(pipe
, psRead
->region
);
1240 drawMap
= pipe
->region_map(pipe
, psDraw
->region
);
1242 /* this will do stencil pixel transfer ops */
1243 st_read_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, GL_UNSIGNED_BYTE
,
1244 &ctx
->DefaultPacking
, buffer
);
1247 /* XXX PixelZoom not handled yet */
1248 for (i
= 0; i
< height
; i
++) {
1255 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1256 y
= ctx
->DrawBuffer
->Height
- y
- 1;
1259 dst
= drawMap
+ (y
* psDraw
->region
->pitch
+ dstx
) * psDraw
->region
->cpp
;
1260 src
= buffer
+ i
* width
;
1262 switch (psDraw
->format
) {
1263 case PIPE_FORMAT_S8_Z24
:
1265 uint
*dst4
= (uint
*) dst
;
1267 for (j
= 0; j
< width
; j
++) {
1268 *dst4
= (*dst4
& 0xffffff) | (src
[j
] << 24);
1273 case PIPE_FORMAT_U_S8
:
1274 memcpy(dst
, src
, width
);
1283 /* unmap the stencil buffers */
1284 pipe
->region_unmap(pipe
, psRead
->region
);
1285 pipe
->region_unmap(pipe
, psDraw
->region
);
1290 st_CopyPixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
1291 GLsizei width
, GLsizei height
,
1292 GLint dstx
, GLint dsty
, GLenum type
)
1294 struct st_context
*st
= ctx
->st
;
1295 struct pipe_context
*pipe
= st
->pipe
;
1296 struct st_renderbuffer
*rbRead
;
1297 struct st_vertex_program
*stvp
;
1298 struct st_fragment_program
*stfp
;
1299 struct pipe_surface
*psRead
;
1300 struct pipe_mipmap_tree
*mt
;
1304 /* make sure rendering has completed */
1305 pipe
->flush(pipe
, 0x0);
1307 st_validate_state(st
);
1309 if (type
== GL_STENCIL
) {
1310 /* can't use texturing to do stencil */
1311 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, dstx
, dsty
);
1315 if (type
== GL_COLOR
) {
1316 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
1318 stfp
= combined_drawpix_fragment_program(ctx
);
1319 stvp
= make_vertex_shader(ctx
->st
, GL_FALSE
);
1322 rbRead
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
1323 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
1324 stfp
= make_fragment_shader_z(ctx
->st
);
1325 stvp
= make_vertex_shader(ctx
->st
, GL_TRUE
);
1328 psRead
= rbRead
->surface
;
1329 format
= psRead
->format
;
1331 mt
= alloc_mipmap_tree(ctx
->st
, width
, height
, format
);
1335 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
1336 srcy
= ctx
->DrawBuffer
->Height
- srcy
- height
;
1339 /* For some drivers (like Xlib) it's not possible to treat the
1340 * front/back color buffers as regions (they're XImages and Pixmaps).
1341 * So, this var tells us if we can use region_copy here...
1343 if (st
->haveFramebufferRegions
) {
1344 /* copy source framebuffer region into mipmap/texture */
1345 pipe
->region_copy(pipe
,
1346 mt
->region
, /* dest */
1347 0, /* dest_offset */
1351 srcx
, srcy
, width
, height
);
1354 /* alternate path using get/put_tile() */
1355 struct pipe_surface
*psTex
;
1356 GLfloat
*buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1358 psTex
= pipe
->get_tex_surface(pipe
, mt
, 0, 0, 0);
1360 (void) pipe
->region_map(pipe
, psRead
->region
);
1361 (void) pipe
->region_map(pipe
, psTex
->region
);
1363 pipe
->get_tile_rgba(pipe
, psRead
, srcx
, srcy
, width
, height
, buf
);
1364 pipe
->put_tile_rgba(pipe
, psTex
, 0, 0, width
, height
, buf
);
1366 pipe
->region_unmap(pipe
, psRead
->region
);
1367 pipe
->region_unmap(pipe
, psTex
->region
);
1369 pipe_surface_reference(&psTex
, NULL
);
1375 /* draw textured quad */
1376 draw_textured_quad(ctx
, dstx
, dsty
, ctx
->Current
.RasterPos
[2],
1377 width
, height
, ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
,
1378 mt
, stvp
, stfp
, color
, GL_TRUE
);
1380 free_mipmap_tree(st
->pipe
, mt
);
1385 void st_init_drawpixels_functions(struct dd_function_table
*functions
)
1387 functions
->DrawPixels
= st_DrawPixels
;
1388 functions
->CopyPixels
= st_CopyPixels
;
1389 functions
->Bitmap
= st_Bitmap
;