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