18cebbc88e452c87cafed5f73566992d72d83956
[mesa.git] / src / mesa / state_tracker / st_cb_drawtex.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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/bufferobj.h"
18 #include "main/drawtex.h"
19 #include "main/macros.h"
20 #include "shader/program.h"
21 #include "shader/prog_print.h"
22
23 #include "st_context.h"
24 #include "st_atom.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
34 #include "cso_cache/cso_context.h"
35
36
37 #if FEATURE_OES_draw_texture
38
39
40 struct cached_shader
41 {
42 void *handle;
43
44 uint num_attribs;
45 uint semantic_names[2 + MAX_TEXTURE_UNITS];
46 uint semantic_indexes[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 */
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);
99 NumCachedShaders++;
100
101 return CachedShaders[i].handle;
102 }
103
104 static void
105 st_DrawTex(GLcontext *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 = ctx->st->cso_context;
111 struct pipe_resource *vbuffer;
112 struct pipe_transfer *vbuffer_transfer;
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 GLbitfield inputs = VERT_BIT_POS;
119
120 st_validate_state(st);
121
122 /* determine if we need vertex color */
123 if (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_COL0)
124 emitColor = GL_TRUE;
125 else
126 emitColor = GL_FALSE;
127
128 /* determine how many enabled sets of texcoords */
129 numTexCoords = 0;
130 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
131 if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) {
132 inputs |= VERT_BIT_TEX(i);
133 numTexCoords++;
134 }
135 }
136
137 /* total number of attributes per vertex */
138 numAttribs = 1 + emitColor + numTexCoords;
139
140
141 /* create the vertex buffer */
142 vbuffer = pipe_buffer_create(pipe->screen, PIPE_BIND_VERTEX_BUFFER,
143 numAttribs * 4 * 4 * sizeof(GLfloat));
144
145 /* load vertex buffer */
146 {
147 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
148 do { \
149 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
150 assert(k < 4 * 4 * numAttribs); \
151 vbuf[k + 0] = X; \
152 vbuf[k + 1] = Y; \
153 vbuf[k + 2] = Z; \
154 vbuf[k + 3] = W; \
155 } while (0)
156
157 const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height;
158 GLfloat *vbuf = (GLfloat *) pipe_buffer_map(pipe, vbuffer,
159 PIPE_TRANSFER_WRITE,
160 &vbuffer_transfer);
161 GLuint attr;
162
163 z = CLAMP(z, 0.0f, 1.0f);
164
165 /* positions (in clip coords) */
166 {
167 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
168 const GLfloat fb_width = (GLfloat)fb->Width;
169 const GLfloat fb_height = (GLfloat)fb->Height;
170
171 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
172 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
173 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
174 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
175
176 SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f); /* lower left */
177 SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f); /* lower right */
178 SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f); /* upper right */
179 SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f); /* upper left */
180
181 semantic_names[0] = TGSI_SEMANTIC_POSITION;
182 semantic_indexes[0] = 0;
183 }
184
185 /* colors */
186 if (emitColor) {
187 const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
188 SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]);
189 SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]);
190 SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]);
191 SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]);
192 semantic_names[1] = TGSI_SEMANTIC_COLOR;
193 semantic_indexes[1] = 0;
194 attr = 2;
195 }
196 else {
197 attr = 1;
198 }
199
200 /* texcoords */
201 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
202 if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) {
203 struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current;
204 struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
205 const GLfloat wt = (GLfloat) img->Width;
206 const GLfloat ht = (GLfloat) img->Height;
207 const GLfloat s0 = obj->CropRect[0] / wt;
208 const GLfloat t0 = obj->CropRect[1] / ht;
209 const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt;
210 const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht;
211
212 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
213 SET_ATTRIB(0, attr, s0, t0, 0.0f, 1.0f); /* lower left */
214 SET_ATTRIB(1, attr, s1, t0, 0.0f, 1.0f); /* lower right */
215 SET_ATTRIB(2, attr, s1, t1, 0.0f, 1.0f); /* upper right */
216 SET_ATTRIB(3, attr, s0, t1, 0.0f, 1.0f); /* upper left */
217
218 semantic_names[attr] = TGSI_SEMANTIC_GENERIC;
219 semantic_indexes[attr] = 0;
220
221 attr++;
222 }
223 }
224
225 pipe_buffer_unmap(pipe, vbuffer, vbuffer_transfer);
226
227 #undef SET_ATTRIB
228 }
229
230
231 cso_save_viewport(cso);
232 cso_save_vertex_shader(cso);
233 cso_save_vertex_elements(cso);
234
235 {
236 void *vs = lookup_shader(pipe, numAttribs,
237 semantic_names, semantic_indexes);
238 cso_set_vertex_shader_handle(cso, vs);
239 }
240
241 for (i = 0; i < numAttribs; i++) {
242 velements[i].src_offset = i * 4 * sizeof(float);
243 velements[i].instance_divisor = 0;
244 velements[i].vertex_buffer_index = 0;
245 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
246 }
247 cso_set_vertex_elements(cso, numAttribs, velements);
248
249 /* viewport state: viewport matching window dims */
250 {
251 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
252 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
253 const GLfloat width = (GLfloat)fb->Width;
254 const GLfloat height = (GLfloat)fb->Height;
255 struct pipe_viewport_state vp;
256 vp.scale[0] = 0.5f * width;
257 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
258 vp.scale[2] = 1.0f;
259 vp.scale[3] = 1.0f;
260 vp.translate[0] = 0.5f * width;
261 vp.translate[1] = 0.5f * height;
262 vp.translate[2] = 0.0f;
263 vp.translate[3] = 0.0f;
264 cso_set_viewport(cso, &vp);
265 }
266
267
268 util_draw_vertex_buffer(pipe, vbuffer,
269 0, /* offset */
270 PIPE_PRIM_TRIANGLE_FAN,
271 4, /* verts */
272 numAttribs); /* attribs/vert */
273
274
275 pipe_resource_reference(&vbuffer, NULL);
276
277 /* restore state */
278 cso_restore_viewport(cso);
279 cso_restore_vertex_shader(cso);
280 cso_restore_vertex_elements(cso);
281 }
282
283
284 void
285 st_init_drawtex_functions(struct dd_function_table *functions)
286 {
287 functions->DrawTex = st_DrawTex;
288 }
289
290
291 /**
292 * Free any cached shaders
293 */
294 void
295 st_destroy_drawtex(struct st_context *st)
296 {
297 GLuint i;
298 for (i = 0; i < NumCachedShaders; i++) {
299 cso_delete_vertex_shader(st->cso_context, CachedShaders[i].handle);
300 }
301 NumCachedShaders = 0;
302 }
303
304
305 #endif /* FEATURE_OES_draw_texture */