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 "main/framebuffer.h" 20 #include "program/program.h" 21 #include "program/prog_print.h" 22 23 #include "st_context.h" 24 #include "st_atom.h" 25 #include "st_cb_bitmap.h" 26 #include "st_cb_drawtex.h" 27 28 #include "pipe/p_context.h" 29 #include "pipe/p_defines.h" 30 #include "util/u_inlines.h" 31 #include "pipe/p_shader_tokens.h" 32 #include "util/u_draw_quad.h" 33 #include "util/u_simple_shaders.h" 34 #include "util/u_upload_mgr.h" 35 36 #include "cso_cache/cso_context.h" 37 38 39 struct cached_shader 40 { 41 void *handle; 42 43 uint num_attribs; 44 uint semantic_names[2 + MAX_TEXTURE_UNITS]; 45 uint semantic_indexes[2 + MAX_TEXTURE_UNITS]; 46 }; 47 48 #define MAX_SHADERS (2 * MAX_TEXTURE_UNITS) 49 50 /** 51 * Simple linear list cache. 52 * Most of the time there'll only be one cached shader. 53 * XXX This should be per-st_context state. 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, FALSE); 99 NumCachedShaders++; 100 101 return CachedShaders[i].handle; 102 } 103 104 105 static void 106 st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z, 107 GLfloat width, GLfloat height) 108 { 109 struct st_context *st = ctx->st; 110 struct pipe_context *pipe = st->pipe; 111 struct cso_context *cso = st->cso_context; 112 struct pipe_resource *vbuffer = NULL; 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 unsigned offset; 119 120 st_flush_bitmap_cache(st); 121 st_invalidate_readpix_cache(st); 122 123 st_validate_state(st, ST_PIPELINE_RENDER); 124 125 /* determine if we need vertex color */ 126 if (ctx->FragmentProgram._Current->info.inputs_read & VARYING_BIT_COL0) 127 emitColor = GL_TRUE; 128 else 129 emitColor = GL_FALSE; 130 131 /* determine how many enabled sets of texcoords */ 132 numTexCoords = 0; 133 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) { 134 if (ctx->Texture.Unit[i]._Current && 135 ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) { 136 numTexCoords++; 137 } 138 } 139 140 /* total number of attributes per vertex */ 141 numAttribs = 1 + emitColor + numTexCoords; 142 143 /* load vertex buffer */ 144 { 145 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \ 146 do { \ 147 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \ 148 assert(k < 4 * 4 * numAttribs); \ 149 vbuf[k + 0] = X; \ 150 vbuf[k + 1] = Y; \ 151 vbuf[k + 2] = Z; \ 152 vbuf[k + 3] = W; \ 153 } while (0) 154 155 const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height; 156 GLfloat *vbuf = NULL; 157 GLuint tex_attr; 158 159 u_upload_alloc(pipe->stream_uploader, 0, 160 numAttribs * 4 * 4 * sizeof(GLfloat), 4, 161 &offset, &vbuffer, (void **) &vbuf); 162 if (!vbuffer) { 163 return; 164 } 165 166 z = CLAMP(z, 0.0f, 1.0f); 167 168 /* positions (in clip coords) */ 169 { 170 const struct gl_framebuffer *fb = ctx->DrawBuffer; 171 const GLfloat fb_width = (GLfloat)_mesa_geometric_width(fb); 172 const GLfloat fb_height = (GLfloat)_mesa_geometric_height(fb); 173 174 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0); 175 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0); 176 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0); 177 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0); 178 179 SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f); /* lower left */ 180 SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f); /* lower right */ 181 SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f); /* upper right */ 182 SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f); /* upper left */ 183 184 semantic_names[0] = TGSI_SEMANTIC_POSITION; 185 semantic_indexes[0] = 0; 186 } 187 188 /* colors */ 189 if (emitColor) { 190 const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0]; 191 SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]); 192 SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]); 193 SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]); 194 SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]); 195 semantic_names[1] = TGSI_SEMANTIC_COLOR; 196 semantic_indexes[1] = 0; 197 tex_attr = 2; 198 } 199 else { 200 tex_attr = 1; 201 } 202 203 /* texcoords */ 204 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) { 205 if (ctx->Texture.Unit[i]._Current && 206 ctx->Texture.Unit[i]._Current->Target == GL_TEXTURE_2D) { 207 struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current; 208 const struct gl_texture_image *img = _mesa_base_tex_image(obj); 209 const GLfloat wt = (GLfloat) img->Width; 210 const GLfloat ht = (GLfloat) img->Height; 211 const GLfloat s0 = obj->CropRect[0] / wt; 212 const GLfloat t0 = obj->CropRect[1] / ht; 213 const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt; 214 const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht; 215 216 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/ 217 SET_ATTRIB(0, tex_attr, s0, t0, 0.0f, 1.0f); /* lower left */ 218 SET_ATTRIB(1, tex_attr, s1, t0, 0.0f, 1.0f); /* lower right */ 219 SET_ATTRIB(2, tex_attr, s1, t1, 0.0f, 1.0f); /* upper right */ 220 SET_ATTRIB(3, tex_attr, s0, t1, 0.0f, 1.0f); /* upper left */ 221 222 semantic_names[tex_attr] = st->needs_texcoord_semantic ? 223 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC; 224 /* XXX: should this use semantic index i instead of 0 ? */ 225 semantic_indexes[tex_attr] = 0; 226 227 tex_attr++; 228 } 229 } 230 231 u_upload_unmap(pipe->stream_uploader); 232 233 #undef SET_ATTRIB 234 } 235 236 cso_save_state(cso, (CSO_BIT_VIEWPORT | 237 CSO_BIT_STREAM_OUTPUTS | 238 CSO_BIT_VERTEX_SHADER | 239 CSO_BIT_TESSCTRL_SHADER | 240 CSO_BIT_TESSEVAL_SHADER | 241 CSO_BIT_GEOMETRY_SHADER | 242 CSO_BIT_VERTEX_ELEMENTS | 243 CSO_BIT_AUX_VERTEX_BUFFER_SLOT)); 244 245 { 246 void *vs = lookup_shader(pipe, numAttribs, 247 semantic_names, semantic_indexes); 248 cso_set_vertex_shader_handle(cso, vs); 249 } 250 cso_set_tessctrl_shader_handle(cso, NULL); 251 cso_set_tesseval_shader_handle(cso, NULL); 252 cso_set_geometry_shader_handle(cso, NULL); 253 254 for (i = 0; i < numAttribs; i++) { 255 velements[i].src_offset = i * 4 * sizeof(float); 256 velements[i].instance_divisor = 0; 257 velements[i].vertex_buffer_index = 0; 258 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 259 } 260 cso_set_vertex_elements(cso, numAttribs, velements); 261 cso_set_stream_outputs(cso, 0, NULL, NULL); 262 263 /* viewport state: viewport matching window dims */ 264 { 265 const struct gl_framebuffer *fb = ctx->DrawBuffer; 266 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP); 267 const GLfloat width = (GLfloat)_mesa_geometric_width(fb); 268 const GLfloat height = (GLfloat)_mesa_geometric_height(fb); 269 struct pipe_viewport_state vp; 270 vp.scale[0] = 0.5f * width; 271 vp.scale[1] = height * (invert ? -0.5f : 0.5f); 272 vp.scale[2] = 1.0f; 273 vp.translate[0] = 0.5f * width; 274 vp.translate[1] = 0.5f * height; 275 vp.translate[2] = 0.0f; 276 cso_set_viewport(cso, &vp); 277 } 278 279 util_draw_vertex_buffer(pipe, cso, vbuffer, 280 cso_get_aux_vertex_buffer_slot(cso), 281 offset, /* offset */ 282 PIPE_PRIM_TRIANGLE_FAN, 283 4, /* verts */ 284 numAttribs); /* attribs/vert */ 285 286 pipe_resource_reference(&vbuffer, NULL); 287 288 /* restore state */ 289 cso_restore_state(cso); 290 } 291 292 293 void 294 st_init_drawtex_functions(struct dd_function_table *functions) 295 { 296 functions->DrawTex = st_DrawTex; 297 } 298 299 300 /** 301 * Free any cached shaders 302 */ 303 void 304 st_destroy_drawtex(struct st_context *st) 305 { 306 GLuint i; 307 for (i = 0; i < NumCachedShaders; i++) { 308 cso_delete_vertex_shader(st->cso_context, CachedShaders[i].handle); 309 } 310 NumCachedShaders = 0; 311 } 312