st_cb_drawtex.c revision d2633af696f2e4ff98f669061e4e222e8643312c
1/**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 **************************************************************************/
7
8
9/**
10 * Implementation of glDrawTex() for GL_OES_draw_tex
11 */
12
13
14
15#include "main/imports.h"
16#include "main/image.h"
17#include "main/macros.h"
18#include "main/mfeatures.h"
19#include "program/program.h"
20#include "program/prog_print.h"
21
22#include "st_context.h"
23#include "st_atom.h"
24#include "st_cb_drawtex.h"
25
26#include "pipe/p_context.h"
27#include "pipe/p_defines.h"
28#include "util/u_inlines.h"
29#include "pipe/p_shader_tokens.h"
30#include "util/u_draw_quad.h"
31#include "util/u_simple_shaders.h"
32
33#include "cso_cache/cso_context.h"
34
35
36#if FEATURE_OES_draw_texture
37
38
39struct cached_shader
40{
41   void *handle;
42
43   uint num_attribs;
44   uint semantic_names[2 + MAX_TEXTURE_UNITS];
45   uint semantic_indexes[2 + MAX_TEXTURE_UNITS];
46};
47
48#define MAX_SHADERS (2 * MAX_TEXTURE_UNITS)
49
50/**
51 * Simple linear list cache.
52 * Most of the time there'll only be one cached shader.
53 */
54static struct cached_shader CachedShaders[MAX_SHADERS];
55static GLuint NumCachedShaders = 0;
56
57
58static void *
59lookup_shader(struct pipe_context *pipe,
60              uint num_attribs,
61              const uint *semantic_names,
62              const uint *semantic_indexes)
63{
64   GLuint i, j;
65
66   /* look for existing shader with same attributes */
67   for (i = 0; i < NumCachedShaders; i++) {
68      if (CachedShaders[i].num_attribs == num_attribs) {
69         GLboolean match = GL_TRUE;
70         for (j = 0; j < num_attribs; j++) {
71            if (semantic_names[j] != CachedShaders[i].semantic_names[j] ||
72                semantic_indexes[j] != CachedShaders[i].semantic_indexes[j]) {
73               match = GL_FALSE;
74               break;
75            }
76         }
77         if (match)
78            return CachedShaders[i].handle;
79      }
80   }
81
82   /* not found - create new one now */
83   if (NumCachedShaders >= MAX_SHADERS) {
84      return NULL;
85   }
86
87   CachedShaders[i].num_attribs = num_attribs;
88   for (j = 0; j < num_attribs; j++) {
89      CachedShaders[i].semantic_names[j] = semantic_names[j];
90      CachedShaders[i].semantic_indexes[j] = semantic_indexes[j];
91   }
92
93   CachedShaders[i].handle =
94      util_make_vertex_passthrough_shader(pipe,
95                                          num_attribs,
96                                          semantic_names,
97                                          semantic_indexes);
98   NumCachedShaders++;
99
100   return CachedShaders[i].handle;
101}
102
103static void
104st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
105           GLfloat width, GLfloat height)
106{
107   struct st_context *st = ctx->st;
108   struct pipe_context *pipe = st->pipe;
109   struct cso_context *cso = ctx->st->cso_context;
110   struct pipe_resource *vbuffer;
111   struct pipe_transfer *vbuffer_transfer;
112   GLuint i, numTexCoords, numAttribs;
113   GLboolean emitColor;
114   uint semantic_names[2 + MAX_TEXTURE_UNITS];
115   uint semantic_indexes[2 + MAX_TEXTURE_UNITS];
116   struct pipe_vertex_element velements[2 + MAX_TEXTURE_UNITS];
117   GLbitfield inputs = VERT_BIT_POS;
118
119   st_validate_state(st);
120
121   /* determine if we need vertex color */
122   if (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_COL0)
123      emitColor = GL_TRUE;
124   else
125      emitColor = GL_FALSE;
126
127   /* determine how many enabled sets of texcoords */
128   numTexCoords = 0;
129   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
130      if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) {
131         inputs |= VERT_BIT_TEX(i);
132         numTexCoords++;
133      }
134   }
135
136   /* total number of attributes per vertex */
137   numAttribs = 1 + emitColor + numTexCoords;
138
139
140   /* create the vertex buffer */
141   vbuffer = pipe_buffer_create(pipe->screen, PIPE_BIND_VERTEX_BUFFER,
142                                PIPE_USAGE_STREAM,
143                                numAttribs * 4 * 4 * sizeof(GLfloat));
144
145   /* load vertex buffer */
146   {
147#define SET_ATTRIB(VERT, ATTR, X, Y, Z, W)                              \
148      do {                                                              \
149         GLuint k = (((VERT) * numAttribs + (ATTR)) * 4);               \
150         assert(k < 4 * 4 * numAttribs);                                \
151         vbuf[k + 0] = X;                                               \
152         vbuf[k + 1] = Y;                                               \
153         vbuf[k + 2] = Z;                                               \
154         vbuf[k + 3] = W;                                               \
155      } while (0)
156
157      const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height;
158      GLfloat *vbuf = (GLfloat *) pipe_buffer_map(pipe, vbuffer,
159                                                  PIPE_TRANSFER_WRITE,
160                                                  &vbuffer_transfer);
161      GLuint attr;
162
163      z = CLAMP(z, 0.0f, 1.0f);
164
165      /* positions (in clip coords) */
166      {
167         const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
168         const GLfloat fb_width = (GLfloat)fb->Width;
169         const GLfloat fb_height = (GLfloat)fb->Height;
170
171         const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
172         const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
173         const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
174         const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
175
176         SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f);   /* lower left */
177         SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f);   /* lower right */
178         SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f);   /* upper right */
179         SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f);   /* upper left */
180
181         semantic_names[0] = TGSI_SEMANTIC_POSITION;
182         semantic_indexes[0] = 0;
183      }
184
185      /* colors */
186      if (emitColor) {
187         const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
188         SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]);
189         SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]);
190         SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]);
191         SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]);
192         semantic_names[1] = TGSI_SEMANTIC_COLOR;
193         semantic_indexes[1] = 0;
194         attr = 2;
195      }
196      else {
197         attr = 1;
198      }
199
200      /* texcoords */
201      for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
202         if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) {
203            struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current;
204            struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
205            const GLfloat wt = (GLfloat) img->Width;
206            const GLfloat ht = (GLfloat) img->Height;
207            const GLfloat s0 = obj->CropRect[0] / wt;
208            const GLfloat t0 = obj->CropRect[1] / ht;
209            const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt;
210            const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht;
211
212            /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
213            SET_ATTRIB(0, attr, s0, t0, 0.0f, 1.0f);  /* lower left */
214            SET_ATTRIB(1, attr, s1, t0, 0.0f, 1.0f);  /* lower right */
215            SET_ATTRIB(2, attr, s1, t1, 0.0f, 1.0f);  /* upper right */
216            SET_ATTRIB(3, attr, s0, t1, 0.0f, 1.0f);  /* upper left */
217
218            semantic_names[attr] = TGSI_SEMANTIC_GENERIC;
219            semantic_indexes[attr] = 0;
220
221            attr++;
222         }
223      }
224
225      pipe_buffer_unmap(pipe, vbuffer_transfer);
226
227#undef SET_ATTRIB
228   }
229
230
231   cso_save_viewport(cso);
232   cso_save_vertex_shader(cso);
233   cso_save_geometry_shader(cso);
234   cso_save_vertex_elements(cso);
235   cso_save_vertex_buffers(cso);
236
237   {
238      void *vs = lookup_shader(pipe, numAttribs,
239                               semantic_names, semantic_indexes);
240      cso_set_vertex_shader_handle(cso, vs);
241   }
242   cso_set_geometry_shader_handle(cso, NULL);
243
244   for (i = 0; i < numAttribs; i++) {
245      velements[i].src_offset = i * 4 * sizeof(float);
246      velements[i].instance_divisor = 0;
247      velements[i].vertex_buffer_index = 0;
248      velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
249   }
250   cso_set_vertex_elements(cso, numAttribs, velements);
251
252   /* viewport state: viewport matching window dims */
253   {
254      const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
255      const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
256      const GLfloat width = (GLfloat)fb->Width;
257      const GLfloat height = (GLfloat)fb->Height;
258      struct pipe_viewport_state vp;
259      vp.scale[0] =  0.5f * width;
260      vp.scale[1] = height * (invert ? -0.5f : 0.5f);
261      vp.scale[2] = 1.0f;
262      vp.scale[3] = 1.0f;
263      vp.translate[0] = 0.5f * width;
264      vp.translate[1] = 0.5f * height;
265      vp.translate[2] = 0.0f;
266      vp.translate[3] = 0.0f;
267      cso_set_viewport(cso, &vp);
268   }
269
270
271   util_draw_vertex_buffer(pipe, cso, vbuffer,
272                           0,  /* offset */
273                           PIPE_PRIM_TRIANGLE_FAN,
274                           4,  /* verts */
275                           numAttribs); /* attribs/vert */
276
277
278   pipe_resource_reference(&vbuffer, NULL);
279
280   /* restore state */
281   cso_restore_viewport(cso);
282   cso_restore_vertex_shader(cso);
283   cso_restore_geometry_shader(cso);
284   cso_restore_vertex_elements(cso);
285   cso_restore_vertex_buffers(cso);
286}
287
288
289void
290st_init_drawtex_functions(struct dd_function_table *functions)
291{
292   functions->DrawTex = st_DrawTex;
293}
294
295
296/**
297 * Free any cached shaders
298 */
299void
300st_destroy_drawtex(struct st_context *st)
301{
302   GLuint i;
303   for (i = 0; i < NumCachedShaders; i++) {
304      cso_delete_vertex_shader(st->cso_context, CachedShaders[i].handle);
305   }
306   NumCachedShaders = 0;
307}
308
309
310#endif /* FEATURE_OES_draw_texture */
311