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