summaryrefslogtreecommitdiff
path: root/src/mesa/state_tracker/st_cb_bitmap.c
blob: 0291b03143caeba05ca2c8dacf393c9ad49be20b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

 /*
  * Authors:
  *   Brian Paul
  */

#include "main/imports.h"
#include "main/image.h"
#include "main/bufferobj.h"
#include "main/macros.h"
#include "main/texformat.h"
#include "shader/program.h"
#include "shader/prog_parameter.h"
#include "shader/prog_print.h"

#include "st_context.h"
#include "st_atom.h"
#include "st_atom_constbuf.h"
#include "st_program.h"
#include "st_cb_bitmap.h"
#include "st_cb_program.h"
#include "st_mesa_to_tgsi.h"
#include "st_texture.h"
#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_inlines.h"
#include "pipe/p_winsys.h"
#include "util/p_tile.h"
#include "util/u_draw_quad.h"
#include "util/u_simple_shaders.h"
#include "shader/prog_instruction.h"
#include "cso_cache/cso_context.h"



/**
 * Make fragment program for glBitmap:
 *   Sample the texture and kill the fragment if the bit is 0.
 * This program will be combined with the user's fragment program.
 */
static struct st_fragment_program *
make_bitmap_fragment_program(GLcontext *ctx)
{
   struct st_fragment_program *stfp;
   struct gl_program *p;
   GLuint ic = 0;

   p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
   if (!p)
      return NULL;

   p->NumInstructions = 5;

   p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
   if (!p->Instructions) {
      ctx->Driver.DeleteProgram(ctx, p);
      return NULL;
   }
   _mesa_init_instructions(p->Instructions, p->NumInstructions);

   /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
   p->Instructions[ic].Opcode = OPCODE_TEX;
   p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
   p->Instructions[ic].DstReg.Index = 0;
   p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
   p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
   p->Instructions[ic].TexSrcUnit = 0;
   p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
   ic++;

   /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
   p->Instructions[ic].Opcode = OPCODE_SWZ;
   p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
   p->Instructions[ic].DstReg.Index = 0;
   p->Instructions[ic].DstReg.WriteMask = WRITEMASK_X;
   p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
   p->Instructions[ic].SrcReg[0].Index = 0;
   p->Instructions[ic].SrcReg[0].Swizzle
      = MAKE_SWIZZLE4(SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE );
   ic++;

   /* SUB tmp0, tmp0.wwww, tmp0.xxxx;  #  tmp0.w -= 1 */
   p->Instructions[ic].Opcode = OPCODE_SUB;
   p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
   p->Instructions[ic].DstReg.Index = 0;
   p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
   p->Instructions[ic].SrcReg[0].Index = 0;
   p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_WWWW;
   p->Instructions[ic].SrcReg[1].File = PROGRAM_TEMPORARY;
   p->Instructions[ic].SrcReg[1].Index = 0;
   p->Instructions[ic].SrcReg[1].Swizzle = SWIZZLE_XXXX; /* 1.0 */
   ic++;

   /* KIL if tmp0 < 0 */
   p->Instructions[ic].Opcode = OPCODE_KIL;
   p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
   p->Instructions[ic].SrcReg[0].Index = 0;
   ic++;

   /* END; */
   p->Instructions[ic++].Opcode = OPCODE_END;

   assert(ic == p->NumInstructions);

   p->InputsRead = FRAG_BIT_TEX0;
   p->OutputsWritten = 0x0;

   stfp = (struct st_fragment_program *) p;
   stfp->Base.UsesKill = GL_TRUE;
   st_translate_fragment_program(ctx->st, stfp, NULL);

   return stfp;
}


/**
 * Combine basic bitmap fragment program with the user-defined program.
 */
static struct st_fragment_program *
combined_bitmap_fragment_program(GLcontext *ctx)
{
   struct st_context *st = ctx->st;
   struct st_fragment_program *stfp;

   if (!st->bitmap.program) {
      /* create the basic bitmap fragment program */
      st->bitmap.program = make_bitmap_fragment_program(ctx);
   }

   if (st->bitmap.user_prog_sn == st->fp->serialNo) {
      /* re-use */
      stfp = st->bitmap.combined_prog;
   }
   else {
      /* Concatenate the bitmap program with the current user-defined program.
       */
      stfp = (struct st_fragment_program *)
         _mesa_combine_programs(ctx,
                                &st->bitmap.program->Base.Base,
                                &st->fp->Base.Base);

#if 0
      {
         struct gl_program *p = &stfp->Base.Base;
         printf("Combined bitmap program:\n");
         _mesa_print_program(p);
         printf("InputsRead: 0x%x\n", p->InputsRead);
         printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
         _mesa_print_parameter_list(p->Parameters);
      }
#endif

      /* translate to TGSI tokens */
      st_translate_fragment_program(st, stfp, NULL);

      /* save new program, update serial numbers */
      st->bitmap.user_prog_sn = st->fp->serialNo;
      st->bitmap.combined_prog = stfp;
   }

   /* Ideally we'd have updated the pipe constants during the normal
    * st/atom mechanism.  But we can't since this is specific to glBitmap.
    */
   st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);

   return stfp;
}


/**
 * Create a texture which represents a bitmap image.
 */
static struct pipe_texture *
make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
                    const struct gl_pixelstore_attrib *unpack,
                    const GLubyte *bitmap)
{
   struct pipe_context *pipe = ctx->st->pipe;
   struct pipe_screen *screen = pipe->screen;
   struct pipe_surface *surface;
   uint format = 0, cpp, comp;
   ubyte *dest;
   struct pipe_texture *pt;
   int row, col;

   /* find a texture format we know */
   if (screen->is_format_supported( screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE )) {
      format = PIPE_FORMAT_U_I8;
      cpp = 1;
      comp = 0;
   }
   else if (screen->is_format_supported( screen, PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_TEXTURE )) {
      format = PIPE_FORMAT_A8R8G8B8_UNORM;
      cpp = 4;
      comp = 3; /* alpha channel */ /*XXX little-endian dependency */
   }
   else {
      /* XXX support more formats */
      assert( 0 );
   }

   /* PBO source... */
   bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
   if (!bitmap) {
      return NULL;
   }

   /**
    * Create texture to hold bitmap pattern.
    */
   pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0, width, height,
			  1, 0);
   if (!pt) {
      _mesa_unmap_bitmap_pbo(ctx, unpack);
      return NULL;
   }

   surface = screen->get_tex_surface(screen, pt, 0, 0, 0);

   /* map texture surface */
   dest = pipe_surface_map(surface);

   /* Put image into texture surface.
    * Note that the image is actually going to be upside down in
    * the texture.  We deal with that with texcoords.
    */

   for (row = 0; row < height; row++) {
      const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
                 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
      ubyte *destRow = dest + row * surface->pitch * cpp;

      if (unpack->LsbFirst) {
         /* Lsb first */
         GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
         for (col = 0; col < width; col++) {

            /* set texel to 255 if bit is set */
            destRow[comp] = (*src & mask) ? 255 : 0;
            destRow += cpp;

            if (mask == 128U) {
               src++;
               mask = 1U;
            }
            else {
               mask = mask << 1;
            }
         }

         /* get ready for next row */
         if (mask != 1)
            src++;
      }
      else {
         /* Msb first */
         GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
         for (col = 0; col < width; col++) {

            /* set texel to 255 if bit is set */
            destRow[comp] =(*src & mask) ? 255 : 0;
            destRow += cpp;

            if (mask == 1U) {
               src++;
               mask = 128U;
            }
            else {
               mask = mask >> 1;
            }
         }

         /* get ready for next row */
         if (mask != 128)
            src++;
      }

   } /* row */

   _mesa_unmap_bitmap_pbo(ctx, unpack);

   /* Release surface */
   pipe_surface_unmap(surface);
   pipe_surface_reference(&surface, NULL);
   pipe->texture_update(pipe, pt, 0, 0x1);

   pt->format = format;

   return pt;
}


static void
setup_bitmap_vertex_data(struct st_context *st,
                         int x, int y, int width, int height,
                         float z, const float color[4])
{
   struct pipe_context *pipe = st->pipe;
   const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
   const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
   const GLfloat x0 = x;
   const GLfloat x1 = x + width;
   const GLfloat y0 = invert ? (fb->Height - y - height) : y;
   const GLfloat y1 = invert ? (y0 + height) : y + height;
   const GLfloat bias = st->bitmap_texcoord_bias;
   const GLfloat xBias = bias / (x1-x0);
   const GLfloat yBias = bias / (y1-y0);
   const GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias;
   const GLfloat tTop = 1.0 - yBias, tBot = 1.0 - tTop - yBias;
   GLuint i;
   void *buf;

   if (!st->bitmap.vbuf) {
      st->bitmap.vbuf = pipe->winsys->buffer_create(pipe->winsys, 32,
                                                   PIPE_BUFFER_USAGE_VERTEX,
                                                   sizeof(st->bitmap.vertices));
   }

   /* positions, texcoords */
   st->bitmap.vertices[0][0][0] = x0;
   st->bitmap.vertices[0][0][1] = y0;
   st->bitmap.vertices[0][2][0] = sLeft;
   st->bitmap.vertices[0][2][1] = tTop;

   st->bitmap.vertices[1][0][0] = x1;
   st->bitmap.vertices[1][0][1] = y0;
   st->bitmap.vertices[1][2][0] = sRight;
   st->bitmap.vertices[1][2][1] = tTop;
   
   st->bitmap.vertices[2][0][0] = x1;
   st->bitmap.vertices[2][0][1] = y1;
   st->bitmap.vertices[2][2][0] = sRight;
   st->bitmap.vertices[2][2][1] = tBot;
   
   st->bitmap.vertices[3][0][0] = x0;
   st->bitmap.vertices[3][0][1] = y1;
   st->bitmap.vertices[3][2][0] = sLeft;
   st->bitmap.vertices[3][2][1] = tBot;
   
   /* same for all verts: */
   for (i = 0; i < 4; i++) {
      st->bitmap.vertices[i][0][2] = z;
      st->bitmap.vertices[i][0][3] = 1.0;
      st->bitmap.vertices[i][1][0] = color[0];
      st->bitmap.vertices[i][1][1] = color[1];
      st->bitmap.vertices[i][1][2] = color[2];
      st->bitmap.vertices[i][1][3] = color[3];
      st->bitmap.vertices[i][2][2] = 0.0; /*R*/
      st->bitmap.vertices[i][2][3] = 1.0; /*Q*/
   }

   /* put vertex data into vbuf */
   buf = pipe->winsys->buffer_map(pipe->winsys, st->bitmap.vbuf,
                                  PIPE_BUFFER_USAGE_CPU_WRITE);
   memcpy(buf, st->bitmap.vertices, sizeof(st->bitmap.vertices));
   pipe->winsys->buffer_unmap(pipe->winsys, st->bitmap.vbuf);
}



/**
 * Render a glBitmap by drawing a textured quad
 */
static void
draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
                 GLsizei width, GLsizei height,
                 struct pipe_texture *pt,
                 struct st_fragment_program *stfp)
{
   struct st_context *st = ctx->st;
   struct pipe_context *pipe = ctx->st->pipe;
   struct cso_context *cso = ctx->st->cso_context;
   GLuint maxSize;

   /* limit checks */
   /* XXX if DrawPixels image is larger than max texture size, break
    * it up into chunks.
    */
   maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
   assert(width <= maxSize);
   assert(height <= maxSize);

   cso_save_rasterizer(cso);
   cso_save_samplers(cso);

   /* rasterizer state: just scissor */
   {
      struct pipe_rasterizer_state rasterizer;
      memset(&rasterizer, 0, sizeof(rasterizer));
      if (ctx->Scissor.Enabled)
         rasterizer.scissor = 1;
      rasterizer.bypass_clipping = 1;

      cso_set_rasterizer(cso, &rasterizer);
   }

   /* fragment shader state: TEX lookup program */
   pipe->bind_fs_state(pipe, stfp->driver_shader);

   /* vertex shader state: position + texcoord pass-through */
   pipe->bind_vs_state(pipe, st->bitmap.vs);

   /* sampler / texture state */
   {
      struct pipe_sampler_state sampler;
      memset(&sampler, 0, sizeof(sampler));
      sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
      sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
      sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
      sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
      sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
      sampler.normalized_coords = 1;

      cso_single_sampler(cso, 0, &sampler);
      cso_single_sampler_done(cso);

      pipe->set_sampler_textures(pipe, 1, &pt);
   }

   /* draw textured quad */
   setup_bitmap_vertex_data(st, x, y, width, height,
                            ctx->Current.RasterPos[2],
                            ctx->Current.RasterColor);

   util_draw_vertex_buffer(pipe, st->bitmap.vbuf,
                           PIPE_PRIM_TRIANGLE_FAN,
                           4,  /* verts */
                           3); /* attribs/vert */


   /* restore state */
   cso_restore_rasterizer(cso);
   cso_restore_samplers(cso);
   /* shaders don't go through cso yet */
   pipe->bind_fs_state(pipe, st->fp->driver_shader);
   pipe->bind_vs_state(pipe, st->vp->driver_shader);
   pipe->set_sampler_textures(pipe, ctx->st->state.num_textures,
                              ctx->st->state.sampler_texture);
}



static void
st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
          const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
{
   struct st_fragment_program *stfp;
   struct st_context *st = ctx->st;
   struct pipe_texture *pt;

   st_validate_state(st);

   stfp = combined_bitmap_fragment_program(ctx);

   if (!st->bitmap.vs) {
      /* create pass-through vertex shader now */
      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
                                      TGSI_SEMANTIC_COLOR,
                                      TGSI_SEMANTIC_GENERIC };
      const uint semantic_indexes[] = { 0, 0, 0 };
      st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
                                                          semantic_names,
                                                          semantic_indexes);
   }

   st_validate_state(st);

   pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
   if (pt) {
      draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
                       width, height,
                       pt, stfp);
      pipe_texture_reference(&pt, NULL);
   }
}



void st_init_bitmap_functions(struct dd_function_table *functions)
{
   functions->Bitmap = st_Bitmap;
}


void
st_destroy_bitmap(struct st_context *st)
{
   struct pipe_context *pipe = st->pipe;

   if (st->bitmap.combined_prog) {
      st_delete_program(st->ctx, &st->bitmap.combined_prog->Base.Base);
   }

   if (st->bitmap.program) {
      st_delete_program(st->ctx, &st->bitmap.program->Base.Base);
   }

   if (st->bitmap.vs) {
      pipe->delete_vs_state(pipe, st->bitmap.vs);
      st->bitmap.vs = NULL;
   }
   if (st->bitmap.vbuf) {
      pipe->winsys->buffer_destroy(pipe->winsys, st->bitmap.vbuf);
      st->bitmap.vbuf = NULL;
   }
}