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