1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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"
38 #include "program/program.h"
39 #include "program/prog_print.h"
41 #include "st_context.h"
43 #include "st_atom_constbuf.h"
44 #include "st_program.h"
45 #include "st_cb_bitmap.h"
46 #include "st_texture.h"
48 #include "pipe/p_context.h"
49 #include "pipe/p_defines.h"
50 #include "pipe/p_shader_tokens.h"
51 #include "util/u_inlines.h"
52 #include "util/u_draw_quad.h"
53 #include "util/u_simple_shaders.h"
54 #include "util/u_upload_mgr.h"
55 #include "program/prog_instruction.h"
56 #include "cso_cache/cso_context.h"
60 * glBitmaps are drawn as textured quads. The user's bitmap pattern
61 * is stored in a texture image. An alpha8 texture format is used.
62 * The fragment shader samples a bit (texel) from the texture, then
63 * discards the fragment if the bit is off.
65 * Note that we actually store the inverse image of the bitmap to
66 * simplify the fragment program. An "on" bit gets stored as texel=0x0
67 * and an "off" bit is stored as texel=0xff. Then we kill the
68 * fragment if the negated texel value is less than zero.
73 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
74 * buffer which is then rendered en mass upon a flush, state change, etc.
75 * A wide, short buffer is used to target the common case of a series
76 * of glBitmap calls being used to draw text.
78 static GLboolean UseBitmapCache
= GL_TRUE
;
81 #define BITMAP_CACHE_WIDTH 512
82 #define BITMAP_CACHE_HEIGHT 32
86 /** Window pos to render the cached image */
88 /** Bounds of region used in window coords */
89 GLint xmin
, ymin
, xmax
, ymax
;
93 /** Bitmap's Z position */
96 struct pipe_resource
*texture
;
97 struct pipe_transfer
*trans
;
101 /** An I8 texture image: */
106 /** Epsilon for Z comparisons */
107 #define Z_EPSILON 1e-06
111 * Make fragment program for glBitmap:
112 * Sample the texture and kill the fragment if the bit is 0.
113 * This program will be combined with the user's fragment program.
115 static struct st_fragment_program
*
116 make_bitmap_fragment_program(struct gl_context
*ctx
, GLuint samplerIndex
)
118 struct st_context
*st
= st_context(ctx
);
119 struct st_fragment_program
*stfp
;
120 struct gl_program
*p
;
123 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
127 p
->NumInstructions
= 3;
129 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
130 if (!p
->Instructions
) {
131 ctx
->Driver
.DeleteProgram(ctx
, p
);
134 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
136 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
137 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
138 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
139 p
->Instructions
[ic
].DstReg
.Index
= 0;
140 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
141 p
->Instructions
[ic
].SrcReg
[0].Index
= VARYING_SLOT_TEX0
;
142 p
->Instructions
[ic
].TexSrcUnit
= samplerIndex
;
143 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
146 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
147 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
148 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
150 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
151 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
153 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
154 p
->Instructions
[ic
].SrcReg
[0].Negate
= NEGATE_XYZW
;
158 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
160 assert(ic
== p
->NumInstructions
);
162 p
->InputsRead
= VARYING_BIT_TEX0
;
163 p
->OutputsWritten
= 0x0;
164 p
->SamplersUsed
= (1 << samplerIndex
);
166 stfp
= (struct st_fragment_program
*) p
;
167 stfp
->Base
.UsesKill
= GL_TRUE
;
173 static struct gl_program
*
174 make_bitmap_fragment_program_glsl(struct st_context
*st
,
175 struct st_fragment_program
*orig
,
178 struct gl_context
*ctx
= st
->ctx
;
179 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
180 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
185 get_bitmap_visitor(fp
, orig
->glsl_to_tgsi
, samplerIndex
);
186 return &fp
->Base
.Base
;
191 find_free_bit(uint bitfield
)
194 for (i
= 0; i
< 32; i
++) {
195 if ((bitfield
& (1 << i
)) == 0) {
204 * Combine basic bitmap fragment program with the user-defined program.
205 * \param st current context
206 * \param fpIn the incoming fragment program
207 * \param fpOut the new fragment program which does fragment culling
208 * \param bitmap_sampler sampler number for the bitmap texture
211 st_make_bitmap_fragment_program(struct st_context
*st
,
212 struct gl_fragment_program
*fpIn
,
213 struct gl_fragment_program
**fpOut
,
214 GLuint
*bitmap_sampler
)
216 struct st_fragment_program
*bitmap_prog
;
217 struct st_fragment_program
*stfpIn
= (struct st_fragment_program
*) fpIn
;
218 struct gl_program
*newProg
;
222 * Generate new program which is the user-defined program prefixed
223 * with the bitmap sampler/kill instructions.
225 sampler
= find_free_bit(fpIn
->Base
.SamplersUsed
);
227 if (stfpIn
->glsl_to_tgsi
)
228 newProg
= make_bitmap_fragment_program_glsl(st
, stfpIn
, sampler
);
230 bitmap_prog
= make_bitmap_fragment_program(st
->ctx
, sampler
);
232 newProg
= _mesa_combine_programs(st
->ctx
,
233 &bitmap_prog
->Base
.Base
,
235 /* done with this after combining */
236 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
241 printf("Combined bitmap program:\n");
242 _mesa_print_program(newProg
);
243 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
244 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
245 _mesa_print_parameter_list(newProg
->Parameters
);
250 *fpOut
= (struct gl_fragment_program
*) newProg
;
251 *bitmap_sampler
= sampler
;
256 * Copy user-provide bitmap bits into texture buffer, expanding
258 * "On" bits will set texels to 0x0.
259 * "Off" bits will not modify texels.
260 * Note that the image is actually going to be upside down in
261 * the texture. We deal with that with texcoords.
264 unpack_bitmap(struct st_context
*st
,
265 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
266 const struct gl_pixelstore_attrib
*unpack
,
267 const GLubyte
*bitmap
,
268 ubyte
*destBuffer
, uint destStride
)
270 destBuffer
+= py
* destStride
+ px
;
272 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
273 destBuffer
, destStride
, 0x0);
278 * Create a texture which represents a bitmap image.
280 static struct pipe_resource
*
281 make_bitmap_texture(struct gl_context
*ctx
, GLsizei width
, GLsizei height
,
282 const struct gl_pixelstore_attrib
*unpack
,
283 const GLubyte
*bitmap
)
285 struct st_context
*st
= st_context(ctx
);
286 struct pipe_context
*pipe
= st
->pipe
;
287 struct pipe_transfer
*transfer
;
289 struct pipe_resource
*pt
;
292 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
298 * Create texture to hold bitmap pattern.
300 pt
= st_texture_create(st
, st
->internal_target
, st
->bitmap
.tex_format
,
301 0, width
, height
, 1, 1, 0,
302 PIPE_BIND_SAMPLER_VIEW
);
304 _mesa_unmap_pbo_source(ctx
, unpack
);
308 dest
= pipe_transfer_map(st
->pipe
, pt
, 0, 0,
310 0, 0, width
, height
, &transfer
);
312 /* Put image into texture transfer */
313 memset(dest
, 0xff, height
* transfer
->stride
);
314 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
315 dest
, transfer
->stride
);
317 _mesa_unmap_pbo_source(ctx
, unpack
);
319 /* Release transfer */
320 pipe_transfer_unmap(pipe
, transfer
);
325 setup_bitmap_vertex_data(struct st_context
*st
, bool normalized
,
326 int x
, int y
, int width
, int height
,
327 float z
, const float color
[4],
328 struct pipe_resource
**vbuf
,
329 unsigned *vbuf_offset
)
331 const GLfloat fb_width
= (GLfloat
)st
->state
.framebuffer
.width
;
332 const GLfloat fb_height
= (GLfloat
)st
->state
.framebuffer
.height
;
333 const GLfloat x0
= (GLfloat
)x
;
334 const GLfloat x1
= (GLfloat
)(x
+ width
);
335 const GLfloat y0
= (GLfloat
)y
;
336 const GLfloat y1
= (GLfloat
)(y
+ height
);
337 GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
338 GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
339 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
340 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
341 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
342 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
344 float (*vertices
)[3][4]; /**< vertex pos + color + texcoord */
348 sRight
= (GLfloat
) width
;
349 tBot
= (GLfloat
) height
;
352 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(vertices
[0]),
353 vbuf_offset
, vbuf
, (void **) &vertices
);
358 /* Positions are in clip coords since we need to do clipping in case
359 * the bitmap quad goes beyond the window bounds.
361 vertices
[0][0][0] = clip_x0
;
362 vertices
[0][0][1] = clip_y0
;
363 vertices
[0][2][0] = sLeft
;
364 vertices
[0][2][1] = tTop
;
366 vertices
[1][0][0] = clip_x1
;
367 vertices
[1][0][1] = clip_y0
;
368 vertices
[1][2][0] = sRight
;
369 vertices
[1][2][1] = tTop
;
371 vertices
[2][0][0] = clip_x1
;
372 vertices
[2][0][1] = clip_y1
;
373 vertices
[2][2][0] = sRight
;
374 vertices
[2][2][1] = tBot
;
376 vertices
[3][0][0] = clip_x0
;
377 vertices
[3][0][1] = clip_y1
;
378 vertices
[3][2][0] = sLeft
;
379 vertices
[3][2][1] = tBot
;
381 /* same for all verts: */
382 for (i
= 0; i
< 4; i
++) {
383 vertices
[i
][0][2] = z
;
384 vertices
[i
][0][3] = 1.0f
;
385 vertices
[i
][1][0] = color
[0];
386 vertices
[i
][1][1] = color
[1];
387 vertices
[i
][1][2] = color
[2];
388 vertices
[i
][1][3] = color
[3];
389 vertices
[i
][2][2] = 0.0; /*R*/
390 vertices
[i
][2][3] = 1.0; /*Q*/
393 u_upload_unmap(st
->uploader
);
399 * Render a glBitmap by drawing a textured quad
402 draw_bitmap_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
403 GLsizei width
, GLsizei height
,
404 struct pipe_sampler_view
*sv
,
405 const GLfloat
*color
)
407 struct st_context
*st
= st_context(ctx
);
408 struct pipe_context
*pipe
= st
->pipe
;
409 struct cso_context
*cso
= st
->cso_context
;
410 struct st_fp_variant
*fpv
;
411 struct st_fp_variant_key key
;
414 struct pipe_resource
*vbuf
= NULL
;
416 memset(&key
, 0, sizeof(key
));
418 key
.bitmap
= GL_TRUE
;
419 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
420 st
->ctx
->Color
._ClampFragmentColor
;
422 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
424 /* As an optimization, Mesa's fragment programs will sometimes get the
425 * primary color from a statevar/constant rather than a varying variable.
426 * when that's the case, we need to ensure that we use the 'color'
427 * parameter and not the current attribute color (which may have changed
428 * through glRasterPos and state validation.
429 * So, we force the proper color here. Not elegant, but it works.
432 GLfloat colorSave
[4];
433 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
434 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
435 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
436 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
441 /* XXX if the bitmap is larger than the max texture size, break
444 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
445 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
446 assert(width
<= (GLsizei
)maxSize
);
447 assert(height
<= (GLsizei
)maxSize
);
449 cso_save_rasterizer(cso
);
450 cso_save_fragment_samplers(cso
);
451 cso_save_fragment_sampler_views(cso
);
452 cso_save_viewport(cso
);
453 cso_save_fragment_shader(cso
);
454 cso_save_stream_outputs(cso
);
455 cso_save_vertex_shader(cso
);
456 cso_save_tessctrl_shader(cso
);
457 cso_save_tesseval_shader(cso
);
458 cso_save_geometry_shader(cso
);
459 cso_save_vertex_elements(cso
);
460 cso_save_aux_vertex_buffer_slot(cso
);
462 /* rasterizer state: just scissor */
463 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
& 1;
464 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
466 /* fragment shader state: TEX lookup program */
467 cso_set_fragment_shader_handle(cso
, fpv
->driver_shader
);
469 /* vertex shader state: position + texcoord pass-through */
470 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
472 /* disable other shaders */
473 cso_set_tessctrl_shader_handle(cso
, NULL
);
474 cso_set_tesseval_shader_handle(cso
, NULL
);
475 cso_set_geometry_shader_handle(cso
, NULL
);
477 /* user samplers, plus our bitmap sampler */
479 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
480 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
481 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
483 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++) {
484 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
486 samplers
[fpv
->bitmap_sampler
] =
487 &st
->bitmap
.samplers
[sv
->texture
->target
!= PIPE_TEXTURE_RECT
];
488 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
,
489 (const struct pipe_sampler_state
**) samplers
);
492 /* user textures, plus the bitmap texture */
494 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
495 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
496 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
497 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
498 sizeof(sampler_views
));
499 sampler_views
[fpv
->bitmap_sampler
] = sv
;
500 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
503 /* viewport state: viewport matching window dims */
505 const GLboolean invert
= st
->state
.fb_orientation
== Y_0_TOP
;
506 const GLfloat width
= (GLfloat
)st
->state
.framebuffer
.width
;
507 const GLfloat height
= (GLfloat
)st
->state
.framebuffer
.height
;
508 struct pipe_viewport_state vp
;
509 vp
.scale
[0] = 0.5f
* width
;
510 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
512 vp
.translate
[0] = 0.5f
* width
;
513 vp
.translate
[1] = 0.5f
* height
;
514 vp
.translate
[2] = 0.5f
;
515 cso_set_viewport(cso
, &vp
);
518 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
519 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
521 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
524 /* draw textured quad */
525 setup_bitmap_vertex_data(st
, sv
->texture
->target
!= PIPE_TEXTURE_RECT
,
526 x
, y
, width
, height
, z
, color
, &vbuf
, &offset
);
529 util_draw_vertex_buffer(pipe
, st
->cso_context
, vbuf
,
530 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
532 PIPE_PRIM_TRIANGLE_FAN
,
534 3); /* attribs/vert */
538 cso_restore_rasterizer(cso
);
539 cso_restore_fragment_samplers(cso
);
540 cso_restore_fragment_sampler_views(cso
);
541 cso_restore_viewport(cso
);
542 cso_restore_fragment_shader(cso
);
543 cso_restore_vertex_shader(cso
);
544 cso_restore_tessctrl_shader(cso
);
545 cso_restore_tesseval_shader(cso
);
546 cso_restore_geometry_shader(cso
);
547 cso_restore_vertex_elements(cso
);
548 cso_restore_aux_vertex_buffer_slot(cso
);
549 cso_restore_stream_outputs(cso
);
551 pipe_resource_reference(&vbuf
, NULL
);
556 reset_cache(struct st_context
*st
)
558 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
560 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
561 cache
->empty
= GL_TRUE
;
563 cache
->xmin
= 1000000;
564 cache
->xmax
= -1000000;
565 cache
->ymin
= 1000000;
566 cache
->ymax
= -1000000;
568 assert(!cache
->texture
);
570 /* allocate a new texture */
571 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
572 st
->bitmap
.tex_format
, 0,
573 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
575 PIPE_BIND_SAMPLER_VIEW
);
579 /** Print bitmap image to stdout (debug) */
581 print_cache(const struct bitmap_cache
*cache
)
585 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
586 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
587 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
588 if (cache
->buffer
[k
])
600 * Create gallium pipe_transfer object for the bitmap cache.
603 create_cache_trans(struct st_context
*st
)
605 struct pipe_context
*pipe
= st
->pipe
;
606 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
611 /* Map the texture transfer.
612 * Subsequent glBitmap calls will write into the texture image.
614 cache
->buffer
= pipe_transfer_map(pipe
, cache
->texture
, 0, 0,
615 PIPE_TRANSFER_WRITE
, 0, 0,
617 BITMAP_CACHE_HEIGHT
, &cache
->trans
);
619 /* init image to all 0xff */
620 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
625 * If there's anything in the bitmap cache, draw/flush it now.
628 st_flush_bitmap_cache(struct st_context
*st
)
630 if (!st
->bitmap
.cache
->empty
) {
631 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
633 struct pipe_context
*pipe
= st
->pipe
;
634 struct pipe_sampler_view
*sv
;
636 assert(cache
->xmin
<= cache
->xmax
);
638 /* printf("flush size %d x %d at %d, %d\n",
639 cache->xmax - cache->xmin,
640 cache->ymax - cache->ymin,
641 cache->xpos, cache->ypos);
644 /* The texture transfer has been mapped until now.
645 * So unmap and release the texture transfer before drawing.
647 if (cache
->trans
&& cache
->buffer
) {
650 pipe_transfer_unmap(pipe
, cache
->trans
);
651 cache
->buffer
= NULL
;
655 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
657 draw_bitmap_quad(st
->ctx
,
661 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
665 pipe_sampler_view_reference(&sv
, NULL
);
668 /* release/free the texture */
669 pipe_resource_reference(&cache
->texture
, NULL
);
677 * Try to accumulate this glBitmap call in the bitmap cache.
678 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
681 accum_bitmap(struct gl_context
*ctx
,
682 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
683 const struct gl_pixelstore_attrib
*unpack
,
684 const GLubyte
*bitmap
)
686 struct st_context
*st
= ctx
->st
;
687 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
688 int px
= -999, py
= -999;
689 const GLfloat z
= st
->ctx
->Current
.RasterPos
[2];
691 if (width
> BITMAP_CACHE_WIDTH
||
692 height
> BITMAP_CACHE_HEIGHT
)
693 return GL_FALSE
; /* too big to cache */
696 px
= x
- cache
->xpos
; /* pos in buffer */
697 py
= y
- cache
->ypos
;
698 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
699 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
700 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
) ||
701 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
702 /* This bitmap would extend beyond cache bounds, or the bitmap
704 * so flush and continue.
706 st_flush_bitmap_cache(st
);
711 /* Initialize. Center bitmap vertically in the buffer. */
713 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
715 cache
->ypos
= y
- py
;
717 cache
->empty
= GL_FALSE
;
718 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
728 if (x
+ width
> cache
->xmax
)
729 cache
->xmax
= x
+ width
;
730 if (y
+ height
> cache
->ymax
)
731 cache
->ymax
= y
+ height
;
733 /* create the transfer if needed */
734 create_cache_trans(st
);
737 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
742 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
743 cache
->buffer
, BITMAP_CACHE_WIDTH
);
745 _mesa_unmap_pbo_source(ctx
, unpack
);
747 return GL_TRUE
; /* accumulated */
753 * Called via ctx->Driver.Bitmap()
756 st_Bitmap(struct gl_context
*ctx
, GLint x
, GLint y
,
757 GLsizei width
, GLsizei height
,
758 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
760 struct st_context
*st
= st_context(ctx
);
761 struct pipe_resource
*pt
;
763 if (width
== 0 || height
== 0)
766 st_validate_state(st
);
768 if (!st
->bitmap
.vs
) {
769 /* create pass-through vertex shader now */
770 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
772 st
->needs_texcoord_semantic
? TGSI_SEMANTIC_TEXCOORD
:
773 TGSI_SEMANTIC_GENERIC
};
774 const uint semantic_indexes
[] = { 0, 0, 0 };
775 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
781 if (UseBitmapCache
&& accum_bitmap(ctx
, 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
.half_pixel_center
= 1;
835 st
->bitmap
.rasterizer
.bottom_edge_rule
= 1;
836 st
->bitmap
.rasterizer
.depth_clip
= 1;
838 /* find a usable texture format */
839 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
,
841 PIPE_BIND_SAMPLER_VIEW
)) {
842 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
844 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
,
846 PIPE_BIND_SAMPLER_VIEW
)) {
847 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
849 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
,
851 PIPE_BIND_SAMPLER_VIEW
)) {
852 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
855 /* XXX support more formats */
859 /* alloc bitmap cache object */
860 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
866 /** Per-context tear-down */
868 st_destroy_bitmap(struct st_context
*st
)
870 struct pipe_context
*pipe
= st
->pipe
;
871 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
874 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
875 st
->bitmap
.vs
= NULL
;
879 if (cache
->trans
&& cache
->buffer
) {
880 pipe_transfer_unmap(pipe
, cache
->trans
);
882 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
883 free(st
->bitmap
.cache
);
884 st
->bitmap
.cache
= NULL
;