1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci /*
29bf215546Sopenharmony_ci  * Authors:
30bf215546Sopenharmony_ci  *   Brian Paul
31bf215546Sopenharmony_ci  */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "main/errors.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "main/image.h"
36bf215546Sopenharmony_ci#include "main/bufferobj.h"
37bf215546Sopenharmony_ci#include "main/dlist.h"
38bf215546Sopenharmony_ci#include "main/framebuffer.h"
39bf215546Sopenharmony_ci#include "main/macros.h"
40bf215546Sopenharmony_ci#include "main/pbo.h"
41bf215546Sopenharmony_ci#include "program/program.h"
42bf215546Sopenharmony_ci#include "program/prog_print.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#include "st_context.h"
45bf215546Sopenharmony_ci#include "st_atom.h"
46bf215546Sopenharmony_ci#include "st_atom_constbuf.h"
47bf215546Sopenharmony_ci#include "st_draw.h"
48bf215546Sopenharmony_ci#include "st_program.h"
49bf215546Sopenharmony_ci#include "st_cb_bitmap.h"
50bf215546Sopenharmony_ci#include "st_cb_drawpixels.h"
51bf215546Sopenharmony_ci#include "st_sampler_view.h"
52bf215546Sopenharmony_ci#include "st_texture.h"
53bf215546Sopenharmony_ci#include "st_util.h"
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci#include "pipe/p_context.h"
56bf215546Sopenharmony_ci#include "pipe/p_defines.h"
57bf215546Sopenharmony_ci#include "pipe/p_shader_tokens.h"
58bf215546Sopenharmony_ci#include "util/u_inlines.h"
59bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
60bf215546Sopenharmony_ci#include "program/prog_instruction.h"
61bf215546Sopenharmony_ci#include "cso_cache/cso_context.h"
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci/**
65bf215546Sopenharmony_ci * glBitmaps are drawn as textured quads.  The user's bitmap pattern
66bf215546Sopenharmony_ci * is stored in a texture image.  An alpha8 texture format is used.
67bf215546Sopenharmony_ci * The fragment shader samples a bit (texel) from the texture, then
68bf215546Sopenharmony_ci * discards the fragment if the bit is off.
69bf215546Sopenharmony_ci *
70bf215546Sopenharmony_ci * Note that we actually store the inverse image of the bitmap to
71bf215546Sopenharmony_ci * simplify the fragment program.  An "on" bit gets stored as texel=0x0
72bf215546Sopenharmony_ci * and an "off" bit is stored as texel=0xff.  Then we kill the
73bf215546Sopenharmony_ci * fragment if the negated texel value is less than zero.
74bf215546Sopenharmony_ci */
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci/**
78bf215546Sopenharmony_ci * The bitmap cache attempts to accumulate multiple glBitmap calls in a
79bf215546Sopenharmony_ci * buffer which is then rendered en mass upon a flush, state change, etc.
80bf215546Sopenharmony_ci * A wide, short buffer is used to target the common case of a series
81bf215546Sopenharmony_ci * of glBitmap calls being used to draw text.
82bf215546Sopenharmony_ci */
83bf215546Sopenharmony_cistatic GLboolean UseBitmapCache = GL_TRUE;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci#define BITMAP_CACHE_WIDTH  512
87bf215546Sopenharmony_ci#define BITMAP_CACHE_HEIGHT 32
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci/** Epsilon for Z comparisons */
91bf215546Sopenharmony_ci#define Z_EPSILON 1e-06
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci/**
95bf215546Sopenharmony_ci * Copy user-provide bitmap bits into texture buffer, expanding
96bf215546Sopenharmony_ci * bits into texels.
97bf215546Sopenharmony_ci * "On" bits will set texels to 0x0.
98bf215546Sopenharmony_ci * "Off" bits will not modify texels.
99bf215546Sopenharmony_ci * Note that the image is actually going to be upside down in
100bf215546Sopenharmony_ci * the texture.  We deal with that with texcoords.
101bf215546Sopenharmony_ci */
102bf215546Sopenharmony_cistatic void
103bf215546Sopenharmony_ciunpack_bitmap(struct st_context *st,
104bf215546Sopenharmony_ci              GLint px, GLint py, GLsizei width, GLsizei height,
105bf215546Sopenharmony_ci              const struct gl_pixelstore_attrib *unpack,
106bf215546Sopenharmony_ci              const GLubyte *bitmap,
107bf215546Sopenharmony_ci              ubyte *destBuffer, uint destStride)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   destBuffer += py * destStride + px;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   _mesa_expand_bitmap(width, height, unpack, bitmap,
112bf215546Sopenharmony_ci                       destBuffer, destStride, 0x0);
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci/**
117bf215546Sopenharmony_ci * Create a texture which represents a bitmap image.
118bf215546Sopenharmony_ci */
119bf215546Sopenharmony_cistatic struct pipe_resource *
120bf215546Sopenharmony_cimake_bitmap_texture(struct gl_context *ctx, GLsizei width, GLsizei height,
121bf215546Sopenharmony_ci                    const struct gl_pixelstore_attrib *unpack,
122bf215546Sopenharmony_ci                    const GLubyte *bitmap)
123bf215546Sopenharmony_ci{
124bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
125bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
126bf215546Sopenharmony_ci   struct pipe_transfer *transfer;
127bf215546Sopenharmony_ci   ubyte *dest;
128bf215546Sopenharmony_ci   struct pipe_resource *pt;
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   /* PBO source... */
131bf215546Sopenharmony_ci   bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
132bf215546Sopenharmony_ci   if (!bitmap) {
133bf215546Sopenharmony_ci      return NULL;
134bf215546Sopenharmony_ci   }
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   /**
137bf215546Sopenharmony_ci    * Create texture to hold bitmap pattern.
138bf215546Sopenharmony_ci    */
139bf215546Sopenharmony_ci   pt = st_texture_create(st, st->internal_target, st->bitmap.tex_format,
140bf215546Sopenharmony_ci                          0, width, height, 1, 1, 0,
141bf215546Sopenharmony_ci                          PIPE_BIND_SAMPLER_VIEW, false);
142bf215546Sopenharmony_ci   if (!pt) {
143bf215546Sopenharmony_ci      _mesa_unmap_pbo_source(ctx, unpack);
144bf215546Sopenharmony_ci      return NULL;
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   dest = pipe_texture_map(st->pipe, pt, 0, 0,
148bf215546Sopenharmony_ci                            PIPE_MAP_WRITE,
149bf215546Sopenharmony_ci                            0, 0, width, height, &transfer);
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   /* Put image into texture transfer */
152bf215546Sopenharmony_ci   memset(dest, 0xff, height * transfer->stride);
153bf215546Sopenharmony_ci   unpack_bitmap(st, 0, 0, width, height, unpack, bitmap,
154bf215546Sopenharmony_ci                 dest, transfer->stride);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   _mesa_unmap_pbo_source(ctx, unpack);
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci   /* Release transfer */
159bf215546Sopenharmony_ci   pipe_texture_unmap(pipe, transfer);
160bf215546Sopenharmony_ci   return pt;
161bf215546Sopenharmony_ci}
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci/**
165bf215546Sopenharmony_ci * Setup pipeline state prior to rendering the bitmap textured quad.
166bf215546Sopenharmony_ci */
167bf215546Sopenharmony_cistatic void
168bf215546Sopenharmony_cisetup_render_state(struct gl_context *ctx,
169bf215546Sopenharmony_ci                   struct pipe_sampler_view *sv,
170bf215546Sopenharmony_ci                   const GLfloat *color,
171bf215546Sopenharmony_ci                   bool atlas)
172bf215546Sopenharmony_ci{
173bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
174bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
175bf215546Sopenharmony_ci   struct cso_context *cso = st->cso_context;
176bf215546Sopenharmony_ci   struct st_fp_variant *fpv;
177bf215546Sopenharmony_ci   struct st_fp_variant_key key;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   memset(&key, 0, sizeof(key));
180bf215546Sopenharmony_ci   key.st = st->has_shareable_shaders ? NULL : st;
181bf215546Sopenharmony_ci   key.bitmap = GL_TRUE;
182bf215546Sopenharmony_ci   key.clamp_color = st->clamp_frag_color_in_shader &&
183bf215546Sopenharmony_ci                     ctx->Color._ClampFragmentColor;
184bf215546Sopenharmony_ci   key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   fpv = st_get_fp_variant(st, st->fp, &key);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   /* As an optimization, Mesa's fragment programs will sometimes get the
189bf215546Sopenharmony_ci    * primary color from a statevar/constant rather than a varying variable.
190bf215546Sopenharmony_ci    * when that's the case, we need to ensure that we use the 'color'
191bf215546Sopenharmony_ci    * parameter and not the current attribute color (which may have changed
192bf215546Sopenharmony_ci    * through glRasterPos and state validation.
193bf215546Sopenharmony_ci    * So, we force the proper color here.  Not elegant, but it works.
194bf215546Sopenharmony_ci    */
195bf215546Sopenharmony_ci   {
196bf215546Sopenharmony_ci      GLfloat colorSave[4];
197bf215546Sopenharmony_ci      COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
198bf215546Sopenharmony_ci      COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
199bf215546Sopenharmony_ci      st_upload_constants(st, st->fp, MESA_SHADER_FRAGMENT);
200bf215546Sopenharmony_ci      COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
201bf215546Sopenharmony_ci   }
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci   cso_save_state(cso, (CSO_BIT_RASTERIZER |
204bf215546Sopenharmony_ci                        CSO_BIT_FRAGMENT_SAMPLERS |
205bf215546Sopenharmony_ci                        CSO_BIT_VIEWPORT |
206bf215546Sopenharmony_ci                        CSO_BIT_STREAM_OUTPUTS |
207bf215546Sopenharmony_ci                        CSO_BIT_VERTEX_ELEMENTS |
208bf215546Sopenharmony_ci                        CSO_BITS_ALL_SHADERS));
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   /* rasterizer state: just scissor */
212bf215546Sopenharmony_ci   st->bitmap.rasterizer.scissor = ctx->Scissor.EnableFlags & 1;
213bf215546Sopenharmony_ci   cso_set_rasterizer(cso, &st->bitmap.rasterizer);
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci   /* fragment shader state: TEX lookup program */
216bf215546Sopenharmony_ci   cso_set_fragment_shader_handle(cso, fpv->base.driver_shader);
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   /* vertex shader state: position + texcoord pass-through */
219bf215546Sopenharmony_ci   cso_set_vertex_shader_handle(cso, st->passthrough_vs);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci   /* disable other shaders */
222bf215546Sopenharmony_ci   cso_set_tessctrl_shader_handle(cso, NULL);
223bf215546Sopenharmony_ci   cso_set_tesseval_shader_handle(cso, NULL);
224bf215546Sopenharmony_ci   cso_set_geometry_shader_handle(cso, NULL);
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci   /* user samplers, plus our bitmap sampler */
227bf215546Sopenharmony_ci   {
228bf215546Sopenharmony_ci      struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
229bf215546Sopenharmony_ci      uint num = MAX2(fpv->bitmap_sampler + 1,
230bf215546Sopenharmony_ci                      st->state.num_frag_samplers);
231bf215546Sopenharmony_ci      uint i;
232bf215546Sopenharmony_ci      for (i = 0; i < st->state.num_frag_samplers; i++) {
233bf215546Sopenharmony_ci         samplers[i] = &st->state.frag_samplers[i];
234bf215546Sopenharmony_ci      }
235bf215546Sopenharmony_ci      if (atlas)
236bf215546Sopenharmony_ci         samplers[fpv->bitmap_sampler] = &st->bitmap.atlas_sampler;
237bf215546Sopenharmony_ci      else
238bf215546Sopenharmony_ci         samplers[fpv->bitmap_sampler] = &st->bitmap.sampler;
239bf215546Sopenharmony_ci      cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
240bf215546Sopenharmony_ci                       (const struct pipe_sampler_state **) samplers);
241bf215546Sopenharmony_ci   }
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   /* user textures, plus the bitmap texture */
244bf215546Sopenharmony_ci   {
245bf215546Sopenharmony_ci      struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
246bf215546Sopenharmony_ci      unsigned num_views =
247bf215546Sopenharmony_ci         st_get_sampler_views(st, PIPE_SHADER_FRAGMENT,
248bf215546Sopenharmony_ci                              ctx->FragmentProgram._Current, sampler_views);
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_ci      num_views = MAX2(fpv->bitmap_sampler + 1, num_views);
251bf215546Sopenharmony_ci      sampler_views[fpv->bitmap_sampler] = sv;
252bf215546Sopenharmony_ci      pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num_views, 0,
253bf215546Sopenharmony_ci                              true, sampler_views);
254bf215546Sopenharmony_ci      st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = num_views;
255bf215546Sopenharmony_ci   }
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci   /* viewport state: viewport matching window dims */
258bf215546Sopenharmony_ci   cso_set_viewport_dims(cso, st->state.fb_width,
259bf215546Sopenharmony_ci                         st->state.fb_height,
260bf215546Sopenharmony_ci                         st->state.fb_orientation == Y_0_TOP);
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   st->util_velems.count = 3;
263bf215546Sopenharmony_ci   cso_set_vertex_elements(cso, &st->util_velems);
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
266bf215546Sopenharmony_ci}
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci/**
270bf215546Sopenharmony_ci * Restore pipeline state after rendering the bitmap textured quad.
271bf215546Sopenharmony_ci */
272bf215546Sopenharmony_cistatic void
273bf215546Sopenharmony_cirestore_render_state(struct gl_context *ctx)
274bf215546Sopenharmony_ci{
275bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
276bf215546Sopenharmony_ci   struct cso_context *cso = st->cso_context;
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   /* Unbind all because st/mesa won't do it if the current shader doesn't
279bf215546Sopenharmony_ci    * use them.
280bf215546Sopenharmony_ci    */
281bf215546Sopenharmony_ci   cso_restore_state(cso, CSO_UNBIND_FS_SAMPLERVIEWS);
282bf215546Sopenharmony_ci   st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = 0;
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci   ctx->Array.NewVertexElements = true;
285bf215546Sopenharmony_ci   st->dirty |= ST_NEW_VERTEX_ARRAYS |
286bf215546Sopenharmony_ci                ST_NEW_FS_SAMPLER_VIEWS;
287bf215546Sopenharmony_ci}
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci/**
291bf215546Sopenharmony_ci * Render a glBitmap by drawing a textured quad
292bf215546Sopenharmony_ci */
293bf215546Sopenharmony_cistatic void
294bf215546Sopenharmony_cidraw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
295bf215546Sopenharmony_ci                 GLsizei width, GLsizei height,
296bf215546Sopenharmony_ci                 struct pipe_sampler_view *sv,
297bf215546Sopenharmony_ci                 const GLfloat *color)
298bf215546Sopenharmony_ci{
299bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
300bf215546Sopenharmony_ci   const float fb_width = (float) st->state.fb_width;
301bf215546Sopenharmony_ci   const float fb_height = (float) st->state.fb_height;
302bf215546Sopenharmony_ci   const float x0 = (float) x;
303bf215546Sopenharmony_ci   const float x1 = (float) (x + width);
304bf215546Sopenharmony_ci   const float y0 = (float) y;
305bf215546Sopenharmony_ci   const float y1 = (float) (y + height);
306bf215546Sopenharmony_ci   float sLeft = 0.0f, sRight = 1.0f;
307bf215546Sopenharmony_ci   float tTop = 0.0f, tBot = 1.0f - tTop;
308bf215546Sopenharmony_ci   const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
309bf215546Sopenharmony_ci   const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
310bf215546Sopenharmony_ci   const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
311bf215546Sopenharmony_ci   const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   /* limit checks */
314bf215546Sopenharmony_ci   {
315bf215546Sopenharmony_ci      /* XXX if the bitmap is larger than the max texture size, break
316bf215546Sopenharmony_ci       * it up into chunks.
317bf215546Sopenharmony_ci       */
318bf215546Sopenharmony_ci      ASSERTED GLuint maxSize =
319bf215546Sopenharmony_ci         st->screen->get_param(st->screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
320bf215546Sopenharmony_ci      assert(width <= (GLsizei) maxSize);
321bf215546Sopenharmony_ci      assert(height <= (GLsizei) maxSize);
322bf215546Sopenharmony_ci   }
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   setup_render_state(ctx, sv, color, false);
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_ci   /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
327bf215546Sopenharmony_ci   z = z * 2.0f - 1.0f;
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci   if (sv->texture->target == PIPE_TEXTURE_RECT) {
330bf215546Sopenharmony_ci      /* use non-normalized texcoords */
331bf215546Sopenharmony_ci      sRight = (float) width;
332bf215546Sopenharmony_ci      tBot = (float) height;
333bf215546Sopenharmony_ci   }
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci   if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
336bf215546Sopenharmony_ci                     sLeft, tBot, sRight, tTop, color, 0)) {
337bf215546Sopenharmony_ci      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBitmap");
338bf215546Sopenharmony_ci   }
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_ci   restore_render_state(ctx);
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   /* We uploaded modified constants, need to invalidate them. */
343bf215546Sopenharmony_ci   st->dirty |= ST_NEW_FS_CONSTANTS;
344bf215546Sopenharmony_ci}
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_cistatic void
348bf215546Sopenharmony_cireset_cache(struct st_context *st)
349bf215546Sopenharmony_ci{
350bf215546Sopenharmony_ci   struct st_bitmap_cache *cache = &st->bitmap.cache;
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci   /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
353bf215546Sopenharmony_ci   cache->empty = GL_TRUE;
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   cache->xmin = 1000000;
356bf215546Sopenharmony_ci   cache->xmax = -1000000;
357bf215546Sopenharmony_ci   cache->ymin = 1000000;
358bf215546Sopenharmony_ci   cache->ymax = -1000000;
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci   assert(!cache->texture);
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci   /* allocate a new texture */
363bf215546Sopenharmony_ci   cache->texture = st_texture_create(st, st->internal_target,
364bf215546Sopenharmony_ci                                      st->bitmap.tex_format, 0,
365bf215546Sopenharmony_ci                                      BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
366bf215546Sopenharmony_ci                                      1, 1, 0,
367bf215546Sopenharmony_ci                                      PIPE_BIND_SAMPLER_VIEW,
368bf215546Sopenharmony_ci                                      false);
369bf215546Sopenharmony_ci}
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci/** Print bitmap image to stdout (debug) */
373bf215546Sopenharmony_cistatic void
374bf215546Sopenharmony_ciprint_cache(const struct st_bitmap_cache *cache)
375bf215546Sopenharmony_ci{
376bf215546Sopenharmony_ci   int i, j, k;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci   for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
379bf215546Sopenharmony_ci      k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
380bf215546Sopenharmony_ci      for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
381bf215546Sopenharmony_ci         if (cache->buffer[k])
382bf215546Sopenharmony_ci            printf("X");
383bf215546Sopenharmony_ci         else
384bf215546Sopenharmony_ci            printf(" ");
385bf215546Sopenharmony_ci         k++;
386bf215546Sopenharmony_ci      }
387bf215546Sopenharmony_ci      printf("\n");
388bf215546Sopenharmony_ci   }
389bf215546Sopenharmony_ci}
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci
392bf215546Sopenharmony_ci/**
393bf215546Sopenharmony_ci * Create gallium pipe_transfer object for the bitmap cache.
394bf215546Sopenharmony_ci */
395bf215546Sopenharmony_cistatic void
396bf215546Sopenharmony_cicreate_cache_trans(struct st_context *st)
397bf215546Sopenharmony_ci{
398bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
399bf215546Sopenharmony_ci   struct st_bitmap_cache *cache = &st->bitmap.cache;
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci   if (cache->trans)
402bf215546Sopenharmony_ci      return;
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   /* Map the texture transfer.
405bf215546Sopenharmony_ci    * Subsequent glBitmap calls will write into the texture image.
406bf215546Sopenharmony_ci    */
407bf215546Sopenharmony_ci   cache->buffer = pipe_texture_map(pipe, cache->texture, 0, 0,
408bf215546Sopenharmony_ci                                     PIPE_MAP_WRITE, 0, 0,
409bf215546Sopenharmony_ci                                     BITMAP_CACHE_WIDTH,
410bf215546Sopenharmony_ci                                     BITMAP_CACHE_HEIGHT, &cache->trans);
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci   /* init image to all 0xff */
413bf215546Sopenharmony_ci   memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
414bf215546Sopenharmony_ci}
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_ci/**
418bf215546Sopenharmony_ci * If there's anything in the bitmap cache, draw/flush it now.
419bf215546Sopenharmony_ci */
420bf215546Sopenharmony_civoid
421bf215546Sopenharmony_cist_flush_bitmap_cache(struct st_context *st)
422bf215546Sopenharmony_ci{
423bf215546Sopenharmony_ci   struct st_bitmap_cache *cache = &st->bitmap.cache;
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci   if (!cache->empty) {
426bf215546Sopenharmony_ci      struct pipe_context *pipe = st->pipe;
427bf215546Sopenharmony_ci      struct pipe_sampler_view *sv;
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci      assert(cache->xmin <= cache->xmax);
430bf215546Sopenharmony_ci
431bf215546Sopenharmony_ci      if (0)
432bf215546Sopenharmony_ci         printf("flush bitmap, size %d x %d  at %d, %d\n",
433bf215546Sopenharmony_ci                cache->xmax - cache->xmin,
434bf215546Sopenharmony_ci                cache->ymax - cache->ymin,
435bf215546Sopenharmony_ci                cache->xpos, cache->ypos);
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci      /* The texture transfer has been mapped until now.
438bf215546Sopenharmony_ci       * So unmap and release the texture transfer before drawing.
439bf215546Sopenharmony_ci       */
440bf215546Sopenharmony_ci      if (cache->trans && cache->buffer) {
441bf215546Sopenharmony_ci         if (0)
442bf215546Sopenharmony_ci            print_cache(cache);
443bf215546Sopenharmony_ci         pipe_texture_unmap(pipe, cache->trans);
444bf215546Sopenharmony_ci         cache->buffer = NULL;
445bf215546Sopenharmony_ci         cache->trans = NULL;
446bf215546Sopenharmony_ci      }
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci      sv = st_create_texture_sampler_view(st->pipe, cache->texture);
449bf215546Sopenharmony_ci      if (sv) {
450bf215546Sopenharmony_ci         draw_bitmap_quad(st->ctx,
451bf215546Sopenharmony_ci                          cache->xpos,
452bf215546Sopenharmony_ci                          cache->ypos,
453bf215546Sopenharmony_ci                          cache->zpos,
454bf215546Sopenharmony_ci                          BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
455bf215546Sopenharmony_ci                          sv,
456bf215546Sopenharmony_ci                          cache->color);
457bf215546Sopenharmony_ci      }
458bf215546Sopenharmony_ci
459bf215546Sopenharmony_ci      /* release/free the texture */
460bf215546Sopenharmony_ci      pipe_resource_reference(&cache->texture, NULL);
461bf215546Sopenharmony_ci
462bf215546Sopenharmony_ci      reset_cache(st);
463bf215546Sopenharmony_ci   }
464bf215546Sopenharmony_ci}
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci/**
468bf215546Sopenharmony_ci * Try to accumulate this glBitmap call in the bitmap cache.
469bf215546Sopenharmony_ci * \return  GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
470bf215546Sopenharmony_ci */
471bf215546Sopenharmony_cistatic GLboolean
472bf215546Sopenharmony_ciaccum_bitmap(struct gl_context *ctx,
473bf215546Sopenharmony_ci             GLint x, GLint y, GLsizei width, GLsizei height,
474bf215546Sopenharmony_ci             const struct gl_pixelstore_attrib *unpack,
475bf215546Sopenharmony_ci             const GLubyte *bitmap )
476bf215546Sopenharmony_ci{
477bf215546Sopenharmony_ci   struct st_context *st = ctx->st;
478bf215546Sopenharmony_ci   struct st_bitmap_cache *cache = &st->bitmap.cache;
479bf215546Sopenharmony_ci   int px = -999, py = -999;
480bf215546Sopenharmony_ci   const GLfloat z = ctx->Current.RasterPos[2];
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_ci   if (width > BITMAP_CACHE_WIDTH ||
483bf215546Sopenharmony_ci       height > BITMAP_CACHE_HEIGHT)
484bf215546Sopenharmony_ci      return GL_FALSE; /* too big to cache */
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   if (!cache->empty) {
487bf215546Sopenharmony_ci      px = x - cache->xpos;  /* pos in buffer */
488bf215546Sopenharmony_ci      py = y - cache->ypos;
489bf215546Sopenharmony_ci      if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
490bf215546Sopenharmony_ci          py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
491bf215546Sopenharmony_ci          !TEST_EQ_4V(ctx->Current.RasterColor, cache->color) ||
492bf215546Sopenharmony_ci          ((fabsf(z - cache->zpos) > Z_EPSILON))) {
493bf215546Sopenharmony_ci         /* This bitmap would extend beyond cache bounds, or the bitmap
494bf215546Sopenharmony_ci          * color is changing
495bf215546Sopenharmony_ci          * so flush and continue.
496bf215546Sopenharmony_ci          */
497bf215546Sopenharmony_ci         st_flush_bitmap_cache(st);
498bf215546Sopenharmony_ci      }
499bf215546Sopenharmony_ci   }
500bf215546Sopenharmony_ci
501bf215546Sopenharmony_ci   if (cache->empty) {
502bf215546Sopenharmony_ci      /* Initialize.  Center bitmap vertically in the buffer. */
503bf215546Sopenharmony_ci      px = 0;
504bf215546Sopenharmony_ci      py = (BITMAP_CACHE_HEIGHT - height) / 2;
505bf215546Sopenharmony_ci      cache->xpos = x;
506bf215546Sopenharmony_ci      cache->ypos = y - py;
507bf215546Sopenharmony_ci      cache->zpos = z;
508bf215546Sopenharmony_ci      cache->empty = GL_FALSE;
509bf215546Sopenharmony_ci      COPY_4FV(cache->color, ctx->Current.RasterColor);
510bf215546Sopenharmony_ci   }
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   assert(px != -999);
513bf215546Sopenharmony_ci   assert(py != -999);
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_ci   if (x < cache->xmin)
516bf215546Sopenharmony_ci      cache->xmin = x;
517bf215546Sopenharmony_ci   if (y < cache->ymin)
518bf215546Sopenharmony_ci      cache->ymin = y;
519bf215546Sopenharmony_ci   if (x + width > cache->xmax)
520bf215546Sopenharmony_ci      cache->xmax = x + width;
521bf215546Sopenharmony_ci   if (y + height > cache->ymax)
522bf215546Sopenharmony_ci      cache->ymax = y + height;
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ci   /* create the transfer if needed */
525bf215546Sopenharmony_ci   create_cache_trans(st);
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci   /* PBO source... */
528bf215546Sopenharmony_ci   bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
529bf215546Sopenharmony_ci   if (!bitmap) {
530bf215546Sopenharmony_ci      return FALSE;
531bf215546Sopenharmony_ci   }
532bf215546Sopenharmony_ci
533bf215546Sopenharmony_ci   unpack_bitmap(st, px, py, width, height, unpack, bitmap,
534bf215546Sopenharmony_ci                 cache->buffer, BITMAP_CACHE_WIDTH);
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_ci   _mesa_unmap_pbo_source(ctx, unpack);
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci   return GL_TRUE; /* accumulated */
539bf215546Sopenharmony_ci}
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci
542bf215546Sopenharmony_ci/**
543bf215546Sopenharmony_ci * One-time init for drawing bitmaps.
544bf215546Sopenharmony_ci */
545bf215546Sopenharmony_cistatic void
546bf215546Sopenharmony_ciinit_bitmap_state(struct st_context *st)
547bf215546Sopenharmony_ci{
548bf215546Sopenharmony_ci   struct pipe_screen *screen = st->screen;
549bf215546Sopenharmony_ci
550bf215546Sopenharmony_ci   /* This function should only be called once */
551bf215546Sopenharmony_ci   assert(!st->bitmap.tex_format);
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_ci   assert(st->internal_target == PIPE_TEXTURE_2D ||
554bf215546Sopenharmony_ci          st->internal_target == PIPE_TEXTURE_RECT);
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci   /* init sampler state once */
557bf215546Sopenharmony_ci   memset(&st->bitmap.sampler, 0, sizeof(st->bitmap.sampler));
558bf215546Sopenharmony_ci   st->bitmap.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
559bf215546Sopenharmony_ci   st->bitmap.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
560bf215546Sopenharmony_ci   st->bitmap.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
561bf215546Sopenharmony_ci   st->bitmap.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
562bf215546Sopenharmony_ci   st->bitmap.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
563bf215546Sopenharmony_ci   st->bitmap.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
564bf215546Sopenharmony_ci   st->bitmap.sampler.normalized_coords = st->internal_target == PIPE_TEXTURE_2D ||
565bf215546Sopenharmony_ci                                          (st->internal_target == PIPE_TEXTURE_RECT && st->lower_rect_tex);
566bf215546Sopenharmony_ci
567bf215546Sopenharmony_ci   st->bitmap.atlas_sampler = st->bitmap.sampler;
568bf215546Sopenharmony_ci   st->bitmap.atlas_sampler.normalized_coords = 0;
569bf215546Sopenharmony_ci
570bf215546Sopenharmony_ci   /* init baseline rasterizer state once */
571bf215546Sopenharmony_ci   memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
572bf215546Sopenharmony_ci   st->bitmap.rasterizer.half_pixel_center = 1;
573bf215546Sopenharmony_ci   st->bitmap.rasterizer.bottom_edge_rule = 1;
574bf215546Sopenharmony_ci   st->bitmap.rasterizer.depth_clip_near = 1;
575bf215546Sopenharmony_ci   st->bitmap.rasterizer.depth_clip_far = 1;
576bf215546Sopenharmony_ci
577bf215546Sopenharmony_ci   /* find a usable texture format */
578bf215546Sopenharmony_ci   if (screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
579bf215546Sopenharmony_ci                                   st->internal_target, 0, 0,
580bf215546Sopenharmony_ci                                   PIPE_BIND_SAMPLER_VIEW)) {
581bf215546Sopenharmony_ci      st->bitmap.tex_format = PIPE_FORMAT_R8_UNORM;
582bf215546Sopenharmony_ci   }
583bf215546Sopenharmony_ci   else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
584bf215546Sopenharmony_ci                                        st->internal_target, 0, 0,
585bf215546Sopenharmony_ci                                        PIPE_BIND_SAMPLER_VIEW)) {
586bf215546Sopenharmony_ci      st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
587bf215546Sopenharmony_ci   }
588bf215546Sopenharmony_ci   else {
589bf215546Sopenharmony_ci      /* XXX support more formats */
590bf215546Sopenharmony_ci      assert(0);
591bf215546Sopenharmony_ci   }
592bf215546Sopenharmony_ci
593bf215546Sopenharmony_ci   /* Create the vertex shader */
594bf215546Sopenharmony_ci   st_make_passthrough_vertex_shader(st);
595bf215546Sopenharmony_ci
596bf215546Sopenharmony_ci   reset_cache(st);
597bf215546Sopenharmony_ci}
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_civoid
600bf215546Sopenharmony_cist_Bitmap(struct gl_context *ctx, GLint x, GLint y,
601bf215546Sopenharmony_ci          GLsizei width, GLsizei height,
602bf215546Sopenharmony_ci          const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap)
603bf215546Sopenharmony_ci{
604bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
605bf215546Sopenharmony_ci   struct pipe_resource *pt;
606bf215546Sopenharmony_ci
607bf215546Sopenharmony_ci   assert(width > 0);
608bf215546Sopenharmony_ci   assert(height > 0);
609bf215546Sopenharmony_ci
610bf215546Sopenharmony_ci   st_invalidate_readpix_cache(st);
611bf215546Sopenharmony_ci
612bf215546Sopenharmony_ci   if (!st->bitmap.tex_format) {
613bf215546Sopenharmony_ci      init_bitmap_state(st);
614bf215546Sopenharmony_ci   }
615bf215546Sopenharmony_ci
616bf215546Sopenharmony_ci   /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
617bf215546Sopenharmony_ci    * for bitmap drawing uses no constants and the FS constants are
618bf215546Sopenharmony_ci    * explicitly uploaded in the draw_bitmap_quad() function.
619bf215546Sopenharmony_ci    */
620bf215546Sopenharmony_ci   if ((st->dirty | ctx->NewDriverState) & st->active_states &
621bf215546Sopenharmony_ci       ~ST_NEW_CONSTANTS & ST_PIPELINE_RENDER_STATE_MASK ||
622bf215546Sopenharmony_ci       st->gfx_shaders_may_be_dirty) {
623bf215546Sopenharmony_ci      st_validate_state(st, ST_PIPELINE_META);
624bf215546Sopenharmony_ci   }
625bf215546Sopenharmony_ci
626bf215546Sopenharmony_ci   if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
627bf215546Sopenharmony_ci      return;
628bf215546Sopenharmony_ci
629bf215546Sopenharmony_ci   pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
630bf215546Sopenharmony_ci   if (pt) {
631bf215546Sopenharmony_ci      struct pipe_sampler_view *sv =
632bf215546Sopenharmony_ci         st_create_texture_sampler_view(st->pipe, pt);
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_ci      assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci      if (sv) {
637bf215546Sopenharmony_ci         draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
638bf215546Sopenharmony_ci                          width, height, sv, ctx->Current.RasterColor);
639bf215546Sopenharmony_ci      }
640bf215546Sopenharmony_ci
641bf215546Sopenharmony_ci      /* release/free the texture */
642bf215546Sopenharmony_ci      pipe_resource_reference(&pt, NULL);
643bf215546Sopenharmony_ci   }
644bf215546Sopenharmony_ci}
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_civoid
647bf215546Sopenharmony_cist_DrawAtlasBitmaps(struct gl_context *ctx,
648bf215546Sopenharmony_ci                    const struct gl_bitmap_atlas *atlas,
649bf215546Sopenharmony_ci                    GLuint count, const GLubyte *ids)
650bf215546Sopenharmony_ci{
651bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
652bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
653bf215546Sopenharmony_ci   struct gl_texture_object *stObj = atlas->texObj;
654bf215546Sopenharmony_ci   struct pipe_sampler_view *sv;
655bf215546Sopenharmony_ci   /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
656bf215546Sopenharmony_ci   const float z = ctx->Current.RasterPos[2] * 2.0f - 1.0f;
657bf215546Sopenharmony_ci   const float *color = ctx->Current.RasterColor;
658bf215546Sopenharmony_ci   const float clip_x_scale = 2.0f / st->state.fb_width;
659bf215546Sopenharmony_ci   const float clip_y_scale = 2.0f / st->state.fb_height;
660bf215546Sopenharmony_ci   const unsigned num_verts = count * 4;
661bf215546Sopenharmony_ci   const unsigned num_vert_bytes = num_verts * sizeof(struct st_util_vertex);
662bf215546Sopenharmony_ci   struct st_util_vertex *verts;
663bf215546Sopenharmony_ci   struct pipe_vertex_buffer vb = {0};
664bf215546Sopenharmony_ci   unsigned i;
665bf215546Sopenharmony_ci
666bf215546Sopenharmony_ci   if (!st->bitmap.tex_format) {
667bf215546Sopenharmony_ci      init_bitmap_state(st);
668bf215546Sopenharmony_ci   }
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_ci   st_flush_bitmap_cache(st);
671bf215546Sopenharmony_ci
672bf215546Sopenharmony_ci   st_validate_state(st, ST_PIPELINE_META);
673bf215546Sopenharmony_ci   st_invalidate_readpix_cache(st);
674bf215546Sopenharmony_ci
675bf215546Sopenharmony_ci   sv = st_create_texture_sampler_view(pipe, stObj->pt);
676bf215546Sopenharmony_ci   if (!sv) {
677bf215546Sopenharmony_ci      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCallLists(bitmap text)");
678bf215546Sopenharmony_ci      return;
679bf215546Sopenharmony_ci   }
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci   setup_render_state(ctx, sv, color, true);
682bf215546Sopenharmony_ci
683bf215546Sopenharmony_ci   vb.stride = sizeof(struct st_util_vertex);
684bf215546Sopenharmony_ci
685bf215546Sopenharmony_ci   u_upload_alloc(pipe->stream_uploader, 0, num_vert_bytes, 4,
686bf215546Sopenharmony_ci                  &vb.buffer_offset, &vb.buffer.resource, (void **) &verts);
687bf215546Sopenharmony_ci
688bf215546Sopenharmony_ci   if (unlikely(!verts)) {
689bf215546Sopenharmony_ci      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCallLists(bitmap text)");
690bf215546Sopenharmony_ci      goto out;
691bf215546Sopenharmony_ci   }
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci   /* build quads vertex data */
694bf215546Sopenharmony_ci   for (i = 0; i < count; i++) {
695bf215546Sopenharmony_ci      const GLfloat epsilon = 0.0001F;
696bf215546Sopenharmony_ci      const struct gl_bitmap_glyph *g = &atlas->glyphs[ids[i]];
697bf215546Sopenharmony_ci      const float xmove = g->xmove, ymove = g->ymove;
698bf215546Sopenharmony_ci      const float xorig = g->xorig, yorig = g->yorig;
699bf215546Sopenharmony_ci      const float s0 = g->x, t0 = g->y;
700bf215546Sopenharmony_ci      const float s1 = s0 + g->w, t1 = t0 + g->h;
701bf215546Sopenharmony_ci      const float x0 = util_ifloor(ctx->Current.RasterPos[0] - xorig + epsilon);
702bf215546Sopenharmony_ci      const float y0 = util_ifloor(ctx->Current.RasterPos[1] - yorig + epsilon);
703bf215546Sopenharmony_ci      const float x1 = x0 + g->w, y1 = y0 + g->h;
704bf215546Sopenharmony_ci      const float clip_x0 = x0 * clip_x_scale - 1.0f;
705bf215546Sopenharmony_ci      const float clip_y0 = y0 * clip_y_scale - 1.0f;
706bf215546Sopenharmony_ci      const float clip_x1 = x1 * clip_x_scale - 1.0f;
707bf215546Sopenharmony_ci      const float clip_y1 = y1 * clip_y_scale - 1.0f;
708bf215546Sopenharmony_ci
709bf215546Sopenharmony_ci      /* lower-left corner */
710bf215546Sopenharmony_ci      verts->x = clip_x0;
711bf215546Sopenharmony_ci      verts->y = clip_y0;
712bf215546Sopenharmony_ci      verts->z = z;
713bf215546Sopenharmony_ci      verts->r = color[0];
714bf215546Sopenharmony_ci      verts->g = color[1];
715bf215546Sopenharmony_ci      verts->b = color[2];
716bf215546Sopenharmony_ci      verts->a = color[3];
717bf215546Sopenharmony_ci      verts->s = s0;
718bf215546Sopenharmony_ci      verts->t = t0;
719bf215546Sopenharmony_ci      verts++;
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci      /* lower-right corner */
722bf215546Sopenharmony_ci      verts->x = clip_x1;
723bf215546Sopenharmony_ci      verts->y = clip_y0;
724bf215546Sopenharmony_ci      verts->z = z;
725bf215546Sopenharmony_ci      verts->r = color[0];
726bf215546Sopenharmony_ci      verts->g = color[1];
727bf215546Sopenharmony_ci      verts->b = color[2];
728bf215546Sopenharmony_ci      verts->a = color[3];
729bf215546Sopenharmony_ci      verts->s = s1;
730bf215546Sopenharmony_ci      verts->t = t0;
731bf215546Sopenharmony_ci      verts++;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci      /* upper-right corner */
734bf215546Sopenharmony_ci      verts->x = clip_x1;
735bf215546Sopenharmony_ci      verts->y = clip_y1;
736bf215546Sopenharmony_ci      verts->z = z;
737bf215546Sopenharmony_ci      verts->r = color[0];
738bf215546Sopenharmony_ci      verts->g = color[1];
739bf215546Sopenharmony_ci      verts->b = color[2];
740bf215546Sopenharmony_ci      verts->a = color[3];
741bf215546Sopenharmony_ci      verts->s = s1;
742bf215546Sopenharmony_ci      verts->t = t1;
743bf215546Sopenharmony_ci      verts++;
744bf215546Sopenharmony_ci
745bf215546Sopenharmony_ci      /* upper-left corner */
746bf215546Sopenharmony_ci      verts->x = clip_x0;
747bf215546Sopenharmony_ci      verts->y = clip_y1;
748bf215546Sopenharmony_ci      verts->z = z;
749bf215546Sopenharmony_ci      verts->r = color[0];
750bf215546Sopenharmony_ci      verts->g = color[1];
751bf215546Sopenharmony_ci      verts->b = color[2];
752bf215546Sopenharmony_ci      verts->a = color[3];
753bf215546Sopenharmony_ci      verts->s = s0;
754bf215546Sopenharmony_ci      verts->t = t1;
755bf215546Sopenharmony_ci      verts++;
756bf215546Sopenharmony_ci
757bf215546Sopenharmony_ci      /* Update the raster position */
758bf215546Sopenharmony_ci      ctx->Current.RasterPos[0] += xmove;
759bf215546Sopenharmony_ci      ctx->Current.RasterPos[1] += ymove;
760bf215546Sopenharmony_ci      ctx->PopAttribState |= GL_CURRENT_BIT;
761bf215546Sopenharmony_ci   }
762bf215546Sopenharmony_ci
763bf215546Sopenharmony_ci   u_upload_unmap(pipe->stream_uploader);
764bf215546Sopenharmony_ci
765bf215546Sopenharmony_ci   cso_set_vertex_buffers(st->cso_context, 0, 1, 0, false, &vb);
766bf215546Sopenharmony_ci   st->last_num_vbuffers = MAX2(st->last_num_vbuffers, 1);
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci   cso_draw_arrays(st->cso_context, PIPE_PRIM_QUADS, 0, num_verts);
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ciout:
771bf215546Sopenharmony_ci   restore_render_state(ctx);
772bf215546Sopenharmony_ci
773bf215546Sopenharmony_ci   pipe_resource_reference(&vb.buffer.resource, NULL);
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci   /* We uploaded modified constants, need to invalidate them. */
776bf215546Sopenharmony_ci   st->dirty |= ST_NEW_FS_CONSTANTS;
777bf215546Sopenharmony_ci}
778bf215546Sopenharmony_ci
779bf215546Sopenharmony_ci/** Per-context tear-down */
780bf215546Sopenharmony_civoid
781bf215546Sopenharmony_cist_destroy_bitmap(struct st_context *st)
782bf215546Sopenharmony_ci{
783bf215546Sopenharmony_ci   struct pipe_context *pipe = st->pipe;
784bf215546Sopenharmony_ci   struct st_bitmap_cache *cache = &st->bitmap.cache;
785bf215546Sopenharmony_ci
786bf215546Sopenharmony_ci   if (cache->trans && cache->buffer) {
787bf215546Sopenharmony_ci      pipe_texture_unmap(pipe, cache->trans);
788bf215546Sopenharmony_ci   }
789bf215546Sopenharmony_ci   pipe_resource_reference(&st->bitmap.cache.texture, NULL);
790bf215546Sopenharmony_ci}
791