• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * All Rights Reserved.
5  *
6  **************************************************************************/
7 
8 
9 /**
10  * Implementation of glDrawTex() for GL_OES_draw_tex
11  */
12 
13 
14 
15 
16 #include "main/image.h"
17 #include "main/macros.h"
18 #include "main/teximage.h"
19 #include "main/framebuffer.h"
20 #include "program/program.h"
21 #include "program/prog_print.h"
22 
23 #include "st_context.h"
24 #include "st_atom.h"
25 #include "st_cb_bitmap.h"
26 #include "st_cb_drawtex.h"
27 #include "st_nir.h"
28 #include "st_util.h"
29 
30 #include "pipe/p_context.h"
31 #include "pipe/p_defines.h"
32 #include "util/u_inlines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "util/u_draw_quad.h"
35 #include "util/u_upload_mgr.h"
36 
37 #include "cso_cache/cso_context.h"
38 
39 
40 struct cached_shader
41 {
42    void *handle;
43 
44    uint num_attribs;
45    gl_varying_slot slots[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  * XXX This should be per-st_context state.
54  */
55 static struct cached_shader CachedShaders[MAX_SHADERS];
56 static GLuint NumCachedShaders = 0;
57 
58 static gl_vert_attrib
slot_to_vert_attrib(gl_varying_slot slot)59 slot_to_vert_attrib(gl_varying_slot slot)
60 {
61    switch (slot) {
62    case VARYING_SLOT_POS:
63       return VERT_ATTRIB_POS;
64    case VARYING_SLOT_COL0:
65       return VERT_ATTRIB_COLOR0;
66    case VARYING_SLOT_VAR0:
67    case VARYING_SLOT_TEX0:
68       return VERT_ATTRIB_GENERIC0;
69    default:
70       unreachable("unhandled slot");
71    }
72 }
73 
74 static void *
lookup_shader(struct st_context * st,uint num_attribs,const gl_varying_slot * slots)75 lookup_shader(struct st_context *st,
76               uint num_attribs,
77               const gl_varying_slot *slots)
78 {
79    GLuint i, j;
80 
81    /* look for existing shader with same attributes */
82    for (i = 0; i < NumCachedShaders; i++) {
83       if (CachedShaders[i].num_attribs == num_attribs) {
84          GLboolean match = GL_TRUE;
85          for (j = 0; j < num_attribs; j++) {
86             if (slots[j] != CachedShaders[i].slots[j]) {
87                match = GL_FALSE;
88                break;
89             }
90          }
91          if (match)
92             return CachedShaders[i].handle;
93       }
94    }
95 
96    /* not found - create new one now */
97    if (NumCachedShaders >= MAX_SHADERS) {
98       return NULL;
99    }
100 
101    CachedShaders[i].num_attribs = num_attribs;
102    for (j = 0; j < num_attribs; j++)
103       CachedShaders[i].slots[j] = slots[j];
104 
105    unsigned inputs[2 + MAX_TEXTURE_UNITS];
106 
107    for (int j = 0; j < num_attribs; j++) {
108       inputs[j] = slot_to_vert_attrib(slots[j]);
109    }
110 
111    CachedShaders[i].handle =
112       st_nir_make_passthrough_vs(st, "st/drawtex VS", num_attribs, inputs,
113                                  slots, 0);
114 
115    NumCachedShaders++;
116 
117    return CachedShaders[i].handle;
118 }
119 
120 
121 void
st_DrawTex(struct gl_context * ctx,GLfloat x,GLfloat y,GLfloat z,GLfloat width,GLfloat height)122 st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
123            GLfloat width, GLfloat height)
124 {
125    struct st_context *st = ctx->st;
126    struct pipe_context *pipe = st->pipe;
127    struct cso_context *cso = st->cso_context;
128    struct pipe_resource *vbuffer = NULL;
129    GLuint i, numTexCoords, numAttribs;
130    GLboolean emitColor;
131    gl_varying_slot slots[2 + MAX_TEXTURE_UNITS];
132    struct cso_velems_state velems;
133    unsigned offset;
134 
135    st_flush_bitmap_cache(st);
136    st_invalidate_readpix_cache(st);
137 
138    st_validate_state(st, ST_PIPELINE_META_STATE_MASK);
139 
140    /* determine if we need vertex color */
141    if (ctx->FragmentProgram._Current->info.inputs_read & VARYING_BIT_COL0)
142       emitColor = GL_TRUE;
143    else
144       emitColor = GL_FALSE;
145 
146    /* determine how many enabled sets of texcoords */
147    numTexCoords = 0;
148    for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
149       if (ctx->Texture.Unit[i]._Current &&
150           ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) {
151          numTexCoords++;
152       }
153    }
154 
155    /* total number of attributes per vertex */
156    numAttribs = 1 + emitColor + numTexCoords;
157 
158    /* load vertex buffer */
159    {
160 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W)                              \
161       do {                                                              \
162          GLuint k = (((VERT) * numAttribs + (ATTR)) * 4);               \
163          assert(k < 4 * 4 * numAttribs);                                \
164          vbuf[k + 0] = X;                                               \
165          vbuf[k + 1] = Y;                                               \
166          vbuf[k + 2] = Z;                                               \
167          vbuf[k + 3] = W;                                               \
168       } while (0)
169 
170       const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height;
171       GLfloat *vbuf = NULL;
172       GLuint tex_attr;
173 
174       u_upload_alloc(pipe->stream_uploader, 0,
175                      numAttribs * 4 * 4 * sizeof(GLfloat), 4,
176                      &offset, &vbuffer, (void **) &vbuf);
177       if (!vbuffer) {
178          return;
179       }
180 
181       z = SATURATE(z);
182 
183       /* positions (in clip coords) */
184       {
185          const struct gl_framebuffer *fb = ctx->DrawBuffer;
186          const GLfloat fb_width = (GLfloat)_mesa_geometric_width(fb);
187          const GLfloat fb_height = (GLfloat)_mesa_geometric_height(fb);
188 
189          const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
190          const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
191          const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
192          const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
193 
194          SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f);   /* lower left */
195          SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f);   /* lower right */
196          SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f);   /* upper right */
197          SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f);   /* upper left */
198 
199          slots[0] = VARYING_SLOT_POS;
200       }
201 
202       /* colors */
203       if (emitColor) {
204          const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
205          SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]);
206          SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]);
207          SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]);
208          SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]);
209          slots[1] = VARYING_SLOT_COL0;
210          tex_attr = 2;
211       }
212       else {
213          tex_attr = 1;
214       }
215 
216       /* texcoords */
217       for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
218          if (ctx->Texture.Unit[i]._Current &&
219              ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) {
220             struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current;
221             const struct gl_texture_image *img = _mesa_base_tex_image(obj);
222             const GLfloat wt = (GLfloat) img->Width;
223             const GLfloat ht = (GLfloat) img->Height;
224             const GLfloat s0 = obj->CropRect[0] / wt;
225             const GLfloat t0 = obj->CropRect[1] / ht;
226             const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt;
227             const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht;
228 
229             /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
230             SET_ATTRIB(0, tex_attr, s0, t0, 0.0f, 1.0f);  /* lower left */
231             SET_ATTRIB(1, tex_attr, s1, t0, 0.0f, 1.0f);  /* lower right */
232             SET_ATTRIB(2, tex_attr, s1, t1, 0.0f, 1.0f);  /* upper right */
233             SET_ATTRIB(3, tex_attr, s0, t1, 0.0f, 1.0f);  /* upper left */
234 
235             slots[tex_attr] = st->needs_texcoord_semantic ?
236                VARYING_SLOT_TEX0 : VARYING_SLOT_VAR0;
237 
238             tex_attr++;
239          }
240       }
241 
242       u_upload_unmap(pipe->stream_uploader);
243 
244 #undef SET_ATTRIB
245    }
246 
247    cso_save_state(cso, (CSO_BIT_VIEWPORT |
248                         CSO_BIT_STREAM_OUTPUTS |
249                         CSO_BIT_VERTEX_SHADER |
250                         CSO_BIT_TESSCTRL_SHADER |
251                         CSO_BIT_TESSEVAL_SHADER |
252                         CSO_BIT_GEOMETRY_SHADER |
253                         CSO_BIT_VERTEX_ELEMENTS));
254 
255    {
256       void *vs = lookup_shader(st, numAttribs, slots);
257       cso_set_vertex_shader_handle(cso, vs);
258    }
259    cso_set_tessctrl_shader_handle(cso, NULL);
260    cso_set_tesseval_shader_handle(cso, NULL);
261    cso_set_geometry_shader_handle(cso, NULL);
262 
263    for (i = 0; i < numAttribs; i++) {
264       velems.velems[i].src_offset = i * 4 * sizeof(float);
265       velems.velems[i].instance_divisor = 0;
266       velems.velems[i].vertex_buffer_index = 0;
267       velems.velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
268       velems.velems[i].dual_slot = false;
269       velems.velems[i].src_stride = numAttribs * 4 * sizeof(float);
270    }
271    velems.count = numAttribs;
272 
273    cso_set_vertex_elements(cso, &velems);
274    cso_set_stream_outputs(cso, 0, NULL, NULL, 0);
275 
276    /* viewport state: viewport matching window dims */
277    {
278       const struct gl_framebuffer *fb = ctx->DrawBuffer;
279       const GLboolean invert = (_mesa_fb_orientation(fb) == Y_0_TOP);
280       const GLfloat width = (GLfloat)_mesa_geometric_width(fb);
281       const GLfloat height = (GLfloat)_mesa_geometric_height(fb);
282       struct pipe_viewport_state vp;
283       vp.scale[0] =  0.5f * width;
284       vp.scale[1] = height * (invert ? -0.5f : 0.5f);
285       vp.scale[2] = 1.0f;
286       vp.translate[0] = 0.5f * width;
287       vp.translate[1] = 0.5f * height;
288       vp.translate[2] = 0.0f;
289       vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
290       vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
291       vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
292       vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
293       cso_set_viewport(cso, &vp);
294    }
295 
296    util_draw_vertex_buffer(pipe, cso, vbuffer,
297                            offset, true,
298                            MESA_PRIM_TRIANGLE_FAN,
299                            4,  /* verts */
300                            numAttribs); /* attribs/vert */
301 
302    /* restore state */
303    cso_restore_state(cso, 0);
304    ctx->Array.NewVertexElements = true;
305    ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS;
306 }
307 
308 /**
309  * Free any cached shaders
310  */
311 void
st_destroy_drawtex(struct st_context * st)312 st_destroy_drawtex(struct st_context *st)
313 {
314    GLuint i;
315    for (i = 0; i < NumCachedShaders; i++) {
316       st->pipe->delete_vs_state(st->pipe, CachedShaders[i].handle);
317    }
318    NumCachedShaders = 0;
319 }
320