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/dlist.h"
37 #include "main/macros.h"
39 #include "program/program.h"
40 #include "program/prog_print.h"
42 #include "st_context.h"
44 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_cb_bitmap.h"
48 #include "st_texture.h"
50 #include "pipe/p_context.h"
51 #include "pipe/p_defines.h"
52 #include "pipe/p_shader_tokens.h"
53 #include "util/u_inlines.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"
61 * glBitmaps are drawn as textured quads. The user's bitmap pattern
62 * is stored in a texture image. An alpha8 texture format is used.
63 * The fragment shader samples a bit (texel) from the texture, then
64 * discards the fragment if the bit is off.
66 * Note that we actually store the inverse image of the bitmap to
67 * simplify the fragment program. An "on" bit gets stored as texel=0x0
68 * and an "off" bit is stored as texel=0xff. Then we kill the
69 * fragment if the negated texel value is less than zero.
74 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
75 * buffer which is then rendered en mass upon a flush, state change, etc.
76 * A wide, short buffer is used to target the common case of a series
77 * of glBitmap calls being used to draw text.
79 static GLboolean UseBitmapCache
= GL_TRUE
;
82 #define BITMAP_CACHE_WIDTH 512
83 #define BITMAP_CACHE_HEIGHT 32
87 /** Window pos to render the cached image */
89 /** Bounds of region used in window coords */
90 GLint xmin
, ymin
, xmax
, ymax
;
94 /** Bitmap's Z position */
97 struct pipe_resource
*texture
;
98 struct pipe_transfer
*trans
;
102 /** An I8 texture image: */
107 /** Epsilon for Z comparisons */
108 #define Z_EPSILON 1e-06
112 * Copy user-provide bitmap bits into texture buffer, expanding
114 * "On" bits will set texels to 0x0.
115 * "Off" bits will not modify texels.
116 * Note that the image is actually going to be upside down in
117 * the texture. We deal with that with texcoords.
120 unpack_bitmap(struct st_context
*st
,
121 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
122 const struct gl_pixelstore_attrib
*unpack
,
123 const GLubyte
*bitmap
,
124 ubyte
*destBuffer
, uint destStride
)
126 destBuffer
+= py
* destStride
+ px
;
128 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
129 destBuffer
, destStride
, 0x0);
134 * Create a texture which represents a bitmap image.
136 static struct pipe_resource
*
137 make_bitmap_texture(struct gl_context
*ctx
, GLsizei width
, GLsizei height
,
138 const struct gl_pixelstore_attrib
*unpack
,
139 const GLubyte
*bitmap
)
141 struct st_context
*st
= st_context(ctx
);
142 struct pipe_context
*pipe
= st
->pipe
;
143 struct pipe_transfer
*transfer
;
145 struct pipe_resource
*pt
;
148 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
154 * Create texture to hold bitmap pattern.
156 pt
= st_texture_create(st
, st
->internal_target
, st
->bitmap
.tex_format
,
157 0, width
, height
, 1, 1, 0,
158 PIPE_BIND_SAMPLER_VIEW
);
160 _mesa_unmap_pbo_source(ctx
, unpack
);
164 dest
= pipe_transfer_map(st
->pipe
, pt
, 0, 0,
166 0, 0, width
, height
, &transfer
);
168 /* Put image into texture transfer */
169 memset(dest
, 0xff, height
* transfer
->stride
);
170 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
171 dest
, transfer
->stride
);
173 _mesa_unmap_pbo_source(ctx
, unpack
);
175 /* Release transfer */
176 pipe_transfer_unmap(pipe
, transfer
);
182 * Setup pipeline state prior to rendering the bitmap textured quad.
185 setup_render_state(struct gl_context
*ctx
,
186 struct pipe_sampler_view
*sv
,
187 const GLfloat
*color
,
190 struct st_context
*st
= st_context(ctx
);
191 struct cso_context
*cso
= st
->cso_context
;
192 struct st_fp_variant
*fpv
;
193 struct st_fp_variant_key key
;
195 memset(&key
, 0, sizeof(key
));
196 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
197 key
.bitmap
= GL_TRUE
;
198 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
199 ctx
->Color
._ClampFragmentColor
;
201 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
203 /* As an optimization, Mesa's fragment programs will sometimes get the
204 * primary color from a statevar/constant rather than a varying variable.
205 * when that's the case, we need to ensure that we use the 'color'
206 * parameter and not the current attribute color (which may have changed
207 * through glRasterPos and state validation.
208 * So, we force the proper color here. Not elegant, but it works.
211 GLfloat colorSave
[4];
212 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
213 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
214 st_upload_constants(st
, st
->fp
->Base
.Base
.Parameters
,
215 MESA_SHADER_FRAGMENT
);
216 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
219 cso_save_state(cso
, (CSO_BIT_RASTERIZER
|
220 CSO_BIT_FRAGMENT_SAMPLERS
|
221 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
223 CSO_BIT_STREAM_OUTPUTS
|
224 CSO_BIT_VERTEX_ELEMENTS
|
225 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
226 CSO_BITS_ALL_SHADERS
));
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
];
254 samplers
[fpv
->bitmap_sampler
] = &st
->bitmap
.atlas_sampler
;
256 samplers
[fpv
->bitmap_sampler
] = &st
->bitmap
.sampler
;
257 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, num
,
258 (const struct pipe_sampler_state
**) samplers
);
261 /* user textures, plus the bitmap texture */
263 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
264 uint num
= MAX2(fpv
->bitmap_sampler
+ 1,
265 st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
266 memcpy(sampler_views
, st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
267 sizeof(sampler_views
));
268 sampler_views
[fpv
->bitmap_sampler
] = sv
;
269 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, num
, sampler_views
);
272 /* viewport state: viewport matching window dims */
273 cso_set_viewport_dims(cso
, st
->state
.framebuffer
.width
,
274 st
->state
.framebuffer
.height
,
275 st
->state
.fb_orientation
== Y_0_TOP
);
277 cso_set_vertex_elements(cso
, 3, st
->util_velems
);
279 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
284 * Restore pipeline state after rendering the bitmap textured quad.
287 restore_render_state(struct gl_context
*ctx
)
289 struct st_context
*st
= st_context(ctx
);
290 struct cso_context
*cso
= st
->cso_context
;
292 cso_restore_state(cso
);
297 * Render a glBitmap by drawing a textured quad
300 draw_bitmap_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
301 GLsizei width
, GLsizei height
,
302 struct pipe_sampler_view
*sv
,
303 const GLfloat
*color
)
305 struct st_context
*st
= st_context(ctx
);
306 struct pipe_context
*pipe
= st
->pipe
;
307 const float fb_width
= (float) st
->state
.framebuffer
.width
;
308 const float fb_height
= (float) st
->state
.framebuffer
.height
;
309 const float x0
= (float) x
;
310 const float x1
= (float) (x
+ width
);
311 const float y0
= (float) y
;
312 const float y1
= (float) (y
+ height
);
313 float sLeft
= 0.0f
, sRight
= 1.0f
;
314 float tTop
= 0.0f
, tBot
= 1.0f
- tTop
;
315 const float clip_x0
= x0
/ fb_width
* 2.0f
- 1.0f
;
316 const float clip_y0
= y0
/ fb_height
* 2.0f
- 1.0f
;
317 const float clip_x1
= x1
/ fb_width
* 2.0f
- 1.0f
;
318 const float clip_y1
= y1
/ fb_height
* 2.0f
- 1.0f
;
322 /* XXX if the bitmap is larger than the max texture size, break
325 GLuint maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
326 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
327 assert(width
<= (GLsizei
) maxSize
);
328 assert(height
<= (GLsizei
) maxSize
);
331 setup_render_state(ctx
, sv
, color
, false);
333 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
336 if (sv
->texture
->target
== PIPE_TEXTURE_RECT
) {
337 /* use non-normalized texcoords */
338 sRight
= (float) width
;
339 tBot
= (float) height
;
342 if (!st_draw_quad(st
, clip_x0
, clip_y0
, clip_x1
, clip_y1
, z
,
343 sLeft
, tBot
, sRight
, tTop
, color
, 0)) {
344 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBitmap");
347 restore_render_state(ctx
);
349 /* We uploaded modified constants, need to invalidate them. */
350 st
->dirty
|= ST_NEW_FS_CONSTANTS
;
355 reset_cache(struct st_context
*st
)
357 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
359 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
360 cache
->empty
= GL_TRUE
;
362 cache
->xmin
= 1000000;
363 cache
->xmax
= -1000000;
364 cache
->ymin
= 1000000;
365 cache
->ymax
= -1000000;
367 assert(!cache
->texture
);
369 /* allocate a new texture */
370 cache
->texture
= st_texture_create(st
, st
->internal_target
,
371 st
->bitmap
.tex_format
, 0,
372 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
374 PIPE_BIND_SAMPLER_VIEW
);
378 /** Print bitmap image to stdout (debug) */
380 print_cache(const struct bitmap_cache
*cache
)
384 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
385 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
386 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
387 if (cache
->buffer
[k
])
399 * Create gallium pipe_transfer object for the bitmap cache.
402 create_cache_trans(struct st_context
*st
)
404 struct pipe_context
*pipe
= st
->pipe
;
405 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
410 /* Map the texture transfer.
411 * Subsequent glBitmap calls will write into the texture image.
413 cache
->buffer
= pipe_transfer_map(pipe
, cache
->texture
, 0, 0,
414 PIPE_TRANSFER_WRITE
, 0, 0,
416 BITMAP_CACHE_HEIGHT
, &cache
->trans
);
418 /* init image to all 0xff */
419 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
424 * If there's anything in the bitmap cache, draw/flush it now.
427 st_flush_bitmap_cache(struct st_context
*st
)
429 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
431 if (cache
&& !cache
->empty
) {
432 struct pipe_context
*pipe
= st
->pipe
;
433 struct pipe_sampler_view
*sv
;
435 assert(cache
->xmin
<= cache
->xmax
);
438 printf("flush bitmap, size %d x %d at %d, %d\n",
439 cache
->xmax
- cache
->xmin
,
440 cache
->ymax
- cache
->ymin
,
441 cache
->xpos
, cache
->ypos
);
443 /* The texture transfer has been mapped until now.
444 * So unmap and release the texture transfer before drawing.
446 if (cache
->trans
&& cache
->buffer
) {
449 pipe_transfer_unmap(pipe
, cache
->trans
);
450 cache
->buffer
= NULL
;
454 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
456 draw_bitmap_quad(st
->ctx
,
460 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
464 pipe_sampler_view_reference(&sv
, NULL
);
467 /* release/free the texture */
468 pipe_resource_reference(&cache
->texture
, NULL
);
476 * Try to accumulate this glBitmap call in the bitmap cache.
477 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
480 accum_bitmap(struct gl_context
*ctx
,
481 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
482 const struct gl_pixelstore_attrib
*unpack
,
483 const GLubyte
*bitmap
)
485 struct st_context
*st
= ctx
->st
;
486 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
487 int px
= -999, py
= -999;
488 const GLfloat z
= ctx
->Current
.RasterPos
[2];
490 if (width
> BITMAP_CACHE_WIDTH
||
491 height
> BITMAP_CACHE_HEIGHT
)
492 return GL_FALSE
; /* too big to cache */
495 px
= x
- cache
->xpos
; /* pos in buffer */
496 py
= y
- cache
->ypos
;
497 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
498 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
499 !TEST_EQ_4V(ctx
->Current
.RasterColor
, cache
->color
) ||
500 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
501 /* This bitmap would extend beyond cache bounds, or the bitmap
503 * so flush and continue.
505 st_flush_bitmap_cache(st
);
510 /* Initialize. Center bitmap vertically in the buffer. */
512 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
514 cache
->ypos
= y
- py
;
516 cache
->empty
= GL_FALSE
;
517 COPY_4FV(cache
->color
, ctx
->Current
.RasterColor
);
527 if (x
+ width
> cache
->xmax
)
528 cache
->xmax
= x
+ width
;
529 if (y
+ height
> cache
->ymax
)
530 cache
->ymax
= y
+ height
;
532 /* create the transfer if needed */
533 create_cache_trans(st
);
536 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
541 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
542 cache
->buffer
, BITMAP_CACHE_WIDTH
);
544 _mesa_unmap_pbo_source(ctx
, unpack
);
546 return GL_TRUE
; /* accumulated */
551 * One-time init for drawing bitmaps.
554 init_bitmap_state(struct st_context
*st
)
556 struct pipe_context
*pipe
= st
->pipe
;
557 struct pipe_screen
*screen
= pipe
->screen
;
559 /* This function should only be called once */
560 assert(st
->bitmap
.cache
== NULL
);
562 assert(st
->internal_target
== PIPE_TEXTURE_2D
||
563 st
->internal_target
== PIPE_TEXTURE_RECT
);
565 /* alloc bitmap cache object */
566 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
568 /* init sampler state once */
569 memset(&st
->bitmap
.sampler
, 0, sizeof(st
->bitmap
.sampler
));
570 st
->bitmap
.sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
571 st
->bitmap
.sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
572 st
->bitmap
.sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP
;
573 st
->bitmap
.sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
574 st
->bitmap
.sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
575 st
->bitmap
.sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
576 st
->bitmap
.sampler
.normalized_coords
= st
->internal_target
== PIPE_TEXTURE_2D
;
578 st
->bitmap
.atlas_sampler
= st
->bitmap
.sampler
;
579 st
->bitmap
.atlas_sampler
.normalized_coords
= 0;
581 /* init baseline rasterizer state once */
582 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
583 st
->bitmap
.rasterizer
.half_pixel_center
= 1;
584 st
->bitmap
.rasterizer
.bottom_edge_rule
= 1;
585 st
->bitmap
.rasterizer
.depth_clip
= 1;
587 /* find a usable texture format */
588 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
,
589 st
->internal_target
, 0,
590 PIPE_BIND_SAMPLER_VIEW
)) {
591 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
593 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
,
594 st
->internal_target
, 0,
595 PIPE_BIND_SAMPLER_VIEW
)) {
596 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
598 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
,
599 st
->internal_target
, 0,
600 PIPE_BIND_SAMPLER_VIEW
)) {
601 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
604 /* XXX support more formats */
608 /* Create the vertex shader */
610 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
612 st
->needs_texcoord_semantic
? TGSI_SEMANTIC_TEXCOORD
:
613 TGSI_SEMANTIC_GENERIC
};
614 const uint semantic_indexes
[] = { 0, 0, 0 };
615 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
626 * Called via ctx->Driver.Bitmap()
629 st_Bitmap(struct gl_context
*ctx
, GLint x
, GLint y
,
630 GLsizei width
, GLsizei height
,
631 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
633 struct st_context
*st
= st_context(ctx
);
634 struct pipe_resource
*pt
;
639 st_invalidate_readpix_cache(st
);
641 if (!st
->bitmap
.cache
) {
642 init_bitmap_state(st
);
645 /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
646 * for bitmap drawing uses no constants and the FS constants are
647 * explicitly uploaded in the draw_bitmap_quad() function.
649 if ((st
->dirty
| ctx
->NewDriverState
) & ~ST_NEW_CONSTANTS
&
650 ST_PIPELINE_RENDER_STATE_MASK
||
651 st
->gfx_shaders_may_be_dirty
) {
652 st_validate_state(st
, ST_PIPELINE_RENDER
);
655 if (UseBitmapCache
&& accum_bitmap(ctx
, x
, y
, width
, height
, unpack
, bitmap
))
658 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
660 struct pipe_sampler_view
*sv
=
661 st_create_texture_sampler_view(st
->pipe
, pt
);
663 assert(pt
->target
== PIPE_TEXTURE_2D
|| pt
->target
== PIPE_TEXTURE_RECT
);
666 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
667 width
, height
, sv
, ctx
->Current
.RasterColor
);
669 pipe_sampler_view_reference(&sv
, NULL
);
672 /* release/free the texture */
673 pipe_resource_reference(&pt
, NULL
);
679 * Called via ctx->Driver.DrawAtlasBitmap()
682 st_DrawAtlasBitmaps(struct gl_context
*ctx
,
683 const struct gl_bitmap_atlas
*atlas
,
684 GLuint count
, const GLubyte
*ids
)
686 struct st_context
*st
= st_context(ctx
);
687 struct pipe_context
*pipe
= st
->pipe
;
688 struct st_texture_object
*stObj
= st_texture_object(atlas
->texObj
);
689 struct pipe_sampler_view
*sv
;
690 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
691 const float z
= ctx
->Current
.RasterPos
[2] * 2.0f
- 1.0f
;
692 const float *color
= ctx
->Current
.RasterColor
;
693 const float clip_x_scale
= 2.0f
/ st
->state
.framebuffer
.width
;
694 const float clip_y_scale
= 2.0f
/ st
->state
.framebuffer
.height
;
695 const unsigned num_verts
= count
* 4;
696 const unsigned num_vert_bytes
= num_verts
* sizeof(struct st_util_vertex
);
697 struct st_util_vertex
*verts
;
698 struct pipe_vertex_buffer vb
= {0};
701 if (!st
->bitmap
.cache
) {
702 init_bitmap_state(st
);
705 st_flush_bitmap_cache(st
);
707 st_validate_state(st
, ST_PIPELINE_RENDER
);
708 st_invalidate_readpix_cache(st
);
710 sv
= st_create_texture_sampler_view(pipe
, stObj
->pt
);
712 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCallLists(bitmap text)");
716 setup_render_state(ctx
, sv
, color
, true);
718 vb
.stride
= sizeof(struct st_util_vertex
);
720 u_upload_alloc(st
->uploader
, 0, num_vert_bytes
, 4,
721 &vb
.buffer_offset
, &vb
.buffer
, (void **) &verts
);
723 /* build quads vertex data */
724 for (i
= 0; i
< count
; i
++) {
725 const GLfloat epsilon
= 0.0001F
;
726 const struct gl_bitmap_glyph
*g
= &atlas
->glyphs
[ids
[i
]];
727 const float xmove
= g
->xmove
, ymove
= g
->ymove
;
728 const float xorig
= g
->xorig
, yorig
= g
->yorig
;
729 const float s0
= g
->x
, t0
= g
->y
;
730 const float s1
= s0
+ g
->w
, t1
= t0
+ g
->h
;
731 const float x0
= IFLOOR(ctx
->Current
.RasterPos
[0] - xorig
+ epsilon
);
732 const float y0
= IFLOOR(ctx
->Current
.RasterPos
[1] - yorig
+ epsilon
);
733 const float x1
= x0
+ g
->w
, y1
= y0
+ g
->h
;
734 const float clip_x0
= x0
* clip_x_scale
- 1.0f
;
735 const float clip_y0
= y0
* clip_y_scale
- 1.0f
;
736 const float clip_x1
= x1
* clip_x_scale
- 1.0f
;
737 const float clip_y1
= y1
* clip_y_scale
- 1.0f
;
739 /* lower-left corner */
751 /* lower-right corner */
763 /* upper-right corner */
775 /* upper-left corner */
787 /* Update the raster position */
788 ctx
->Current
.RasterPos
[0] += xmove
;
789 ctx
->Current
.RasterPos
[1] += ymove
;
792 u_upload_unmap(st
->uploader
);
794 cso_set_vertex_buffers(st
->cso_context
,
795 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
798 cso_draw_arrays(st
->cso_context
, PIPE_PRIM_QUADS
, 0, num_verts
);
800 restore_render_state(ctx
);
802 pipe_resource_reference(&vb
.buffer
, NULL
);
804 pipe_sampler_view_reference(&sv
, NULL
);
806 /* We uploaded modified constants, need to invalidate them. */
807 st
->dirty
|= ST_NEW_FS_CONSTANTS
;
812 /** Per-context init */
814 st_init_bitmap_functions(struct dd_function_table
*functions
)
816 functions
->Bitmap
= st_Bitmap
;
817 functions
->DrawAtlasBitmaps
= st_DrawAtlasBitmaps
;
821 /** Per-context tear-down */
823 st_destroy_bitmap(struct st_context
*st
)
825 struct pipe_context
*pipe
= st
->pipe
;
826 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
829 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
830 st
->bitmap
.vs
= NULL
;
834 if (cache
->trans
&& cache
->buffer
) {
835 pipe_transfer_unmap(pipe
, cache
->trans
);
837 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
838 free(st
->bitmap
.cache
);
839 st
->bitmap
.cache
= NULL
;