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"
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_simple_shaders.h"
54 #include "program/prog_instruction.h"
55 #include "cso_cache/cso_context.h"
59 * glBitmaps are drawn as textured quads. The user's bitmap pattern
60 * is stored in a texture image. An alpha8 texture format is used.
61 * The fragment shader samples a bit (texel) from the texture, then
62 * discards the fragment if the bit is off.
64 * Note that we actually store the inverse image of the bitmap to
65 * simplify the fragment program. An "on" bit gets stored as texel=0x0
66 * and an "off" bit is stored as texel=0xff. Then we kill the
67 * fragment if the negated texel value is less than zero.
72 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
73 * buffer which is then rendered en mass upon a flush, state change, etc.
74 * A wide, short buffer is used to target the common case of a series
75 * of glBitmap calls being used to draw text.
77 static GLboolean UseBitmapCache
= GL_TRUE
;
80 #define BITMAP_CACHE_WIDTH 512
81 #define BITMAP_CACHE_HEIGHT 32
85 /** Window pos to render the cached image */
87 /** Bounds of region used in window coords */
88 GLint xmin
, ymin
, xmax
, ymax
;
92 /** Bitmap's Z position */
95 struct pipe_resource
*texture
;
96 struct pipe_transfer
*trans
;
100 /** An I8 texture image: */
105 /** Epsilon for Z comparisons */
106 #define Z_EPSILON 1e-06
110 * Copy user-provide bitmap bits into texture buffer, expanding
112 * "On" bits will set texels to 0x0.
113 * "Off" bits will not modify texels.
114 * Note that the image is actually going to be upside down in
115 * the texture. We deal with that with texcoords.
118 unpack_bitmap(struct st_context
*st
,
119 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
120 const struct gl_pixelstore_attrib
*unpack
,
121 const GLubyte
*bitmap
,
122 ubyte
*destBuffer
, uint destStride
)
124 destBuffer
+= py
* destStride
+ px
;
126 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
127 destBuffer
, destStride
, 0x0);
132 * Create a texture which represents a bitmap image.
134 static struct pipe_resource
*
135 make_bitmap_texture(struct gl_context
*ctx
, GLsizei width
, GLsizei height
,
136 const struct gl_pixelstore_attrib
*unpack
,
137 const GLubyte
*bitmap
)
139 struct st_context
*st
= st_context(ctx
);
140 struct pipe_context
*pipe
= st
->pipe
;
141 struct pipe_transfer
*transfer
;
143 struct pipe_resource
*pt
;
146 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
152 * Create texture to hold bitmap pattern.
154 pt
= st_texture_create(st
, st
->internal_target
, st
->bitmap
.tex_format
,
155 0, width
, height
, 1, 1, 0,
156 PIPE_BIND_SAMPLER_VIEW
);
158 _mesa_unmap_pbo_source(ctx
, unpack
);
162 dest
= pipe_transfer_map(st
->pipe
, pt
, 0, 0,
164 0, 0, width
, height
, &transfer
);
166 /* Put image into texture transfer */
167 memset(dest
, 0xff, height
* transfer
->stride
);
168 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
169 dest
, transfer
->stride
);
171 _mesa_unmap_pbo_source(ctx
, unpack
);
173 /* Release transfer */
174 pipe_transfer_unmap(pipe
, transfer
);
180 * Setup pipeline state prior to rendering the bitmap textured quad.
183 setup_render_state(struct gl_context
*ctx
,
184 struct pipe_sampler_view
*sv
,
185 const GLfloat
*color
)
187 struct st_context
*st
= st_context(ctx
);
188 struct cso_context
*cso
= st
->cso_context
;
189 struct st_fp_variant
*fpv
;
190 struct st_fp_variant_key key
;
192 memset(&key
, 0, sizeof(key
));
193 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
194 key
.bitmap
= GL_TRUE
;
195 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
196 st
->ctx
->Color
._ClampFragmentColor
;
198 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
200 /* As an optimization, Mesa's fragment programs will sometimes get the
201 * primary color from a statevar/constant rather than a varying variable.
202 * when that's the case, we need to ensure that we use the 'color'
203 * parameter and not the current attribute color (which may have changed
204 * through glRasterPos and state validation.
205 * So, we force the proper color here. Not elegant, but it works.
208 GLfloat colorSave
[4];
209 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
210 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
211 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
212 PIPE_SHADER_FRAGMENT
);
213 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
216 cso_save_state(cso
, (CSO_BIT_RASTERIZER
|
217 CSO_BIT_FRAGMENT_SAMPLERS
|
218 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
220 CSO_BIT_FRAGMENT_SHADER
|
221 CSO_BIT_STREAM_OUTPUTS
|
222 CSO_BIT_TESSCTRL_SHADER
|
223 CSO_BIT_TESSEVAL_SHADER
|
224 CSO_BIT_GEOMETRY_SHADER
|
225 CSO_BIT_VERTEX_ELEMENTS
|
226 CSO_BIT_VERTEX_SHADER
|
227 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
));
229 /* rasterizer state: just scissor */
230 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.EnableFlags
& 1;
231 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
233 /* fragment shader state: TEX lookup program */
234 cso_set_fragment_shader_handle(cso
, fpv
->driver_shader
);
236 /* vertex shader state: position + texcoord pass-through */
237 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
239 /* disable other shaders */
240 cso_set_tessctrl_shader_handle(cso
, NULL
);
241 cso_set_tesseval_shader_handle(cso
, NULL
);
242 cso_set_geometry_shader_handle(cso
, NULL
);
244 /* user samplers, plus our bitmap sampler */
246 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
247 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
248 st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
250 for (i
= 0; i
< st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]; i
++) {
251 samplers
[i
] = &st
->state
.samplers
[PIPE_SHADER_FRAGMENT
][i
];
253 samplers
[fpv
->bitmap_sampler
] = &st
->bitmap
.sampler
;
254 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
,
255 (const struct pipe_sampler_state
**) samplers
);
258 /* user textures, plus the bitmap texture */
260 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
261 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
262 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
263 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
264 sizeof(sampler_views
));
265 sampler_views
[fpv
->bitmap_sampler
] = sv
;
266 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
269 /* viewport state: viewport matching window dims */
270 cso_set_viewport_dims(cso
, st
->state
.framebuffer
.width
,
271 st
->state
.framebuffer
.height
,
272 st
->state
.fb_orientation
== Y_0_TOP
);
274 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
276 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
281 * Restore pipeline state after rendering the bitmap textured quad.
284 restore_render_state(struct gl_context
*ctx
)
286 struct st_context
*st
= st_context(ctx
);
287 struct cso_context
*cso
= st
->cso_context
;
289 cso_restore_state(cso
);
294 * Render a glBitmap by drawing a textured quad
297 draw_bitmap_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
298 GLsizei width
, GLsizei height
,
299 struct pipe_sampler_view
*sv
,
300 const GLfloat
*color
)
302 struct st_context
*st
= st_context(ctx
);
303 struct pipe_context
*pipe
= st
->pipe
;
304 const float fb_width
= (float) st
->state
.framebuffer
.width
;
305 const float fb_height
= (float) st
->state
.framebuffer
.height
;
306 const float x0
= (float) x
;
307 const float x1
= (float) (x
+ width
);
308 const float y0
= (float) y
;
309 const float y1
= (float) (y
+ height
);
310 float sLeft
= 0.0f
, sRight
= 1.0f
;
311 float tTop
= 0.0f
, tBot
= 1.0f
- tTop
;
312 const float clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
313 const float clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
314 const float clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
315 const float clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
319 /* XXX if the bitmap is larger than the max texture size, break
322 GLuint maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
323 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
324 assert(width
<= (GLsizei
) maxSize
);
325 assert(height
<= (GLsizei
) maxSize
);
328 setup_render_state(ctx
, sv
, color
);
330 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
333 if (sv
->texture
->target
== PIPE_TEXTURE_RECT
) {
334 /* use non-normalized texcoords */
335 sRight
= (float) width
;
336 tBot
= (float) height
;
339 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
340 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
341 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBitmap");
344 restore_render_state(ctx
);
346 /* We uploaded modified constants, need to invalidate them. */
347 st
->dirty
.mesa
|= _NEW_PROGRAM_CONSTANTS
;
352 reset_cache(struct st_context
*st
)
354 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
356 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
357 cache
->empty
= GL_TRUE
;
359 cache
->xmin
= 1000000;
360 cache
->xmax
= -1000000;
361 cache
->ymin
= 1000000;
362 cache
->ymax
= -1000000;
364 assert(!cache
->texture
);
366 /* allocate a new texture */
367 cache
->texture
= st_texture_create(st
, st
->internal_target
,
368 st
->bitmap
.tex_format
, 0,
369 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
371 PIPE_BIND_SAMPLER_VIEW
);
375 /** Print bitmap image to stdout (debug) */
377 print_cache(const struct bitmap_cache
*cache
)
381 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
382 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
383 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
384 if (cache
->buffer
[k
])
396 * Create gallium pipe_transfer object for the bitmap cache.
399 create_cache_trans(struct st_context
*st
)
401 struct pipe_context
*pipe
= st
->pipe
;
402 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
407 /* Map the texture transfer.
408 * Subsequent glBitmap calls will write into the texture image.
410 cache
->buffer
= pipe_transfer_map(pipe
, cache
->texture
, 0, 0,
411 PIPE_TRANSFER_WRITE
, 0, 0,
413 BITMAP_CACHE_HEIGHT
, &cache
->trans
);
415 /* init image to all 0xff */
416 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
421 * If there's anything in the bitmap cache, draw/flush it now.
424 st_flush_bitmap_cache(struct st_context
*st
)
426 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
428 if (cache
&& !cache
->empty
) {
429 struct pipe_context
*pipe
= st
->pipe
;
430 struct pipe_sampler_view
*sv
;
432 assert(cache
->xmin
<= cache
->xmax
);
435 printf("flush bitmap, size %d x %d at %d, %d\n",
436 cache
->xmax
- cache
->xmin
,
437 cache
->ymax
- cache
->ymin
,
438 cache
->xpos
, cache
->ypos
);
440 /* The texture transfer has been mapped until now.
441 * So unmap and release the texture transfer before drawing.
443 if (cache
->trans
&& cache
->buffer
) {
446 pipe_transfer_unmap(pipe
, cache
->trans
);
447 cache
->buffer
= NULL
;
451 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
453 draw_bitmap_quad(st
->ctx
,
457 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
461 pipe_sampler_view_reference(&sv
, NULL
);
464 /* release/free the texture */
465 pipe_resource_reference(&cache
->texture
, NULL
);
473 * Try to accumulate this glBitmap call in the bitmap cache.
474 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
477 accum_bitmap(struct gl_context
*ctx
,
478 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
479 const struct gl_pixelstore_attrib
*unpack
,
480 const GLubyte
*bitmap
)
482 struct st_context
*st
= ctx
->st
;
483 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
484 int px
= -999, py
= -999;
485 const GLfloat z
= st
->ctx
->Current
.RasterPos
[2];
487 if (width
> BITMAP_CACHE_WIDTH
||
488 height
> BITMAP_CACHE_HEIGHT
)
489 return GL_FALSE
; /* too big to cache */
492 px
= x
- cache
->xpos
; /* pos in buffer */
493 py
= y
- cache
->ypos
;
494 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
495 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
496 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
) ||
497 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
498 /* This bitmap would extend beyond cache bounds, or the bitmap
500 * so flush and continue.
502 st_flush_bitmap_cache(st
);
507 /* Initialize. Center bitmap vertically in the buffer. */
509 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
511 cache
->ypos
= y
- py
;
513 cache
->empty
= GL_FALSE
;
514 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
524 if (x
+ width
> cache
->xmax
)
525 cache
->xmax
= x
+ width
;
526 if (y
+ height
> cache
->ymax
)
527 cache
->ymax
= y
+ height
;
529 /* create the transfer if needed */
530 create_cache_trans(st
);
533 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
538 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
539 cache
->buffer
, BITMAP_CACHE_WIDTH
);
541 _mesa_unmap_pbo_source(ctx
, unpack
);
543 return GL_TRUE
; /* accumulated */
548 * One-time init for drawing bitmaps.
551 init_bitmap_state(struct st_context
*st
)
553 struct pipe_context
*pipe
= st
->pipe
;
554 struct pipe_screen
*screen
= pipe
->screen
;
556 /* This function should only be called once */
557 assert(st
->bitmap
.cache
== NULL
);
559 assert(st
->internal_target
== PIPE_TEXTURE_2D
||
560 st
->internal_target
== PIPE_TEXTURE_RECT
);
562 /* alloc bitmap cache object */
563 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
565 /* init sampler state once */
566 memset(&st
->bitmap
.sampler
, 0, sizeof(st
->bitmap
.sampler
));
567 st
->bitmap
.sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
568 st
->bitmap
.sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
569 st
->bitmap
.sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
570 st
->bitmap
.sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
571 st
->bitmap
.sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
572 st
->bitmap
.sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
573 st
->bitmap
.sampler
.normalized_coords
= st
->internal_target
== PIPE_TEXTURE_2D
;
575 /* init baseline rasterizer state once */
576 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
577 st
->bitmap
.rasterizer
.half_pixel_center
= 1;
578 st
->bitmap
.rasterizer
.bottom_edge_rule
= 1;
579 st
->bitmap
.rasterizer
.depth_clip
= 1;
581 /* find a usable texture format */
582 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
,
583 st
->internal_target
, 0,
584 PIPE_BIND_SAMPLER_VIEW
)) {
585 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
587 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
,
588 st
->internal_target
, 0,
589 PIPE_BIND_SAMPLER_VIEW
)) {
590 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
592 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
,
593 st
->internal_target
, 0,
594 PIPE_BIND_SAMPLER_VIEW
)) {
595 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
598 /* XXX support more formats */
602 /* Create the vertex shader */
604 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
606 st
->needs_texcoord_semantic
? TGSI_SEMANTIC_TEXCOORD
:
607 TGSI_SEMANTIC_GENERIC
};
608 const uint semantic_indexes
[] = { 0, 0, 0 };
609 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
620 * Called via ctx->Driver.Bitmap()
623 st_Bitmap(struct gl_context
*ctx
, GLint x
, GLint y
,
624 GLsizei width
, GLsizei height
,
625 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
627 struct st_context
*st
= st_context(ctx
);
628 struct pipe_resource
*pt
;
633 if (!st
->bitmap
.cache
) {
634 init_bitmap_state(st
);
637 /* We only need to validate state of the st dirty flags are set or
638 * any non-_NEW_PROGRAM_CONSTANTS mesa flags are set. The VS we use
639 * for bitmap drawing uses no constants and the FS constants are
640 * explicitly uploaded in the draw_bitmap_quad() function.
642 if ((st
->dirty
.mesa
& ~_NEW_PROGRAM_CONSTANTS
) || st
->dirty
.st
) {
643 st_validate_state(st
, ST_PIPELINE_RENDER
);
646 if (UseBitmapCache
&& accum_bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap
))
649 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
651 struct pipe_sampler_view
*sv
=
652 st_create_texture_sampler_view(st
->pipe
, pt
);
654 assert(pt
->target
== PIPE_TEXTURE_2D
|| pt
->target
== PIPE_TEXTURE_RECT
);
657 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
659 st
->ctx
->Current
.RasterColor
);
661 pipe_sampler_view_reference(&sv
, NULL
);
664 /* release/free the texture */
665 pipe_resource_reference(&pt
, NULL
);
670 /** Per-context init */
672 st_init_bitmap_functions(struct dd_function_table
*functions
)
674 functions
->Bitmap
= st_Bitmap
;
678 /** Per-context tear-down */
680 st_destroy_bitmap(struct st_context
*st
)
682 struct pipe_context
*pipe
= st
->pipe
;
683 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
686 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
687 st
->bitmap
.vs
= NULL
;
691 if (cache
->trans
&& cache
->buffer
) {
692 pipe_transfer_unmap(pipe
, cache
->trans
);
694 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
695 free(st
->bitmap
.cache
);
696 st
->bitmap
.cache
= NULL
;