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/mfeatures.h"
39 #include "program/program.h"
40 #include "program/prog_print.h"
42 #include "st_context.h"
44 #include "st_atom_constbuf.h"
45 #include "st_program.h"
46 #include "st_cb_bitmap.h"
47 #include "st_texture.h"
49 #include "pipe/p_context.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_shader_tokens.h"
52 #include "util/u_inlines.h"
53 #include "util/u_draw_quad.h"
54 #include "util/u_simple_shaders.h"
55 #include "util/u_upload_mgr.h"
56 #include "program/prog_instruction.h"
57 #include "cso_cache/cso_context.h"
63 * glBitmaps are drawn as textured quads. The user's bitmap pattern
64 * is stored in a texture image. An alpha8 texture format is used.
65 * The fragment shader samples a bit (texel) from the texture, then
66 * discards the fragment if the bit is off.
68 * Note that we actually store the inverse image of the bitmap to
69 * simplify the fragment program. An "on" bit gets stored as texel=0x0
70 * and an "off" bit is stored as texel=0xff. Then we kill the
71 * fragment if the negated texel value is less than zero.
76 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
77 * buffer which is then rendered en mass upon a flush, state change, etc.
78 * A wide, short buffer is used to target the common case of a series
79 * of glBitmap calls being used to draw text.
81 static GLboolean UseBitmapCache
= GL_TRUE
;
84 #define BITMAP_CACHE_WIDTH 512
85 #define BITMAP_CACHE_HEIGHT 32
89 /** Window pos to render the cached image */
91 /** Bounds of region used in window coords */
92 GLint xmin
, ymin
, xmax
, ymax
;
96 /** Bitmap's Z position */
99 struct pipe_resource
*texture
;
100 struct pipe_transfer
*trans
;
104 /** An I8 texture image: */
109 /** Epsilon for Z comparisons */
110 #define Z_EPSILON 1e-06
114 * Make fragment program for glBitmap:
115 * Sample the texture and kill the fragment if the bit is 0.
116 * This program will be combined with the user's fragment program.
118 static struct st_fragment_program
*
119 make_bitmap_fragment_program(struct gl_context
*ctx
, GLuint samplerIndex
)
121 struct st_context
*st
= st_context(ctx
);
122 struct st_fragment_program
*stfp
;
123 struct gl_program
*p
;
126 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
130 p
->NumInstructions
= 3;
132 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
133 if (!p
->Instructions
) {
134 ctx
->Driver
.DeleteProgram(ctx
, p
);
137 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
139 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
140 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
141 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
142 p
->Instructions
[ic
].DstReg
.Index
= 0;
143 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
144 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
145 p
->Instructions
[ic
].TexSrcUnit
= samplerIndex
;
146 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
149 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
150 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
151 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
153 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
154 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
156 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
157 p
->Instructions
[ic
].SrcReg
[0].Negate
= NEGATE_XYZW
;
161 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
163 assert(ic
== p
->NumInstructions
);
165 p
->InputsRead
= FRAG_BIT_TEX0
;
166 p
->OutputsWritten
= 0x0;
167 p
->SamplersUsed
= (1 << samplerIndex
);
169 stfp
= (struct st_fragment_program
*) p
;
170 stfp
->Base
.UsesKill
= GL_TRUE
;
176 static struct gl_program
*
177 make_bitmap_fragment_program_glsl(struct st_context
*st
,
178 struct st_fragment_program
*orig
,
181 struct gl_context
*ctx
= st
->ctx
;
182 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
183 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
188 get_bitmap_visitor(fp
, orig
->glsl_to_tgsi
, samplerIndex
);
189 return &fp
->Base
.Base
;
194 find_free_bit(uint bitfield
)
197 for (i
= 0; i
< 32; i
++) {
198 if ((bitfield
& (1 << i
)) == 0) {
207 * Combine basic bitmap fragment program with the user-defined program.
208 * \param st current context
209 * \param fpIn the incoming fragment program
210 * \param fpOut the new fragment program which does fragment culling
211 * \param bitmap_sampler sampler number for the bitmap texture
214 st_make_bitmap_fragment_program(struct st_context
*st
,
215 struct gl_fragment_program
*fpIn
,
216 struct gl_fragment_program
**fpOut
,
217 GLuint
*bitmap_sampler
)
219 struct st_fragment_program
*bitmap_prog
;
220 struct st_fragment_program
*stfpIn
= (struct st_fragment_program
*) fpIn
;
221 struct gl_program
*newProg
;
225 * Generate new program which is the user-defined program prefixed
226 * with the bitmap sampler/kill instructions.
228 sampler
= find_free_bit(fpIn
->Base
.SamplersUsed
);
230 if (stfpIn
->glsl_to_tgsi
)
231 newProg
= make_bitmap_fragment_program_glsl(st
, stfpIn
, sampler
);
233 bitmap_prog
= make_bitmap_fragment_program(st
->ctx
, sampler
);
235 newProg
= _mesa_combine_programs(st
->ctx
,
236 &bitmap_prog
->Base
.Base
,
238 /* done with this after combining */
239 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
244 printf("Combined bitmap program:\n");
245 _mesa_print_program(newProg
);
246 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
247 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
248 _mesa_print_parameter_list(newProg
->Parameters
);
253 *fpOut
= (struct gl_fragment_program
*) newProg
;
254 *bitmap_sampler
= sampler
;
259 * Copy user-provide bitmap bits into texture buffer, expanding
261 * "On" bits will set texels to 0x0.
262 * "Off" bits will not modify texels.
263 * Note that the image is actually going to be upside down in
264 * the texture. We deal with that with texcoords.
267 unpack_bitmap(struct st_context
*st
,
268 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
269 const struct gl_pixelstore_attrib
*unpack
,
270 const GLubyte
*bitmap
,
271 ubyte
*destBuffer
, uint destStride
)
273 destBuffer
+= py
* destStride
+ px
;
275 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
276 destBuffer
, destStride
, 0x0);
281 * Create a texture which represents a bitmap image.
283 static struct pipe_resource
*
284 make_bitmap_texture(struct gl_context
*ctx
, GLsizei width
, GLsizei height
,
285 const struct gl_pixelstore_attrib
*unpack
,
286 const GLubyte
*bitmap
)
288 struct st_context
*st
= st_context(ctx
);
289 struct pipe_context
*pipe
= st
->pipe
;
290 struct pipe_transfer
*transfer
;
292 struct pipe_resource
*pt
;
295 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
301 * Create texture to hold bitmap pattern.
303 pt
= st_texture_create(st
, st
->internal_target
, st
->bitmap
.tex_format
,
304 0, width
, height
, 1, 1,
305 PIPE_BIND_SAMPLER_VIEW
);
307 _mesa_unmap_pbo_source(ctx
, unpack
);
311 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
313 0, 0, width
, height
);
315 dest
= pipe_transfer_map(pipe
, transfer
);
317 /* Put image into texture transfer */
318 memset(dest
, 0xff, height
* transfer
->stride
);
319 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
320 dest
, transfer
->stride
);
322 _mesa_unmap_pbo_source(ctx
, unpack
);
324 /* Release transfer */
325 pipe_transfer_unmap(pipe
, transfer
);
326 pipe
->transfer_destroy(pipe
, transfer
);
332 setup_bitmap_vertex_data(struct st_context
*st
, bool normalized
,
333 int x
, int y
, int width
, int height
,
334 float z
, const float color
[4],
335 struct pipe_resource
**vbuf
,
336 unsigned *vbuf_offset
)
338 const GLfloat fb_width
= (GLfloat
)st
->state
.framebuffer
.width
;
339 const GLfloat fb_height
= (GLfloat
)st
->state
.framebuffer
.height
;
340 const GLfloat x0
= (GLfloat
)x
;
341 const GLfloat x1
= (GLfloat
)(x
+ width
);
342 const GLfloat y0
= (GLfloat
)y
;
343 const GLfloat y1
= (GLfloat
)(y
+ height
);
344 GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
345 GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
346 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
347 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
348 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
349 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
351 float (*vertices
)[3][4]; /**< vertex pos + color + texcoord */
355 sRight
= (GLfloat
) width
;
356 tBot
= (GLfloat
) height
;
359 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(vertices
[0]), vbuf_offset
, vbuf
,
365 /* Positions are in clip coords since we need to do clipping in case
366 * the bitmap quad goes beyond the window bounds.
368 vertices
[0][0][0] = clip_x0
;
369 vertices
[0][0][1] = clip_y0
;
370 vertices
[0][2][0] = sLeft
;
371 vertices
[0][2][1] = tTop
;
373 vertices
[1][0][0] = clip_x1
;
374 vertices
[1][0][1] = clip_y0
;
375 vertices
[1][2][0] = sRight
;
376 vertices
[1][2][1] = tTop
;
378 vertices
[2][0][0] = clip_x1
;
379 vertices
[2][0][1] = clip_y1
;
380 vertices
[2][2][0] = sRight
;
381 vertices
[2][2][1] = tBot
;
383 vertices
[3][0][0] = clip_x0
;
384 vertices
[3][0][1] = clip_y1
;
385 vertices
[3][2][0] = sLeft
;
386 vertices
[3][2][1] = tBot
;
388 /* same for all verts: */
389 for (i
= 0; i
< 4; i
++) {
390 vertices
[i
][0][2] = z
;
391 vertices
[i
][0][3] = 1.0f
;
392 vertices
[i
][1][0] = color
[0];
393 vertices
[i
][1][1] = color
[1];
394 vertices
[i
][1][2] = color
[2];
395 vertices
[i
][1][3] = color
[3];
396 vertices
[i
][2][2] = 0.0; /*R*/
397 vertices
[i
][2][3] = 1.0; /*Q*/
400 u_upload_unmap(st
->uploader
);
406 * Render a glBitmap by drawing a textured quad
409 draw_bitmap_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
410 GLsizei width
, GLsizei height
,
411 struct pipe_sampler_view
*sv
,
412 const GLfloat
*color
)
414 struct st_context
*st
= st_context(ctx
);
415 struct pipe_context
*pipe
= st
->pipe
;
416 struct cso_context
*cso
= st
->cso_context
;
417 struct st_fp_variant
*fpv
;
418 struct st_fp_variant_key key
;
421 struct pipe_resource
*vbuf
= NULL
;
423 memset(&key
, 0, sizeof(key
));
425 key
.bitmap
= GL_TRUE
;
426 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
427 st
->ctx
->Color
._ClampFragmentColor
&&
428 !st
->ctx
->DrawBuffer
->_IntegerColor
;
430 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
432 /* As an optimization, Mesa's fragment programs will sometimes get the
433 * primary color from a statevar/constant rather than a varying variable.
434 * when that's the case, we need to ensure that we use the 'color'
435 * parameter and not the current attribute color (which may have changed
436 * through glRasterPos and state validation.
437 * So, we force the proper color here. Not elegant, but it works.
440 GLfloat colorSave
[4];
441 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
442 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
443 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
444 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
449 /* XXX if the bitmap is larger than the max texture size, break
452 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
453 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
454 assert(width
<= (GLsizei
)maxSize
);
455 assert(height
<= (GLsizei
)maxSize
);
457 cso_save_rasterizer(cso
);
458 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
459 cso_save_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
460 cso_save_viewport(cso
);
461 cso_save_fragment_shader(cso
);
462 cso_save_stream_outputs(cso
);
463 cso_save_vertex_shader(cso
);
464 cso_save_geometry_shader(cso
);
465 cso_save_vertex_elements(cso
);
466 cso_save_vertex_buffers(cso
);
468 /* rasterizer state: just scissor */
469 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
470 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
472 /* fragment shader state: TEX lookup program */
473 cso_set_fragment_shader_handle(cso
, fpv
->driver_shader
);
475 /* vertex shader state: position + texcoord pass-through */
476 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
478 /* geometry shader state: disabled */
479 cso_set_geometry_shader_handle(cso
, NULL
);
481 /* user samplers, plus our bitmap sampler */
483 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
484 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
485 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
487 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++) {
488 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
490 samplers
[fpv
->bitmap_sampler
] =
491 &st
->bitmap
.samplers
[sv
->texture
->target
!= PIPE_TEXTURE_RECT
];
492 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
,
493 (const struct pipe_sampler_state
**) samplers
);
496 /* user textures, plus the bitmap texture */
498 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
499 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
500 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
501 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
502 sizeof(sampler_views
));
503 sampler_views
[fpv
->bitmap_sampler
] = sv
;
504 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
507 /* viewport state: viewport matching window dims */
509 const GLboolean invert
= st
->state
.fb_orientation
== Y_0_TOP
;
510 const GLfloat width
= (GLfloat
)st
->state
.framebuffer
.width
;
511 const GLfloat height
= (GLfloat
)st
->state
.framebuffer
.height
;
512 struct pipe_viewport_state vp
;
513 vp
.scale
[0] = 0.5f
* width
;
514 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
517 vp
.translate
[0] = 0.5f
* width
;
518 vp
.translate
[1] = 0.5f
* height
;
519 vp
.translate
[2] = 0.5f
;
520 vp
.translate
[3] = 0.0f
;
521 cso_set_viewport(cso
, &vp
);
524 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
525 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
527 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
530 /* draw textured quad */
531 setup_bitmap_vertex_data(st
, sv
->texture
->target
!= PIPE_TEXTURE_RECT
,
532 x
, y
, width
, height
, z
, color
, &vbuf
, &offset
);
535 util_draw_vertex_buffer(pipe
, st
->cso_context
, vbuf
, offset
,
536 PIPE_PRIM_TRIANGLE_FAN
,
538 3); /* attribs/vert */
542 cso_restore_rasterizer(cso
);
543 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
544 cso_restore_sampler_views(cso
, PIPE_SHADER_FRAGMENT
);
545 cso_restore_viewport(cso
);
546 cso_restore_fragment_shader(cso
);
547 cso_restore_vertex_shader(cso
);
548 cso_restore_geometry_shader(cso
);
549 cso_restore_vertex_elements(cso
);
550 cso_restore_vertex_buffers(cso
);
551 cso_restore_stream_outputs(cso
);
553 pipe_resource_reference(&vbuf
, NULL
);
558 reset_cache(struct st_context
*st
)
560 struct pipe_context
*pipe
= st
->pipe
;
561 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
563 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
564 cache
->empty
= GL_TRUE
;
566 cache
->xmin
= 1000000;
567 cache
->xmax
= -1000000;
568 cache
->ymin
= 1000000;
569 cache
->ymax
= -1000000;
572 pipe
->transfer_destroy(pipe
, cache
->trans
);
576 assert(!cache
->texture
);
578 /* allocate a new texture */
579 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
580 st
->bitmap
.tex_format
, 0,
581 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
583 PIPE_BIND_SAMPLER_VIEW
);
587 /** Print bitmap image to stdout (debug) */
589 print_cache(const struct bitmap_cache
*cache
)
593 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
594 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
595 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
596 if (cache
->buffer
[k
])
608 * Create gallium pipe_transfer object for the bitmap cache.
611 create_cache_trans(struct st_context
*st
)
613 struct pipe_context
*pipe
= st
->pipe
;
614 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
619 /* Map the texture transfer.
620 * Subsequent glBitmap calls will write into the texture image.
622 cache
->trans
= pipe_get_transfer(st
->pipe
, cache
->texture
, 0, 0,
623 PIPE_TRANSFER_WRITE
, 0, 0,
625 BITMAP_CACHE_HEIGHT
);
626 cache
->buffer
= pipe_transfer_map(pipe
, cache
->trans
);
628 /* init image to all 0xff */
629 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
634 * If there's anything in the bitmap cache, draw/flush it now.
637 st_flush_bitmap_cache(struct st_context
*st
)
639 if (!st
->bitmap
.cache
->empty
) {
640 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
642 struct pipe_context
*pipe
= st
->pipe
;
643 struct pipe_sampler_view
*sv
;
645 assert(cache
->xmin
<= cache
->xmax
);
647 /* printf("flush size %d x %d at %d, %d\n",
648 cache->xmax - cache->xmin,
649 cache->ymax - cache->ymin,
650 cache->xpos, cache->ypos);
653 /* The texture transfer has been mapped until now.
654 * So unmap and release the texture transfer before drawing.
659 pipe_transfer_unmap(pipe
, cache
->trans
);
660 cache
->buffer
= NULL
;
662 pipe
->transfer_destroy(pipe
, cache
->trans
);
666 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
668 draw_bitmap_quad(st
->ctx
,
672 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
676 pipe_sampler_view_reference(&sv
, NULL
);
679 /* release/free the texture */
680 pipe_resource_reference(&cache
->texture
, NULL
);
688 * Try to accumulate this glBitmap call in the bitmap cache.
689 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
692 accum_bitmap(struct st_context
*st
,
693 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
694 const struct gl_pixelstore_attrib
*unpack
,
695 const GLubyte
*bitmap
)
697 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
698 int px
= -999, py
= -999;
699 const GLfloat z
= st
->ctx
->Current
.RasterPos
[2];
701 if (width
> BITMAP_CACHE_WIDTH
||
702 height
> BITMAP_CACHE_HEIGHT
)
703 return GL_FALSE
; /* too big to cache */
706 px
= x
- cache
->xpos
; /* pos in buffer */
707 py
= y
- cache
->ypos
;
708 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
709 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
710 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
) ||
711 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
712 /* This bitmap would extend beyond cache bounds, or the bitmap
714 * so flush and continue.
716 st_flush_bitmap_cache(st
);
721 /* Initialize. Center bitmap vertically in the buffer. */
723 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
725 cache
->ypos
= y
- py
;
727 cache
->empty
= GL_FALSE
;
728 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
738 if (x
+ width
> cache
->xmax
)
739 cache
->xmax
= x
+ width
;
740 if (y
+ height
> cache
->ymax
)
741 cache
->ymax
= y
+ height
;
743 /* create the transfer if needed */
744 create_cache_trans(st
);
746 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
747 cache
->buffer
, BITMAP_CACHE_WIDTH
);
749 return GL_TRUE
; /* accumulated */
755 * Called via ctx->Driver.Bitmap()
758 st_Bitmap(struct gl_context
*ctx
, GLint x
, GLint y
,
759 GLsizei width
, GLsizei height
,
760 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
762 struct st_context
*st
= st_context(ctx
);
763 struct pipe_resource
*pt
;
765 if (width
== 0 || height
== 0)
768 st_validate_state(st
);
770 if (!st
->bitmap
.vs
) {
771 /* create pass-through vertex shader now */
772 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
774 TGSI_SEMANTIC_GENERIC
};
775 const uint semantic_indexes
[] = { 0, 0, 0 };
776 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
781 if (UseBitmapCache
&& accum_bitmap(st
, x
, y
, width
, height
, unpack
, bitmap
))
784 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
786 struct pipe_sampler_view
*sv
=
787 st_create_texture_sampler_view(st
->pipe
, pt
);
789 assert(pt
->target
== PIPE_TEXTURE_2D
|| pt
->target
== PIPE_TEXTURE_RECT
);
792 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
794 st
->ctx
->Current
.RasterColor
);
796 pipe_sampler_view_reference(&sv
, NULL
);
799 /* release/free the texture */
800 pipe_resource_reference(&pt
, NULL
);
805 /** Per-context init */
807 st_init_bitmap_functions(struct dd_function_table
*functions
)
809 functions
->Bitmap
= st_Bitmap
;
813 /** Per-context init */
815 st_init_bitmap(struct st_context
*st
)
817 struct pipe_sampler_state
*sampler
= &st
->bitmap
.samplers
[0];
818 struct pipe_context
*pipe
= st
->pipe
;
819 struct pipe_screen
*screen
= pipe
->screen
;
821 /* init sampler state once */
822 memset(sampler
, 0, sizeof(*sampler
));
823 sampler
->wrap_s
= PIPE_TEX_WRAP_CLAMP
;
824 sampler
->wrap_t
= PIPE_TEX_WRAP_CLAMP
;
825 sampler
->wrap_r
= PIPE_TEX_WRAP_CLAMP
;
826 sampler
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
827 sampler
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
828 sampler
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
829 st
->bitmap
.samplers
[1] = *sampler
;
830 st
->bitmap
.samplers
[1].normalized_coords
= 1;
832 /* init baseline rasterizer state once */
833 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
834 st
->bitmap
.rasterizer
.gl_rasterization_rules
= 1;
835 st
->bitmap
.rasterizer
.depth_clip
= 1;
837 /* find a usable texture format */
838 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
,
840 PIPE_BIND_SAMPLER_VIEW
)) {
841 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
843 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
,
845 PIPE_BIND_SAMPLER_VIEW
)) {
846 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
848 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
,
850 PIPE_BIND_SAMPLER_VIEW
)) {
851 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
854 /* XXX support more formats */
858 /* alloc bitmap cache object */
859 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
865 /** Per-context tear-down */
867 st_destroy_bitmap(struct st_context
*st
)
869 struct pipe_context
*pipe
= st
->pipe
;
870 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
873 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
874 st
->bitmap
.vs
= NULL
;
879 pipe_transfer_unmap(pipe
, cache
->trans
);
880 pipe
->transfer_destroy(pipe
, cache
->trans
);
882 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
883 free(st
->bitmap
.cache
);
884 st
->bitmap
.cache
= NULL
;
888 #endif /* FEATURE_drawpix */