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