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