a7926295277e98a9367c052759defdd7e1bdbda1
[mesa.git] / src / mesa / state_tracker / st_cb_drawtex.c
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 #include "main/imports.h"
16 #include "main/image.h"
17 #include "main/macros.h"
18 #include "main/teximage.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_bitmap.h"
25 #include "st_cb_drawtex.h"
26
27 #include "pipe/p_context.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_inlines.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "util/u_draw_quad.h"
32 #include "util/u_simple_shaders.h"
33 #include "util/u_upload_mgr.h"
34
35 #include "cso_cache/cso_context.h"
36
37
38 struct cached_shader
39 {
40 void *handle;
41
42 uint num_attribs;
43 uint semantic_names[2 + MAX_TEXTURE_UNITS];
44 uint semantic_indexes[2 + MAX_TEXTURE_UNITS];
45 };
46
47 #define MAX_SHADERS (2 * MAX_TEXTURE_UNITS)
48
49 /**
50 * Simple linear list cache.
51 * Most of the time there'll only be one cached shader.
52 * XXX This should be per-st_context state.
53 */
54 static struct cached_shader CachedShaders[MAX_SHADERS];
55 static GLuint NumCachedShaders = 0;
56
57
58 static void *
59 lookup_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, FALSE);
98 NumCachedShaders++;
99
100 return CachedShaders[i].handle;
101 }
102
103
104 static void
105 st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
106 GLfloat width, GLfloat height)
107 {
108 struct st_context *st = ctx->st;
109 struct pipe_context *pipe = st->pipe;
110 struct cso_context *cso = st->cso_context;
111 struct pipe_resource *vbuffer = NULL;
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 unsigned offset;
118
119 st_flush_bitmap_cache(st);
120
121 st_validate_state(st, ST_PIPELINE_RENDER);
122
123 /* determine if we need vertex color */
124 if (ctx->FragmentProgram._Current->Base.InputsRead & VARYING_BIT_COL0)
125 emitColor = GL_TRUE;
126 else
127 emitColor = GL_FALSE;
128
129 /* determine how many enabled sets of texcoords */
130 numTexCoords = 0;
131 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
132 if (ctx->Texture.Unit[i]._Current &&
133 ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) {
134 numTexCoords++;
135 }
136 }
137
138 /* total number of attributes per vertex */
139 numAttribs = 1 + emitColor + numTexCoords;
140
141 /* load vertex buffer */
142 {
143 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
144 do { \
145 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
146 assert(k < 4 * 4 * numAttribs); \
147 vbuf[k + 0] = X; \
148 vbuf[k + 1] = Y; \
149 vbuf[k + 2] = Z; \
150 vbuf[k + 3] = W; \
151 } while (0)
152
153 const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height;
154 GLfloat *vbuf = NULL;
155 GLuint tex_attr;
156
157 u_upload_alloc(st->uploader, 0,
158 numAttribs * 4 * 4 * sizeof(GLfloat), 4,
159 &offset, &vbuffer, (void **) &vbuf);
160 if (!vbuffer) {
161 return;
162 }
163
164 z = CLAMP(z, 0.0f, 1.0f);
165
166 /* positions (in clip coords) */
167 {
168 const struct gl_framebuffer *fb = ctx->DrawBuffer;
169 const GLfloat fb_width = (GLfloat)fb->Width;
170 const GLfloat fb_height = (GLfloat)fb->Height;
171
172 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
173 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
174 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
175 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
176
177 SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f); /* lower left */
178 SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f); /* lower right */
179 SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f); /* upper right */
180 SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f); /* upper left */
181
182 semantic_names[0] = TGSI_SEMANTIC_POSITION;
183 semantic_indexes[0] = 0;
184 }
185
186 /* colors */
187 if (emitColor) {
188 const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
189 SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]);
190 SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]);
191 SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]);
192 SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]);
193 semantic_names[1] = TGSI_SEMANTIC_COLOR;
194 semantic_indexes[1] = 0;
195 tex_attr = 2;
196 }
197 else {
198 tex_attr = 1;
199 }
200
201 /* texcoords */
202 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
203 if (ctx->Texture.Unit[i]._Current &&
204 ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) {
205 struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current;
206 const struct gl_texture_image *img = _mesa_base_tex_image(obj);
207 const GLfloat wt = (GLfloat) img->Width;
208 const GLfloat ht = (GLfloat) img->Height;
209 const GLfloat s0 = obj->CropRect[0] / wt;
210 const GLfloat t0 = obj->CropRect[1] / ht;
211 const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt;
212 const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht;
213
214 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
215 SET_ATTRIB(0, tex_attr, s0, t0, 0.0f, 1.0f); /* lower left */
216 SET_ATTRIB(1, tex_attr, s1, t0, 0.0f, 1.0f); /* lower right */
217 SET_ATTRIB(2, tex_attr, s1, t1, 0.0f, 1.0f); /* upper right */
218 SET_ATTRIB(3, tex_attr, s0, t1, 0.0f, 1.0f); /* upper left */
219
220 semantic_names[tex_attr] = st->needs_texcoord_semantic ?
221 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC;
222 /* XXX: should this use semantic index i instead of 0 ? */
223 semantic_indexes[tex_attr] = 0;
224
225 tex_attr++;
226 }
227 }
228
229 u_upload_unmap(st->uploader);
230
231 #undef SET_ATTRIB
232 }
233
234 cso_save_state(cso, (CSO_BIT_VIEWPORT |
235 CSO_BIT_STREAM_OUTPUTS |
236 CSO_BIT_VERTEX_SHADER |
237 CSO_BIT_TESSCTRL_SHADER |
238 CSO_BIT_TESSEVAL_SHADER |
239 CSO_BIT_GEOMETRY_SHADER |
240 CSO_BIT_VERTEX_ELEMENTS |
241 CSO_BIT_AUX_VERTEX_BUFFER_SLOT));
242
243 {
244 void *vs = lookup_shader(pipe, numAttribs,
245 semantic_names, semantic_indexes);
246 cso_set_vertex_shader_handle(cso, vs);
247 }
248 cso_set_tessctrl_shader_handle(cso, NULL);
249 cso_set_tesseval_shader_handle(cso, NULL);
250 cso_set_geometry_shader_handle(cso, NULL);
251
252 for (i = 0; i < numAttribs; i++) {
253 velements[i].src_offset = i * 4 * sizeof(float);
254 velements[i].instance_divisor = 0;
255 velements[i].vertex_buffer_index = 0;
256 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
257 }
258 cso_set_vertex_elements(cso, numAttribs, velements);
259 cso_set_stream_outputs(cso, 0, NULL, NULL);
260
261 /* viewport state: viewport matching window dims */
262 {
263 const struct gl_framebuffer *fb = ctx->DrawBuffer;
264 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
265 const GLfloat width = (GLfloat)fb->Width;
266 const GLfloat height = (GLfloat)fb->Height;
267 struct pipe_viewport_state vp;
268 vp.scale[0] = 0.5f * width;
269 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
270 vp.scale[2] = 1.0f;
271 vp.translate[0] = 0.5f * width;
272 vp.translate[1] = 0.5f * height;
273 vp.translate[2] = 0.0f;
274 cso_set_viewport(cso, &vp);
275 }
276
277 util_draw_vertex_buffer(pipe, cso, vbuffer,
278 cso_get_aux_vertex_buffer_slot(cso),
279 offset, /* offset */
280 PIPE_PRIM_TRIANGLE_FAN,
281 4, /* verts */
282 numAttribs); /* attribs/vert */
283
284 pipe_resource_reference(&vbuffer, NULL);
285
286 /* restore state */
287 cso_restore_state(cso);
288 }
289
290
291 void
292 st_init_drawtex_functions(struct dd_function_table *functions)
293 {
294 functions->DrawTex = st_DrawTex;
295 }
296
297
298 /**
299 * Free any cached shaders
300 */
301 void
302 st_destroy_drawtex(struct st_context *st)
303 {
304 GLuint i;
305 for (i = 0; i < NumCachedShaders; i++) {
306 cso_delete_vertex_shader(st->cso_context, CachedShaders[i].handle);
307 }
308 NumCachedShaders = 0;
309 }