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 "program/program.h"
38 #include "program/prog_print.h"
40 #include "st_context.h"
42 #include "st_atom_constbuf.h"
43 #include "st_program.h"
44 #include "st_cb_bitmap.h"
45 #include "st_texture.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49 #include "pipe/p_shader_tokens.h"
50 #include "util/u_inlines.h"
51 #include "util/u_draw_quad.h"
52 #include "util/u_simple_shaders.h"
53 #include "program/prog_instruction.h"
54 #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(GLcontext
*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
= FRAG_ATTRIB_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
= FRAG_BIT_TEX0
;
163 p
->OutputsWritten
= 0x0;
164 p
->SamplersUsed
= (1 << samplerIndex
);
166 stfp
= (struct st_fragment_program
*) p
;
167 stfp
->Base
.UsesKill
= GL_TRUE
;
174 find_free_bit(uint bitfield
)
177 for (i
= 0; i
< 32; i
++) {
178 if ((bitfield
& (1 << i
)) == 0) {
187 * Combine basic bitmap fragment program with the user-defined program.
189 static struct st_fragment_program
*
190 combined_bitmap_fragment_program(GLcontext
*ctx
)
192 struct st_context
*st
= st_context(ctx
);
193 struct st_fragment_program
*stfp
= st
->fp
;
195 if (!stfp
->bitmap_program
) {
197 * Generate new program which is the user-defined program prefixed
198 * with the bitmap sampler/kill instructions.
200 struct st_fragment_program
*bitmap_prog
;
203 sampler
= find_free_bit(st
->fp
->Base
.Base
.SamplersUsed
);
204 bitmap_prog
= make_bitmap_fragment_program(ctx
, sampler
);
206 stfp
->bitmap_program
= (struct st_fragment_program
*)
207 _mesa_combine_programs(ctx
,
208 &bitmap_prog
->Base
.Base
, &stfp
->Base
.Base
);
209 stfp
->bitmap_program
->bitmap_sampler
= sampler
;
211 /* done with this after combining */
212 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
216 struct gl_program
*p
= &stfp
->bitmap_program
->Base
.Base
;
217 printf("Combined bitmap program:\n");
218 _mesa_print_program(p
);
219 printf("InputsRead: 0x%x\n", p
->InputsRead
);
220 printf("OutputsWritten: 0x%x\n", p
->OutputsWritten
);
221 _mesa_print_parameter_list(p
->Parameters
);
225 /* translate to TGSI tokens */
226 st_translate_fragment_program(st
, stfp
->bitmap_program
);
229 return stfp
->bitmap_program
;
234 * Copy user-provide bitmap bits into texture buffer, expanding
236 * "On" bits will set texels to 0x0.
237 * "Off" bits will not modify texels.
238 * Note that the image is actually going to be upside down in
239 * the texture. We deal with that with texcoords.
242 unpack_bitmap(struct st_context
*st
,
243 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
244 const struct gl_pixelstore_attrib
*unpack
,
245 const GLubyte
*bitmap
,
246 ubyte
*destBuffer
, uint destStride
)
248 destBuffer
+= py
* destStride
+ px
;
250 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
251 destBuffer
, destStride
, 0x0);
256 * Create a texture which represents a bitmap image.
258 static struct pipe_resource
*
259 make_bitmap_texture(GLcontext
*ctx
, GLsizei width
, GLsizei height
,
260 const struct gl_pixelstore_attrib
*unpack
,
261 const GLubyte
*bitmap
)
263 struct st_context
*st
= st_context(ctx
);
264 struct pipe_context
*pipe
= st
->pipe
;
265 struct pipe_transfer
*transfer
;
267 struct pipe_resource
*pt
;
270 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
276 * Create texture to hold bitmap pattern.
278 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, st
->bitmap
.tex_format
,
280 PIPE_BIND_SAMPLER_VIEW
);
282 _mesa_unmap_pbo_source(ctx
, unpack
);
286 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0, 0,
288 0, 0, width
, height
);
290 dest
= pipe_transfer_map(pipe
, transfer
);
292 /* Put image into texture transfer */
293 memset(dest
, 0xff, height
* transfer
->stride
);
294 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
295 dest
, transfer
->stride
);
297 _mesa_unmap_pbo_source(ctx
, unpack
);
299 /* Release transfer */
300 pipe_transfer_unmap(pipe
, transfer
);
301 pipe
->transfer_destroy(pipe
, transfer
);
307 setup_bitmap_vertex_data(struct st_context
*st
,
308 int x
, int y
, int width
, int height
,
309 float z
, const float color
[4])
311 struct pipe_context
*pipe
= st
->pipe
;
312 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
313 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
314 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
315 const GLfloat x0
= (GLfloat
)x
;
316 const GLfloat x1
= (GLfloat
)(x
+ width
);
317 const GLfloat y0
= (GLfloat
)y
;
318 const GLfloat y1
= (GLfloat
)(y
+ height
);
319 const GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
320 const GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
321 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
322 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
323 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
324 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
326 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
327 * no_flush) updates to buffers where we know there is no conflict
328 * with previous data. Currently using max_slots > 1 will cause
329 * synchronous rendering if the driver flushes its command buffers
330 * between one bitmap and the next. Our flush hook below isn't
331 * sufficient to catch this as the driver doesn't tell us when it
332 * flushes its own command buffers. Until this gets fixed, pay the
333 * price of allocating a new buffer for each bitmap cache-flush to
334 * avoid synchronous rendering.
336 const GLuint max_slots
= 1; /* 4096 / sizeof(st->bitmap.vertices); */
339 if (st
->bitmap
.vbuf_slot
>= max_slots
) {
340 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
341 st
->bitmap
.vbuf_slot
= 0;
344 if (!st
->bitmap
.vbuf
) {
345 st
->bitmap
.vbuf
= pipe_buffer_create(pipe
->screen
,
346 PIPE_BIND_VERTEX_BUFFER
,
347 max_slots
* sizeof(st
->bitmap
.vertices
));
350 /* Positions are in clip coords since we need to do clipping in case
351 * the bitmap quad goes beyond the window bounds.
353 st
->bitmap
.vertices
[0][0][0] = clip_x0
;
354 st
->bitmap
.vertices
[0][0][1] = clip_y0
;
355 st
->bitmap
.vertices
[0][2][0] = sLeft
;
356 st
->bitmap
.vertices
[0][2][1] = tTop
;
358 st
->bitmap
.vertices
[1][0][0] = clip_x1
;
359 st
->bitmap
.vertices
[1][0][1] = clip_y0
;
360 st
->bitmap
.vertices
[1][2][0] = sRight
;
361 st
->bitmap
.vertices
[1][2][1] = tTop
;
363 st
->bitmap
.vertices
[2][0][0] = clip_x1
;
364 st
->bitmap
.vertices
[2][0][1] = clip_y1
;
365 st
->bitmap
.vertices
[2][2][0] = sRight
;
366 st
->bitmap
.vertices
[2][2][1] = tBot
;
368 st
->bitmap
.vertices
[3][0][0] = clip_x0
;
369 st
->bitmap
.vertices
[3][0][1] = clip_y1
;
370 st
->bitmap
.vertices
[3][2][0] = sLeft
;
371 st
->bitmap
.vertices
[3][2][1] = tBot
;
373 /* same for all verts: */
374 for (i
= 0; i
< 4; i
++) {
375 st
->bitmap
.vertices
[i
][0][2] = z
;
376 st
->bitmap
.vertices
[i
][0][3] = 1.0;
377 st
->bitmap
.vertices
[i
][1][0] = color
[0];
378 st
->bitmap
.vertices
[i
][1][1] = color
[1];
379 st
->bitmap
.vertices
[i
][1][2] = color
[2];
380 st
->bitmap
.vertices
[i
][1][3] = color
[3];
381 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
382 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
385 /* put vertex data into vbuf */
386 pipe_buffer_write_nooverlap(st
->pipe
,
388 st
->bitmap
.vbuf_slot
* sizeof st
->bitmap
.vertices
,
389 sizeof st
->bitmap
.vertices
,
390 st
->bitmap
.vertices
);
392 return st
->bitmap
.vbuf_slot
++ * sizeof st
->bitmap
.vertices
;
398 * Render a glBitmap by drawing a textured quad
401 draw_bitmap_quad(GLcontext
*ctx
, GLint x
, GLint y
, GLfloat z
,
402 GLsizei width
, GLsizei height
,
403 struct pipe_sampler_view
*sv
,
404 const GLfloat
*color
)
406 struct st_context
*st
= st_context(ctx
);
407 struct pipe_context
*pipe
= st
->pipe
;
408 struct cso_context
*cso
= st
->cso_context
;
409 struct st_fragment_program
*stfp
;
413 stfp
= combined_bitmap_fragment_program(ctx
);
415 /* As an optimization, Mesa's fragment programs will sometimes get the
416 * primary color from a statevar/constant rather than a varying variable.
417 * when that's the case, we need to ensure that we use the 'color'
418 * parameter and not the current attribute color (which may have changed
419 * through glRasterPos and state validation.
420 * So, we force the proper color here. Not elegant, but it works.
423 GLfloat colorSave
[4];
424 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
425 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
426 st_upload_constants(st
, stfp
->Base
.Base
.Parameters
, PIPE_SHADER_FRAGMENT
);
427 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
432 /* XXX if the bitmap is larger than the max texture size, break
435 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
436 assert(width
<= (GLsizei
)maxSize
);
437 assert(height
<= (GLsizei
)maxSize
);
439 cso_save_rasterizer(cso
);
440 cso_save_samplers(cso
);
441 cso_save_fragment_sampler_views(cso
);
442 cso_save_viewport(cso
);
443 cso_save_fragment_shader(cso
);
444 cso_save_vertex_shader(cso
);
445 cso_save_vertex_elements(cso
);
447 /* rasterizer state: just scissor */
448 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
449 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
451 /* fragment shader state: TEX lookup program */
452 cso_set_fragment_shader_handle(cso
, stfp
->driver_shader
);
454 /* vertex shader state: position + texcoord pass-through */
455 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
457 /* user samplers, plus our bitmap sampler */
459 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
460 uint num
= MAX2(stfp
->bitmap_sampler
+ 1, st
->state
.num_samplers
);
462 for (i
= 0; i
< st
->state
.num_samplers
; i
++) {
463 samplers
[i
] = &st
->state
.samplers
[i
];
465 samplers
[stfp
->bitmap_sampler
] = &st
->bitmap
.sampler
;
466 cso_set_samplers(cso
, num
, (const struct pipe_sampler_state
**) samplers
);
469 /* user textures, plus the bitmap texture */
471 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
472 uint num
= MAX2(stfp
->bitmap_sampler
+ 1, st
->state
.num_textures
);
473 memcpy(sampler_views
, st
->state
.sampler_views
, sizeof(sampler_views
));
474 sampler_views
[stfp
->bitmap_sampler
] = sv
;
475 cso_set_fragment_sampler_views(cso
, num
, sampler_views
);
478 /* viewport state: viewport matching window dims */
480 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
481 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
482 const GLfloat width
= (GLfloat
)fb
->Width
;
483 const GLfloat height
= (GLfloat
)fb
->Height
;
484 struct pipe_viewport_state vp
;
485 vp
.scale
[0] = 0.5f
* width
;
486 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
489 vp
.translate
[0] = 0.5f
* width
;
490 vp
.translate
[1] = 0.5f
* height
;
491 vp
.translate
[2] = 0.5f
;
492 vp
.translate
[3] = 0.0f
;
493 cso_set_viewport(cso
, &vp
);
496 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
498 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
501 /* draw textured quad */
502 offset
= setup_bitmap_vertex_data(st
, x
, y
, width
, height
, z
, color
);
504 util_draw_vertex_buffer(pipe
, st
->bitmap
.vbuf
, offset
,
505 PIPE_PRIM_TRIANGLE_FAN
,
507 3); /* attribs/vert */
511 cso_restore_rasterizer(cso
);
512 cso_restore_samplers(cso
);
513 cso_restore_fragment_sampler_views(cso
);
514 cso_restore_viewport(cso
);
515 cso_restore_fragment_shader(cso
);
516 cso_restore_vertex_shader(cso
);
517 cso_restore_vertex_elements(cso
);
522 reset_cache(struct st_context
*st
)
524 struct pipe_context
*pipe
= st
->pipe
;
525 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
527 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
528 cache
->empty
= GL_TRUE
;
530 cache
->xmin
= 1000000;
531 cache
->xmax
= -1000000;
532 cache
->ymin
= 1000000;
533 cache
->ymax
= -1000000;
536 pipe
->transfer_destroy(pipe
, cache
->trans
);
540 assert(!cache
->texture
);
542 /* allocate a new texture */
543 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
544 st
->bitmap
.tex_format
, 0,
545 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
547 PIPE_BIND_SAMPLER_VIEW
);
551 /** Print bitmap image to stdout (debug) */
553 print_cache(const struct bitmap_cache
*cache
)
557 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
558 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
559 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
560 if (cache
->buffer
[k
])
572 create_cache_trans(struct st_context
*st
)
574 struct pipe_context
*pipe
= st
->pipe
;
575 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
580 /* Map the texture transfer.
581 * Subsequent glBitmap calls will write into the texture image.
583 cache
->trans
= pipe_get_transfer(st
->pipe
, cache
->texture
, 0, 0, 0,
584 PIPE_TRANSFER_WRITE
, 0, 0,
586 BITMAP_CACHE_HEIGHT
);
587 cache
->buffer
= pipe_transfer_map(pipe
, cache
->trans
);
589 /* init image to all 0xff */
590 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
595 * If there's anything in the bitmap cache, draw/flush it now.
598 st_flush_bitmap_cache(struct st_context
*st
)
600 if (!st
->bitmap
.cache
->empty
) {
601 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
603 if (st
->ctx
->DrawBuffer
) {
604 struct pipe_context
*pipe
= st
->pipe
;
605 struct pipe_sampler_view
*sv
;
607 assert(cache
->xmin
<= cache
->xmax
);
609 /* printf("flush size %d x %d at %d, %d\n",
610 cache->xmax - cache->xmin,
611 cache->ymax - cache->ymin,
612 cache->xpos, cache->ypos);
615 /* The texture transfer has been mapped until now.
616 * So unmap and release the texture transfer before drawing.
621 pipe_transfer_unmap(pipe
, cache
->trans
);
622 cache
->buffer
= NULL
;
624 pipe
->transfer_destroy(pipe
, cache
->trans
);
628 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
630 draw_bitmap_quad(st
->ctx
,
634 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
638 pipe_sampler_view_reference(&sv
, NULL
);
642 /* release/free the texture */
643 pipe_resource_reference(&cache
->texture
, NULL
);
649 /* Flush bitmap cache and release vertex buffer.
652 st_flush_bitmap( struct st_context
*st
)
654 st_flush_bitmap_cache(st
);
656 /* Release vertex buffer to avoid synchronous rendering if we were
657 * to map it in the next frame.
659 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
660 st
->bitmap
.vbuf_slot
= 0;
665 * Try to accumulate this glBitmap call in the bitmap cache.
666 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
669 accum_bitmap(struct st_context
*st
,
670 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
671 const struct gl_pixelstore_attrib
*unpack
,
672 const GLubyte
*bitmap
)
674 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
675 int px
= -999, py
= -999;
676 const GLfloat z
= st
->ctx
->Current
.RasterPos
[2];
678 if (width
> BITMAP_CACHE_WIDTH
||
679 height
> BITMAP_CACHE_HEIGHT
)
680 return GL_FALSE
; /* too big to cache */
683 px
= x
- cache
->xpos
; /* pos in buffer */
684 py
= y
- cache
->ypos
;
685 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
686 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
687 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
) ||
688 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
689 /* This bitmap would extend beyond cache bounds, or the bitmap
691 * so flush and continue.
693 st_flush_bitmap_cache(st
);
698 /* Initialize. Center bitmap vertically in the buffer. */
700 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
702 cache
->ypos
= y
- py
;
704 cache
->empty
= GL_FALSE
;
705 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
715 if (x
+ width
> cache
->xmax
)
716 cache
->xmax
= x
+ width
;
717 if (y
+ height
> cache
->ymax
)
718 cache
->ymax
= y
+ height
;
720 /* create the transfer if needed */
721 create_cache_trans(st
);
723 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
724 cache
->buffer
, BITMAP_CACHE_WIDTH
);
726 return GL_TRUE
; /* accumulated */
732 * Called via ctx->Driver.Bitmap()
735 st_Bitmap(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
736 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
738 struct st_context
*st
= st_context(ctx
);
739 struct pipe_resource
*pt
;
741 if (width
== 0 || height
== 0)
744 st_validate_state(st
);
746 if (!st
->bitmap
.vs
) {
747 /* create pass-through vertex shader now */
748 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
750 TGSI_SEMANTIC_GENERIC
};
751 const uint semantic_indexes
[] = { 0, 0, 0 };
752 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
757 if (UseBitmapCache
&& accum_bitmap(st
, x
, y
, width
, height
, unpack
, bitmap
))
760 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
762 struct pipe_sampler_view
*sv
= st_create_texture_sampler_view(st
->pipe
, pt
);
764 assert(pt
->target
== PIPE_TEXTURE_2D
|| pt
->target
== PIPE_TEXTURE_RECT
);
767 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
769 st
->ctx
->Current
.RasterColor
);
771 pipe_sampler_view_reference(&sv
, NULL
);
774 /* release/free the texture */
775 pipe_resource_reference(&pt
, NULL
);
780 /** Per-context init */
782 st_init_bitmap_functions(struct dd_function_table
*functions
)
784 functions
->Bitmap
= st_Bitmap
;
788 /** Per-context init */
790 st_init_bitmap(struct st_context
*st
)
792 struct pipe_sampler_state
*sampler
= &st
->bitmap
.sampler
;
793 struct pipe_context
*pipe
= st
->pipe
;
794 struct pipe_screen
*screen
= pipe
->screen
;
796 /* init sampler state once */
797 memset(sampler
, 0, sizeof(*sampler
));
798 sampler
->wrap_s
= PIPE_TEX_WRAP_CLAMP
;
799 sampler
->wrap_t
= PIPE_TEX_WRAP_CLAMP
;
800 sampler
->wrap_r
= PIPE_TEX_WRAP_CLAMP
;
801 sampler
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
802 sampler
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
803 sampler
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
804 sampler
->normalized_coords
= 1;
806 /* init baseline rasterizer state once */
807 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
808 st
->bitmap
.rasterizer
.gl_rasterization_rules
= 1;
810 /* find a usable texture format */
811 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
, PIPE_TEXTURE_2D
, 0,
812 PIPE_BIND_SAMPLER_VIEW
, 0)) {
813 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
815 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
, PIPE_TEXTURE_2D
, 0,
816 PIPE_BIND_SAMPLER_VIEW
, 0)) {
817 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
819 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
, PIPE_TEXTURE_2D
, 0,
820 PIPE_BIND_SAMPLER_VIEW
, 0)) {
821 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
824 /* XXX support more formats */
828 /* alloc bitmap cache object */
829 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
835 /** Per-context tear-down */
837 st_destroy_bitmap(struct st_context
*st
)
839 struct pipe_context
*pipe
= st
->pipe
;
840 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
845 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
846 st
->bitmap
.vs
= NULL
;
849 if (st
->bitmap
.vbuf
) {
850 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
851 st
->bitmap
.vbuf
= NULL
;
856 pipe_transfer_unmap(pipe
, cache
->trans
);
857 pipe
->transfer_destroy(pipe
, cache
->trans
);
859 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
860 free(st
->bitmap
.cache
);
861 st
->bitmap
.cache
= NULL
;
865 #endif /* FEATURE_drawpix */