1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
6 **************************************************************************/
10 * Implementation of glDrawTex() for GL_OES_draw_tex
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"
21 #include "st_context.h"
23 #include "st_cb_drawtex.h"
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"
33 #include "cso_cache/cso_context.h"
41 uint semantic_names
[2 + MAX_TEXTURE_UNITS
];
42 uint semantic_indexes
[2 + MAX_TEXTURE_UNITS
];
45 #define MAX_SHADERS (2 * MAX_TEXTURE_UNITS)
48 * Simple linear list cache.
49 * Most of the time there'll only be one cached shader.
51 static struct cached_shader CachedShaders
[MAX_SHADERS
];
52 static GLuint NumCachedShaders
= 0;
56 lookup_shader(struct pipe_context
*pipe
,
58 const uint
*semantic_names
,
59 const uint
*semantic_indexes
)
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
]) {
75 return CachedShaders
[i
].handle
;
79 /* not found - create new one now */
80 if (NumCachedShaders
>= MAX_SHADERS
) {
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
];
90 CachedShaders
[i
].handle
=
91 util_make_vertex_passthrough_shader(pipe
,
94 semantic_indexes
, FALSE
);
97 return CachedShaders
[i
].handle
;
101 st_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
102 GLfloat width
, GLfloat height
)
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
;
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
];
115 st_validate_state(st
);
117 /* determine if we need vertex color */
118 if (ctx
->FragmentProgram
._Current
->Base
.InputsRead
& VARYING_BIT_COL0
)
121 emitColor
= GL_FALSE
;
123 /* determine how many enabled sets of texcoords */
125 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
126 if (ctx
->Texture
.Unit
[i
]._Current
&&
127 ctx
->Texture
.Unit
[i
]._Current
->Target
== GL_TEXTURE_2D
) {
132 /* total number of attributes per vertex */
133 numAttribs
= 1 + emitColor
+ numTexCoords
;
135 /* load vertex buffer */
137 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
139 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
140 assert(k < 4 * 4 * numAttribs); \
147 const GLfloat x0
= x
, y0
= y
, x1
= x
+ width
, y1
= y
+ height
;
148 GLfloat
*vbuf
= NULL
;
151 if (u_upload_alloc(st
->uploader
, 0,
152 numAttribs
* 4 * 4 * sizeof(GLfloat
),
153 &offset
, &vbuffer
, (void **) &vbuf
) != PIPE_OK
) {
157 z
= CLAMP(z
, 0.0f
, 1.0f
);
159 /* positions (in clip coords) */
161 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
162 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
163 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
165 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
166 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
167 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
168 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
170 SET_ATTRIB(0, 0, clip_x0
, clip_y0
, z
, 1.0f
); /* lower left */
171 SET_ATTRIB(1, 0, clip_x1
, clip_y0
, z
, 1.0f
); /* lower right */
172 SET_ATTRIB(2, 0, clip_x1
, clip_y1
, z
, 1.0f
); /* upper right */
173 SET_ATTRIB(3, 0, clip_x0
, clip_y1
, z
, 1.0f
); /* upper left */
175 semantic_names
[0] = TGSI_SEMANTIC_POSITION
;
176 semantic_indexes
[0] = 0;
181 const GLfloat
*c
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
182 SET_ATTRIB(0, 1, c
[0], c
[1], c
[2], c
[3]);
183 SET_ATTRIB(1, 1, c
[0], c
[1], c
[2], c
[3]);
184 SET_ATTRIB(2, 1, c
[0], c
[1], c
[2], c
[3]);
185 SET_ATTRIB(3, 1, c
[0], c
[1], c
[2], c
[3]);
186 semantic_names
[1] = TGSI_SEMANTIC_COLOR
;
187 semantic_indexes
[1] = 0;
195 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
196 if (ctx
->Texture
.Unit
[i
]._Current
&&
197 ctx
->Texture
.Unit
[i
]._Current
->Target
== GL_TEXTURE_2D
) {
198 struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[i
]._Current
;
199 struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
200 const GLfloat wt
= (GLfloat
) img
->Width
;
201 const GLfloat ht
= (GLfloat
) img
->Height
;
202 const GLfloat s0
= obj
->CropRect
[0] / wt
;
203 const GLfloat t0
= obj
->CropRect
[1] / ht
;
204 const GLfloat s1
= (obj
->CropRect
[0] + obj
->CropRect
[2]) / wt
;
205 const GLfloat t1
= (obj
->CropRect
[1] + obj
->CropRect
[3]) / ht
;
207 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
208 SET_ATTRIB(0, attr
, s0
, t0
, 0.0f
, 1.0f
); /* lower left */
209 SET_ATTRIB(1, attr
, s1
, t0
, 0.0f
, 1.0f
); /* lower right */
210 SET_ATTRIB(2, attr
, s1
, t1
, 0.0f
, 1.0f
); /* upper right */
211 SET_ATTRIB(3, attr
, s0
, t1
, 0.0f
, 1.0f
); /* upper left */
213 semantic_names
[attr
] = st
->needs_texcoord_semantic
?
214 TGSI_SEMANTIC_TEXCOORD
: TGSI_SEMANTIC_GENERIC
;
215 /* XXX: should this use semantic index i instead of 0 ? */
216 semantic_indexes
[attr
] = 0;
222 u_upload_unmap(st
->uploader
);
228 cso_save_viewport(cso
);
229 cso_save_stream_outputs(cso
);
230 cso_save_vertex_shader(cso
);
231 cso_save_geometry_shader(cso
);
232 cso_save_vertex_elements(cso
);
233 cso_save_aux_vertex_buffer_slot(cso
);
236 void *vs
= lookup_shader(pipe
, numAttribs
,
237 semantic_names
, semantic_indexes
);
238 cso_set_vertex_shader_handle(cso
, vs
);
240 cso_set_geometry_shader_handle(cso
, NULL
);
242 for (i
= 0; i
< numAttribs
; i
++) {
243 velements
[i
].src_offset
= i
* 4 * sizeof(float);
244 velements
[i
].instance_divisor
= 0;
245 velements
[i
].vertex_buffer_index
= 0;
246 velements
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
248 cso_set_vertex_elements(cso
, numAttribs
, velements
);
249 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, NULL
);
251 /* viewport state: viewport matching window dims */
253 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
254 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
255 const GLfloat width
= (GLfloat
)fb
->Width
;
256 const GLfloat height
= (GLfloat
)fb
->Height
;
257 struct pipe_viewport_state vp
;
258 vp
.scale
[0] = 0.5f
* width
;
259 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
262 vp
.translate
[0] = 0.5f
* width
;
263 vp
.translate
[1] = 0.5f
* height
;
264 vp
.translate
[2] = 0.0f
;
265 vp
.translate
[3] = 0.0f
;
266 cso_set_viewport(cso
, &vp
);
270 util_draw_vertex_buffer(pipe
, cso
, vbuffer
,
271 cso_get_aux_vertex_buffer_slot(cso
),
273 PIPE_PRIM_TRIANGLE_FAN
,
275 numAttribs
); /* attribs/vert */
278 pipe_resource_reference(&vbuffer
, NULL
);
281 cso_restore_viewport(cso
);
282 cso_restore_vertex_shader(cso
);
283 cso_restore_geometry_shader(cso
);
284 cso_restore_vertex_elements(cso
);
285 cso_restore_aux_vertex_buffer_slot(cso
);
286 cso_restore_stream_outputs(cso
);
291 st_init_drawtex_functions(struct dd_function_table
*functions
)
293 functions
->DrawTex
= st_DrawTex
;
298 * Free any cached shaders
301 st_destroy_drawtex(struct st_context
*st
)
304 for (i
= 0; i
< NumCachedShaders
; i
++) {
305 cso_delete_vertex_shader(st
->cso_context
, CachedShaders
[i
].handle
);
307 NumCachedShaders
= 0;