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 "program/prog_instruction.h"
56 #include "cso_cache/cso_context.h"
62 * glBitmaps are drawn as textured quads. The user's bitmap pattern
63 * is stored in a texture image. An alpha8 texture format is used.
64 * The fragment shader samples a bit (texel) from the texture, then
65 * discards the fragment if the bit is off.
67 * Note that we actually store the inverse image of the bitmap to
68 * simplify the fragment program. An "on" bit gets stored as texel=0x0
69 * and an "off" bit is stored as texel=0xff. Then we kill the
70 * fragment if the negated texel value is less than zero.
75 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
76 * buffer which is then rendered en mass upon a flush, state change, etc.
77 * A wide, short buffer is used to target the common case of a series
78 * of glBitmap calls being used to draw text.
80 static GLboolean UseBitmapCache
= GL_TRUE
;
83 #define BITMAP_CACHE_WIDTH 512
84 #define BITMAP_CACHE_HEIGHT 32
88 /** Window pos to render the cached image */
90 /** Bounds of region used in window coords */
91 GLint xmin
, ymin
, xmax
, ymax
;
95 /** Bitmap's Z position */
98 struct pipe_resource
*texture
;
99 struct pipe_transfer
*trans
;
103 /** An I8 texture image: */
108 /** Epsilon for Z comparisons */
109 #define Z_EPSILON 1e-06
113 * Make fragment program for glBitmap:
114 * Sample the texture and kill the fragment if the bit is 0.
115 * This program will be combined with the user's fragment program.
117 static struct st_fragment_program
*
118 make_bitmap_fragment_program(struct gl_context
*ctx
, GLuint samplerIndex
)
120 struct st_context
*st
= st_context(ctx
);
121 struct st_fragment_program
*stfp
;
122 struct gl_program
*p
;
125 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
129 p
->NumInstructions
= 3;
131 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
132 if (!p
->Instructions
) {
133 ctx
->Driver
.DeleteProgram(ctx
, p
);
136 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
138 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
139 p
->Instructions
[ic
].Opcode
= OPCODE_TEX
;
140 p
->Instructions
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
141 p
->Instructions
[ic
].DstReg
.Index
= 0;
142 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
143 p
->Instructions
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
144 p
->Instructions
[ic
].TexSrcUnit
= samplerIndex
;
145 p
->Instructions
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
148 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
149 p
->Instructions
[ic
].Opcode
= OPCODE_KIL
;
150 p
->Instructions
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
152 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
153 p
->Instructions
[ic
].SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
155 p
->Instructions
[ic
].SrcReg
[0].Index
= 0;
156 p
->Instructions
[ic
].SrcReg
[0].Negate
= NEGATE_XYZW
;
160 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
162 assert(ic
== p
->NumInstructions
);
164 p
->InputsRead
= FRAG_BIT_TEX0
;
165 p
->OutputsWritten
= 0x0;
166 p
->SamplersUsed
= (1 << samplerIndex
);
168 stfp
= (struct st_fragment_program
*) p
;
169 stfp
->Base
.UsesKill
= GL_TRUE
;
175 static struct gl_program
*
176 make_bitmap_fragment_program_glsl(struct st_context
*st
,
177 struct st_fragment_program
*orig
,
180 struct gl_context
*ctx
= st
->ctx
;
181 struct st_fragment_program
*fp
= (struct st_fragment_program
*)
182 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
187 get_bitmap_visitor(fp
, orig
->glsl_to_tgsi
, samplerIndex
);
188 return &fp
->Base
.Base
;
193 find_free_bit(uint bitfield
)
196 for (i
= 0; i
< 32; i
++) {
197 if ((bitfield
& (1 << i
)) == 0) {
206 * Combine basic bitmap fragment program with the user-defined program.
207 * \param st current context
208 * \param fpIn the incoming fragment program
209 * \param fpOut the new fragment program which does fragment culling
210 * \param bitmap_sampler sampler number for the bitmap texture
213 st_make_bitmap_fragment_program(struct st_context
*st
,
214 struct gl_fragment_program
*fpIn
,
215 struct gl_fragment_program
**fpOut
,
216 GLuint
*bitmap_sampler
)
218 struct st_fragment_program
*bitmap_prog
;
219 struct st_fragment_program
*stfpIn
= (struct st_fragment_program
*) fpIn
;
220 struct gl_program
*newProg
;
224 * Generate new program which is the user-defined program prefixed
225 * with the bitmap sampler/kill instructions.
227 sampler
= find_free_bit(fpIn
->Base
.SamplersUsed
);
229 if (stfpIn
->glsl_to_tgsi
)
230 newProg
= make_bitmap_fragment_program_glsl(st
, stfpIn
, sampler
);
232 bitmap_prog
= make_bitmap_fragment_program(st
->ctx
, sampler
);
234 newProg
= _mesa_combine_programs(st
->ctx
,
235 &bitmap_prog
->Base
.Base
,
237 /* done with this after combining */
238 st_reference_fragprog(st
, &bitmap_prog
, NULL
);
243 printf("Combined bitmap program:\n");
244 _mesa_print_program(newProg
);
245 printf("InputsRead: 0x%x\n", newProg
->InputsRead
);
246 printf("OutputsWritten: 0x%x\n", newProg
->OutputsWritten
);
247 _mesa_print_parameter_list(newProg
->Parameters
);
252 *fpOut
= (struct gl_fragment_program
*) newProg
;
253 *bitmap_sampler
= sampler
;
258 * Copy user-provide bitmap bits into texture buffer, expanding
260 * "On" bits will set texels to 0x0.
261 * "Off" bits will not modify texels.
262 * Note that the image is actually going to be upside down in
263 * the texture. We deal with that with texcoords.
266 unpack_bitmap(struct st_context
*st
,
267 GLint px
, GLint py
, GLsizei width
, GLsizei height
,
268 const struct gl_pixelstore_attrib
*unpack
,
269 const GLubyte
*bitmap
,
270 ubyte
*destBuffer
, uint destStride
)
272 destBuffer
+= py
* destStride
+ px
;
274 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
,
275 destBuffer
, destStride
, 0x0);
280 * Create a texture which represents a bitmap image.
282 static struct pipe_resource
*
283 make_bitmap_texture(struct gl_context
*ctx
, GLsizei width
, GLsizei height
,
284 const struct gl_pixelstore_attrib
*unpack
,
285 const GLubyte
*bitmap
)
287 struct st_context
*st
= st_context(ctx
);
288 struct pipe_context
*pipe
= st
->pipe
;
289 struct pipe_transfer
*transfer
;
291 struct pipe_resource
*pt
;
294 bitmap
= _mesa_map_pbo_source(ctx
, unpack
, bitmap
);
300 * Create texture to hold bitmap pattern.
302 pt
= st_texture_create(st
, st
->internal_target
, st
->bitmap
.tex_format
,
303 0, width
, height
, 1, 1,
304 PIPE_BIND_SAMPLER_VIEW
);
306 _mesa_unmap_pbo_source(ctx
, unpack
);
310 transfer
= pipe_get_transfer(st
->pipe
, pt
, 0, 0,
312 0, 0, width
, height
);
314 dest
= pipe_transfer_map(pipe
, transfer
);
316 /* Put image into texture transfer */
317 memset(dest
, 0xff, height
* transfer
->stride
);
318 unpack_bitmap(st
, 0, 0, width
, height
, unpack
, bitmap
,
319 dest
, transfer
->stride
);
321 _mesa_unmap_pbo_source(ctx
, unpack
);
323 /* Release transfer */
324 pipe_transfer_unmap(pipe
, transfer
);
325 pipe
->transfer_destroy(pipe
, transfer
);
331 setup_bitmap_vertex_data(struct st_context
*st
, bool normalized
,
332 int x
, int y
, int width
, int height
,
333 float z
, const float color
[4])
335 struct pipe_context
*pipe
= st
->pipe
;
336 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
337 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
338 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
339 const GLfloat x0
= (GLfloat
)x
;
340 const GLfloat x1
= (GLfloat
)(x
+ width
);
341 const GLfloat y0
= (GLfloat
)y
;
342 const GLfloat y1
= (GLfloat
)(y
+ height
);
343 GLfloat sLeft
= (GLfloat
)0.0, sRight
= (GLfloat
)1.0;
344 GLfloat tTop
= (GLfloat
)0.0, tBot
= (GLfloat
)1.0 - tTop
;
345 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
346 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
347 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
348 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
349 const GLuint max_slots
= 1; /* 4096 / sizeof(st->bitmap.vertices); */
354 sRight
= (GLfloat
) width
;
355 tBot
= (GLfloat
) height
;
358 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
359 * no_flush) updates to buffers where we know there is no conflict
360 * with previous data. Currently using max_slots > 1 will cause
361 * synchronous rendering if the driver flushes its command buffers
362 * between one bitmap and the next. Our flush hook below isn't
363 * sufficient to catch this as the driver doesn't tell us when it
364 * flushes its own command buffers. Until this gets fixed, pay the
365 * price of allocating a new buffer for each bitmap cache-flush to
366 * avoid synchronous rendering.
368 if (st
->bitmap
.vbuf_slot
>= max_slots
) {
369 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
370 st
->bitmap
.vbuf_slot
= 0;
373 if (!st
->bitmap
.vbuf
) {
374 st
->bitmap
.vbuf
= pipe_buffer_create(pipe
->screen
,
375 PIPE_BIND_VERTEX_BUFFER
,
378 sizeof(st
->bitmap
.vertices
));
379 if (!st
->bitmap
.vbuf
) {
385 /* Positions are in clip coords since we need to do clipping in case
386 * the bitmap quad goes beyond the window bounds.
388 st
->bitmap
.vertices
[0][0][0] = clip_x0
;
389 st
->bitmap
.vertices
[0][0][1] = clip_y0
;
390 st
->bitmap
.vertices
[0][2][0] = sLeft
;
391 st
->bitmap
.vertices
[0][2][1] = tTop
;
393 st
->bitmap
.vertices
[1][0][0] = clip_x1
;
394 st
->bitmap
.vertices
[1][0][1] = clip_y0
;
395 st
->bitmap
.vertices
[1][2][0] = sRight
;
396 st
->bitmap
.vertices
[1][2][1] = tTop
;
398 st
->bitmap
.vertices
[2][0][0] = clip_x1
;
399 st
->bitmap
.vertices
[2][0][1] = clip_y1
;
400 st
->bitmap
.vertices
[2][2][0] = sRight
;
401 st
->bitmap
.vertices
[2][2][1] = tBot
;
403 st
->bitmap
.vertices
[3][0][0] = clip_x0
;
404 st
->bitmap
.vertices
[3][0][1] = clip_y1
;
405 st
->bitmap
.vertices
[3][2][0] = sLeft
;
406 st
->bitmap
.vertices
[3][2][1] = tBot
;
408 /* same for all verts: */
409 for (i
= 0; i
< 4; i
++) {
410 st
->bitmap
.vertices
[i
][0][2] = z
;
411 st
->bitmap
.vertices
[i
][0][3] = 1.0f
;
412 st
->bitmap
.vertices
[i
][1][0] = color
[0];
413 st
->bitmap
.vertices
[i
][1][1] = color
[1];
414 st
->bitmap
.vertices
[i
][1][2] = color
[2];
415 st
->bitmap
.vertices
[i
][1][3] = color
[3];
416 st
->bitmap
.vertices
[i
][2][2] = 0.0; /*R*/
417 st
->bitmap
.vertices
[i
][2][3] = 1.0; /*Q*/
420 /* put vertex data into vbuf */
421 pipe_buffer_write_nooverlap(st
->pipe
,
424 * sizeof(st
->bitmap
.vertices
),
425 sizeof st
->bitmap
.vertices
,
426 st
->bitmap
.vertices
);
428 return st
->bitmap
.vbuf_slot
++ * sizeof st
->bitmap
.vertices
;
434 * Render a glBitmap by drawing a textured quad
437 draw_bitmap_quad(struct gl_context
*ctx
, GLint x
, GLint y
, GLfloat z
,
438 GLsizei width
, GLsizei height
,
439 struct pipe_sampler_view
*sv
,
440 const GLfloat
*color
)
442 struct st_context
*st
= st_context(ctx
);
443 struct pipe_context
*pipe
= st
->pipe
;
444 struct cso_context
*cso
= st
->cso_context
;
445 struct st_fp_variant
*fpv
;
446 struct st_fp_variant_key key
;
450 memset(&key
, 0, sizeof(key
));
452 key
.bitmap
= GL_TRUE
;
454 fpv
= st_get_fp_variant(st
, st
->fp
, &key
);
456 /* As an optimization, Mesa's fragment programs will sometimes get the
457 * primary color from a statevar/constant rather than a varying variable.
458 * when that's the case, we need to ensure that we use the 'color'
459 * parameter and not the current attribute color (which may have changed
460 * through glRasterPos and state validation.
461 * So, we force the proper color here. Not elegant, but it works.
464 GLfloat colorSave
[4];
465 COPY_4V(colorSave
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
466 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], color
);
467 st_upload_constants(st
, fpv
->parameters
, PIPE_SHADER_FRAGMENT
);
468 COPY_4V(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], colorSave
);
473 /* XXX if the bitmap is larger than the max texture size, break
476 maxSize
= 1 << (pipe
->screen
->get_param(pipe
->screen
,
477 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
) - 1);
478 assert(width
<= (GLsizei
)maxSize
);
479 assert(height
<= (GLsizei
)maxSize
);
481 cso_save_rasterizer(cso
);
482 cso_save_samplers(cso
);
483 cso_save_fragment_sampler_views(cso
);
484 cso_save_viewport(cso
);
485 cso_save_fragment_shader(cso
);
486 cso_save_vertex_shader(cso
);
487 cso_save_geometry_shader(cso
);
488 cso_save_vertex_elements(cso
);
489 cso_save_vertex_buffers(cso
);
491 /* rasterizer state: just scissor */
492 st
->bitmap
.rasterizer
.scissor
= ctx
->Scissor
.Enabled
;
493 cso_set_rasterizer(cso
, &st
->bitmap
.rasterizer
);
495 /* fragment shader state: TEX lookup program */
496 cso_set_fragment_shader_handle(cso
, fpv
->driver_shader
);
498 /* vertex shader state: position + texcoord pass-through */
499 cso_set_vertex_shader_handle(cso
, st
->bitmap
.vs
);
501 /* geometry shader state: disabled */
502 cso_set_geometry_shader_handle(cso
, NULL
);
504 /* user samplers, plus our bitmap sampler */
506 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
507 uint num
= MAX2(fpv
->bitmap_sampler
+ 1, st
->state
.num_samplers
);
509 for (i
= 0; i
< st
->state
.num_samplers
; i
++) {
510 samplers
[i
] = &st
->state
.samplers
[i
];
512 samplers
[fpv
->bitmap_sampler
] =
513 &st
->bitmap
.samplers
[sv
->texture
->target
!= PIPE_TEXTURE_RECT
];
514 cso_set_samplers(cso
, num
, (const struct pipe_sampler_state
**) samplers
);
517 /* user textures, plus the bitmap texture */
519 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SAMPLERS
];
520 uint num
= MAX2(fpv
->bitmap_sampler
+ 1, st
->state
.num_textures
);
521 memcpy(sampler_views
, st
->state
.sampler_views
, sizeof(sampler_views
));
522 sampler_views
[fpv
->bitmap_sampler
] = sv
;
523 cso_set_fragment_sampler_views(cso
, num
, sampler_views
);
526 /* viewport state: viewport matching window dims */
528 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
529 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
530 const GLfloat width
= (GLfloat
)fb
->Width
;
531 const GLfloat height
= (GLfloat
)fb
->Height
;
532 struct pipe_viewport_state vp
;
533 vp
.scale
[0] = 0.5f
* width
;
534 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
537 vp
.translate
[0] = 0.5f
* width
;
538 vp
.translate
[1] = 0.5f
* height
;
539 vp
.translate
[2] = 0.5f
;
540 vp
.translate
[3] = 0.0f
;
541 cso_set_viewport(cso
, &vp
);
544 cso_set_vertex_elements(cso
, 3, st
->velems_util_draw
);
546 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
549 /* draw textured quad */
550 offset
= setup_bitmap_vertex_data(st
,
551 sv
->texture
->target
!= PIPE_TEXTURE_RECT
,
552 x
, y
, width
, height
, z
, color
);
554 if (st
->bitmap
.vbuf
) {
555 util_draw_vertex_buffer(pipe
, st
->cso_context
, st
->bitmap
.vbuf
, offset
,
556 PIPE_PRIM_TRIANGLE_FAN
,
558 3); /* attribs/vert */
562 cso_restore_rasterizer(cso
);
563 cso_restore_samplers(cso
);
564 cso_restore_fragment_sampler_views(cso
);
565 cso_restore_viewport(cso
);
566 cso_restore_fragment_shader(cso
);
567 cso_restore_vertex_shader(cso
);
568 cso_restore_geometry_shader(cso
);
569 cso_restore_vertex_elements(cso
);
570 cso_restore_vertex_buffers(cso
);
575 reset_cache(struct st_context
*st
)
577 struct pipe_context
*pipe
= st
->pipe
;
578 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
580 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
581 cache
->empty
= GL_TRUE
;
583 cache
->xmin
= 1000000;
584 cache
->xmax
= -1000000;
585 cache
->ymin
= 1000000;
586 cache
->ymax
= -1000000;
589 pipe
->transfer_destroy(pipe
, cache
->trans
);
593 assert(!cache
->texture
);
595 /* allocate a new texture */
596 cache
->texture
= st_texture_create(st
, PIPE_TEXTURE_2D
,
597 st
->bitmap
.tex_format
, 0,
598 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
600 PIPE_BIND_SAMPLER_VIEW
);
604 /** Print bitmap image to stdout (debug) */
606 print_cache(const struct bitmap_cache
*cache
)
610 for (i
= 0; i
< BITMAP_CACHE_HEIGHT
; i
++) {
611 k
= BITMAP_CACHE_WIDTH
* (BITMAP_CACHE_HEIGHT
- i
- 1);
612 for (j
= 0; j
< BITMAP_CACHE_WIDTH
; j
++) {
613 if (cache
->buffer
[k
])
625 * Create gallium pipe_transfer object for the bitmap cache.
628 create_cache_trans(struct st_context
*st
)
630 struct pipe_context
*pipe
= st
->pipe
;
631 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
636 /* Map the texture transfer.
637 * Subsequent glBitmap calls will write into the texture image.
639 cache
->trans
= pipe_get_transfer(st
->pipe
, cache
->texture
, 0, 0,
640 PIPE_TRANSFER_WRITE
, 0, 0,
642 BITMAP_CACHE_HEIGHT
);
643 cache
->buffer
= pipe_transfer_map(pipe
, cache
->trans
);
645 /* init image to all 0xff */
646 memset(cache
->buffer
, 0xff, cache
->trans
->stride
* BITMAP_CACHE_HEIGHT
);
651 * If there's anything in the bitmap cache, draw/flush it now.
654 st_flush_bitmap_cache(struct st_context
*st
)
656 if (!st
->bitmap
.cache
->empty
) {
657 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
659 if (st
->ctx
->DrawBuffer
) {
660 struct pipe_context
*pipe
= st
->pipe
;
661 struct pipe_sampler_view
*sv
;
663 assert(cache
->xmin
<= cache
->xmax
);
665 /* printf("flush size %d x %d at %d, %d\n",
666 cache->xmax - cache->xmin,
667 cache->ymax - cache->ymin,
668 cache->xpos, cache->ypos);
671 /* The texture transfer has been mapped until now.
672 * So unmap and release the texture transfer before drawing.
677 pipe_transfer_unmap(pipe
, cache
->trans
);
678 cache
->buffer
= NULL
;
680 pipe
->transfer_destroy(pipe
, cache
->trans
);
684 sv
= st_create_texture_sampler_view(st
->pipe
, cache
->texture
);
686 draw_bitmap_quad(st
->ctx
,
690 BITMAP_CACHE_WIDTH
, BITMAP_CACHE_HEIGHT
,
694 pipe_sampler_view_reference(&sv
, NULL
);
698 /* release/free the texture */
699 pipe_resource_reference(&cache
->texture
, NULL
);
707 * Flush bitmap cache and release vertex buffer.
710 st_flush_bitmap( struct st_context
*st
)
712 st_flush_bitmap_cache(st
);
714 /* Release vertex buffer to avoid synchronous rendering if we were
715 * to map it in the next frame.
717 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
718 st
->bitmap
.vbuf_slot
= 0;
723 * Try to accumulate this glBitmap call in the bitmap cache.
724 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
727 accum_bitmap(struct st_context
*st
,
728 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
729 const struct gl_pixelstore_attrib
*unpack
,
730 const GLubyte
*bitmap
)
732 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
733 int px
= -999, py
= -999;
734 const GLfloat z
= st
->ctx
->Current
.RasterPos
[2];
736 if (width
> BITMAP_CACHE_WIDTH
||
737 height
> BITMAP_CACHE_HEIGHT
)
738 return GL_FALSE
; /* too big to cache */
741 px
= x
- cache
->xpos
; /* pos in buffer */
742 py
= y
- cache
->ypos
;
743 if (px
< 0 || px
+ width
> BITMAP_CACHE_WIDTH
||
744 py
< 0 || py
+ height
> BITMAP_CACHE_HEIGHT
||
745 !TEST_EQ_4V(st
->ctx
->Current
.RasterColor
, cache
->color
) ||
746 ((fabs(z
- cache
->zpos
) > Z_EPSILON
))) {
747 /* This bitmap would extend beyond cache bounds, or the bitmap
749 * so flush and continue.
751 st_flush_bitmap_cache(st
);
756 /* Initialize. Center bitmap vertically in the buffer. */
758 py
= (BITMAP_CACHE_HEIGHT
- height
) / 2;
760 cache
->ypos
= y
- py
;
762 cache
->empty
= GL_FALSE
;
763 COPY_4FV(cache
->color
, st
->ctx
->Current
.RasterColor
);
773 if (x
+ width
> cache
->xmax
)
774 cache
->xmax
= x
+ width
;
775 if (y
+ height
> cache
->ymax
)
776 cache
->ymax
= y
+ height
;
778 /* create the transfer if needed */
779 create_cache_trans(st
);
781 unpack_bitmap(st
, px
, py
, width
, height
, unpack
, bitmap
,
782 cache
->buffer
, BITMAP_CACHE_WIDTH
);
784 return GL_TRUE
; /* accumulated */
790 * Called via ctx->Driver.Bitmap()
793 st_Bitmap(struct gl_context
*ctx
, GLint x
, GLint y
,
794 GLsizei width
, GLsizei height
,
795 const struct gl_pixelstore_attrib
*unpack
, const GLubyte
*bitmap
)
797 struct st_context
*st
= st_context(ctx
);
798 struct pipe_resource
*pt
;
800 if (width
== 0 || height
== 0)
803 st_validate_state(st
);
805 if (!st
->bitmap
.vs
) {
806 /* create pass-through vertex shader now */
807 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
809 TGSI_SEMANTIC_GENERIC
};
810 const uint semantic_indexes
[] = { 0, 0, 0 };
811 st
->bitmap
.vs
= util_make_vertex_passthrough_shader(st
->pipe
, 3,
816 if (UseBitmapCache
&& accum_bitmap(st
, x
, y
, width
, height
, unpack
, bitmap
))
819 pt
= make_bitmap_texture(ctx
, width
, height
, unpack
, bitmap
);
821 struct pipe_sampler_view
*sv
=
822 st_create_texture_sampler_view(st
->pipe
, pt
);
824 assert(pt
->target
== PIPE_TEXTURE_2D
|| pt
->target
== PIPE_TEXTURE_RECT
);
827 draw_bitmap_quad(ctx
, x
, y
, ctx
->Current
.RasterPos
[2],
829 st
->ctx
->Current
.RasterColor
);
831 pipe_sampler_view_reference(&sv
, NULL
);
834 /* release/free the texture */
835 pipe_resource_reference(&pt
, NULL
);
840 /** Per-context init */
842 st_init_bitmap_functions(struct dd_function_table
*functions
)
844 functions
->Bitmap
= st_Bitmap
;
848 /** Per-context init */
850 st_init_bitmap(struct st_context
*st
)
852 struct pipe_sampler_state
*sampler
= &st
->bitmap
.samplers
[0];
853 struct pipe_context
*pipe
= st
->pipe
;
854 struct pipe_screen
*screen
= pipe
->screen
;
856 /* init sampler state once */
857 memset(sampler
, 0, sizeof(*sampler
));
858 sampler
->wrap_s
= PIPE_TEX_WRAP_CLAMP
;
859 sampler
->wrap_t
= PIPE_TEX_WRAP_CLAMP
;
860 sampler
->wrap_r
= PIPE_TEX_WRAP_CLAMP
;
861 sampler
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
862 sampler
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
863 sampler
->mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
864 st
->bitmap
.samplers
[1] = *sampler
;
865 st
->bitmap
.samplers
[1].normalized_coords
= 1;
867 /* init baseline rasterizer state once */
868 memset(&st
->bitmap
.rasterizer
, 0, sizeof(st
->bitmap
.rasterizer
));
869 st
->bitmap
.rasterizer
.gl_rasterization_rules
= 1;
871 /* find a usable texture format */
872 if (screen
->is_format_supported(screen
, PIPE_FORMAT_I8_UNORM
,
874 PIPE_BIND_SAMPLER_VIEW
)) {
875 st
->bitmap
.tex_format
= PIPE_FORMAT_I8_UNORM
;
877 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_A8_UNORM
,
879 PIPE_BIND_SAMPLER_VIEW
)) {
880 st
->bitmap
.tex_format
= PIPE_FORMAT_A8_UNORM
;
882 else if (screen
->is_format_supported(screen
, PIPE_FORMAT_L8_UNORM
,
884 PIPE_BIND_SAMPLER_VIEW
)) {
885 st
->bitmap
.tex_format
= PIPE_FORMAT_L8_UNORM
;
888 /* XXX support more formats */
892 /* alloc bitmap cache object */
893 st
->bitmap
.cache
= ST_CALLOC_STRUCT(bitmap_cache
);
899 /** Per-context tear-down */
901 st_destroy_bitmap(struct st_context
*st
)
903 struct pipe_context
*pipe
= st
->pipe
;
904 struct bitmap_cache
*cache
= st
->bitmap
.cache
;
907 cso_delete_vertex_shader(st
->cso_context
, st
->bitmap
.vs
);
908 st
->bitmap
.vs
= NULL
;
911 if (st
->bitmap
.vbuf
) {
912 pipe_resource_reference(&st
->bitmap
.vbuf
, NULL
);
913 st
->bitmap
.vbuf
= NULL
;
918 pipe_transfer_unmap(pipe
, cache
->trans
);
919 pipe
->transfer_destroy(pipe
, cache
->trans
);
921 pipe_resource_reference(&st
->bitmap
.cache
->texture
, NULL
);
922 free(st
->bitmap
.cache
);
923 st
->bitmap
.cache
= NULL
;
927 #endif /* FEATURE_drawpix */