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