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_shader(st, "st/drawtex VS",
113 MESA_SHADER_VERTEX,
114 num_attribs, inputs,
115 slots, NULL, 0);
116
117 NumCachedShaders++;
118
119 return CachedShaders[i].handle;
120 }
121
122
123 void
st_DrawTex(struct gl_context * ctx,GLfloat x,GLfloat y,GLfloat z,GLfloat width,GLfloat height)124 st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
125 GLfloat width, GLfloat height)
126 {
127 struct st_context *st = ctx->st;
128 struct pipe_context *pipe = st->pipe;
129 struct cso_context *cso = st->cso_context;
130 struct pipe_resource *vbuffer = NULL;
131 GLuint i, numTexCoords, numAttribs;
132 GLboolean emitColor;
133 gl_varying_slot slots[2 + MAX_TEXTURE_UNITS];
134 struct cso_velems_state velems;
135 unsigned offset;
136
137 st_flush_bitmap_cache(st);
138 st_invalidate_readpix_cache(st);
139
140 st_validate_state(st, ST_PIPELINE_META_STATE_MASK);
141
142 /* determine if we need vertex color */
143 if (ctx->FragmentProgram._Current->info.inputs_read & VARYING_BIT_COL0)
144 emitColor = GL_TRUE;
145 else
146 emitColor = GL_FALSE;
147
148 /* determine how many enabled sets of texcoords */
149 numTexCoords = 0;
150 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
151 if (ctx->Texture.Unit[i]._Current &&
152 ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) {
153 numTexCoords++;
154 }
155 }
156
157 /* total number of attributes per vertex */
158 numAttribs = 1 + emitColor + numTexCoords;
159
160 /* load vertex buffer */
161 {
162 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
163 do { \
164 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
165 assert(k < 4 * 4 * numAttribs); \
166 vbuf[k + 0] = X; \
167 vbuf[k + 1] = Y; \
168 vbuf[k + 2] = Z; \
169 vbuf[k + 3] = W; \
170 } while (0)
171
172 const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height;
173 GLfloat *vbuf = NULL;
174 GLuint tex_attr;
175
176 u_upload_alloc(pipe->stream_uploader, 0,
177 numAttribs * 4 * 4 * sizeof(GLfloat), 4,
178 &offset, &vbuffer, (void **) &vbuf);
179 if (!vbuffer) {
180 return;
181 }
182
183 z = SATURATE(z);
184
185 /* positions (in clip coords) */
186 {
187 const struct gl_framebuffer *fb = ctx->DrawBuffer;
188 const GLfloat fb_width = (GLfloat)_mesa_geometric_width(fb);
189 const GLfloat fb_height = (GLfloat)_mesa_geometric_height(fb);
190
191 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
192 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
193 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
194 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
195
196 SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f); /* lower left */
197 SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f); /* lower right */
198 SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f); /* upper right */
199 SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f); /* upper left */
200
201 slots[0] = VARYING_SLOT_POS;
202 }
203
204 /* colors */
205 if (emitColor) {
206 const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
207 SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]);
208 SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]);
209 SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]);
210 SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]);
211 slots[1] = VARYING_SLOT_COL0;
212 tex_attr = 2;
213 }
214 else {
215 tex_attr = 1;
216 }
217
218 /* texcoords */
219 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
220 if (ctx->Texture.Unit[i]._Current &&
221 ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) {
222 struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current;
223 const struct gl_texture_image *img = _mesa_base_tex_image(obj);
224 const GLfloat wt = (GLfloat) img->Width;
225 const GLfloat ht = (GLfloat) img->Height;
226 const GLfloat s0 = obj->CropRect[0] / wt;
227 const GLfloat t0 = obj->CropRect[1] / ht;
228 const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt;
229 const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht;
230
231 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
232 SET_ATTRIB(0, tex_attr, s0, t0, 0.0f, 1.0f); /* lower left */
233 SET_ATTRIB(1, tex_attr, s1, t0, 0.0f, 1.0f); /* lower right */
234 SET_ATTRIB(2, tex_attr, s1, t1, 0.0f, 1.0f); /* upper right */
235 SET_ATTRIB(3, tex_attr, s0, t1, 0.0f, 1.0f); /* upper left */
236
237 slots[tex_attr] = st->needs_texcoord_semantic ?
238 VARYING_SLOT_TEX0 : VARYING_SLOT_VAR0;
239
240 tex_attr++;
241 }
242 }
243
244 u_upload_unmap(pipe->stream_uploader);
245
246 #undef SET_ATTRIB
247 }
248
249 cso_save_state(cso, (CSO_BIT_VIEWPORT |
250 CSO_BIT_STREAM_OUTPUTS |
251 CSO_BIT_VERTEX_SHADER |
252 CSO_BIT_TESSCTRL_SHADER |
253 CSO_BIT_TESSEVAL_SHADER |
254 CSO_BIT_GEOMETRY_SHADER |
255 CSO_BIT_VERTEX_ELEMENTS));
256
257 {
258 void *vs = lookup_shader(st, numAttribs, slots);
259 cso_set_vertex_shader_handle(cso, vs);
260 }
261 cso_set_tessctrl_shader_handle(cso, NULL);
262 cso_set_tesseval_shader_handle(cso, NULL);
263 cso_set_geometry_shader_handle(cso, NULL);
264
265 for (i = 0; i < numAttribs; i++) {
266 velems.velems[i].src_offset = i * 4 * sizeof(float);
267 velems.velems[i].instance_divisor = 0;
268 velems.velems[i].vertex_buffer_index = 0;
269 velems.velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
270 velems.velems[i].dual_slot = false;
271 velems.velems[i].src_stride = numAttribs * 4 * sizeof(float);
272 }
273 velems.count = numAttribs;
274
275 cso_set_vertex_elements(cso, &velems);
276 cso_set_stream_outputs(cso, 0, NULL, NULL);
277
278 /* viewport state: viewport matching window dims */
279 {
280 const struct gl_framebuffer *fb = ctx->DrawBuffer;
281 const GLboolean invert = (_mesa_fb_orientation(fb) == Y_0_TOP);
282 const GLfloat width = (GLfloat)_mesa_geometric_width(fb);
283 const GLfloat height = (GLfloat)_mesa_geometric_height(fb);
284 struct pipe_viewport_state vp;
285 vp.scale[0] = 0.5f * width;
286 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
287 vp.scale[2] = 1.0f;
288 vp.translate[0] = 0.5f * width;
289 vp.translate[1] = 0.5f * height;
290 vp.translate[2] = 0.0f;
291 vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
292 vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
293 vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
294 vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
295 cso_set_viewport(cso, &vp);
296 }
297
298 util_draw_vertex_buffer(pipe, cso, vbuffer,
299 offset, true,
300 MESA_PRIM_TRIANGLE_FAN,
301 4, /* verts */
302 numAttribs); /* attribs/vert */
303
304 /* restore state */
305 cso_restore_state(cso, 0);
306 ctx->Array.NewVertexElements = true;
307 ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS;
308 }
309
310 /**
311 * Free any cached shaders
312 */
313 void
st_destroy_drawtex(struct st_context * st)314 st_destroy_drawtex(struct st_context *st)
315 {
316 GLuint i;
317 for (i = 0; i < NumCachedShaders; i++) {
318 st->pipe->delete_vs_state(st->pipe, CachedShaders[i].handle);
319 }
320 NumCachedShaders = 0;
321 }
322