1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 "main/mfeatures.h"
19 #include "program/program.h"
20 #include "program/prog_print.h"
22 #include "st_context.h"
24 #include "st_cb_drawtex.h"
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"
34 #include "cso_cache/cso_context.h"
37 #if FEATURE_OES_draw_texture
45 uint semantic_names
[2 + MAX_TEXTURE_UNITS
];
46 uint semantic_indexes
[2 + MAX_TEXTURE_UNITS
];
49 #define MAX_SHADERS (2 * MAX_TEXTURE_UNITS)
52 * Simple linear list cache.
53 * Most of the time there'll only be one cached shader.
55 static struct cached_shader CachedShaders
[MAX_SHADERS
];
56 static GLuint NumCachedShaders
= 0;
60 lookup_shader(struct pipe_context
*pipe
,
62 const uint
*semantic_names
,
63 const uint
*semantic_indexes
)
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
]) {
79 return CachedShaders
[i
].handle
;
83 /* not found - create new one now */
84 if (NumCachedShaders
>= MAX_SHADERS
) {
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
];
94 CachedShaders
[i
].handle
=
95 util_make_vertex_passthrough_shader(pipe
,
101 return CachedShaders
[i
].handle
;
105 st_DrawTex(struct gl_context
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
106 GLfloat width
, GLfloat height
)
108 struct st_context
*st
= ctx
->st
;
109 struct pipe_context
*pipe
= st
->pipe
;
110 struct cso_context
*cso
= ctx
->st
->cso_context
;
111 struct pipe_resource
*vbuffer
= NULL
;
112 GLuint i
, numTexCoords
, numAttribs
;
114 uint semantic_names
[2 + MAX_TEXTURE_UNITS
];
115 uint semantic_indexes
[2 + MAX_TEXTURE_UNITS
];
116 struct pipe_vertex_element velements
[2 + MAX_TEXTURE_UNITS
];
119 st_validate_state(st
);
121 /* determine if we need vertex color */
122 if (ctx
->FragmentProgram
._Current
->Base
.InputsRead
& FRAG_BIT_COL0
)
125 emitColor
= GL_FALSE
;
127 /* determine how many enabled sets of texcoords */
129 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
130 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
& TEXTURE_2D_BIT
) {
135 /* total number of attributes per vertex */
136 numAttribs
= 1 + emitColor
+ numTexCoords
;
138 /* load vertex buffer */
140 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
142 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
143 assert(k < 4 * 4 * numAttribs); \
150 const GLfloat x0
= x
, y0
= y
, x1
= x
+ width
, y1
= y
+ height
;
151 GLfloat
*vbuf
= NULL
;
154 u_upload_alloc(st
->uploader
, 0,
155 numAttribs
* 4 * 4 * sizeof(GLfloat
),
156 &offset
, &vbuffer
, (void**)&vbuf
);
161 z
= CLAMP(z
, 0.0f
, 1.0f
);
163 /* positions (in clip coords) */
165 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
166 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
167 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
169 const GLfloat clip_x0
= (GLfloat
)(x0
/ fb_width
* 2.0 - 1.0);
170 const GLfloat clip_y0
= (GLfloat
)(y0
/ fb_height
* 2.0 - 1.0);
171 const GLfloat clip_x1
= (GLfloat
)(x1
/ fb_width
* 2.0 - 1.0);
172 const GLfloat clip_y1
= (GLfloat
)(y1
/ fb_height
* 2.0 - 1.0);
174 SET_ATTRIB(0, 0, clip_x0
, clip_y0
, z
, 1.0f
); /* lower left */
175 SET_ATTRIB(1, 0, clip_x1
, clip_y0
, z
, 1.0f
); /* lower right */
176 SET_ATTRIB(2, 0, clip_x1
, clip_y1
, z
, 1.0f
); /* upper right */
177 SET_ATTRIB(3, 0, clip_x0
, clip_y1
, z
, 1.0f
); /* upper left */
179 semantic_names
[0] = TGSI_SEMANTIC_POSITION
;
180 semantic_indexes
[0] = 0;
185 const GLfloat
*c
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
186 SET_ATTRIB(0, 1, c
[0], c
[1], c
[2], c
[3]);
187 SET_ATTRIB(1, 1, c
[0], c
[1], c
[2], c
[3]);
188 SET_ATTRIB(2, 1, c
[0], c
[1], c
[2], c
[3]);
189 SET_ATTRIB(3, 1, c
[0], c
[1], c
[2], c
[3]);
190 semantic_names
[1] = TGSI_SEMANTIC_COLOR
;
191 semantic_indexes
[1] = 0;
199 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
200 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
& TEXTURE_2D_BIT
) {
201 struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[i
]._Current
;
202 struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
203 const GLfloat wt
= (GLfloat
) img
->Width
;
204 const GLfloat ht
= (GLfloat
) img
->Height
;
205 const GLfloat s0
= obj
->CropRect
[0] / wt
;
206 const GLfloat t0
= obj
->CropRect
[1] / ht
;
207 const GLfloat s1
= (obj
->CropRect
[0] + obj
->CropRect
[2]) / wt
;
208 const GLfloat t1
= (obj
->CropRect
[1] + obj
->CropRect
[3]) / ht
;
210 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
211 SET_ATTRIB(0, attr
, s0
, t0
, 0.0f
, 1.0f
); /* lower left */
212 SET_ATTRIB(1, attr
, s1
, t0
, 0.0f
, 1.0f
); /* lower right */
213 SET_ATTRIB(2, attr
, s1
, t1
, 0.0f
, 1.0f
); /* upper right */
214 SET_ATTRIB(3, attr
, s0
, t1
, 0.0f
, 1.0f
); /* upper left */
216 semantic_names
[attr
] = TGSI_SEMANTIC_GENERIC
;
217 semantic_indexes
[attr
] = 0;
223 u_upload_unmap(st
->uploader
);
229 cso_save_viewport(cso
);
230 cso_save_stream_outputs(cso
);
231 cso_save_vertex_shader(cso
);
232 cso_save_geometry_shader(cso
);
233 cso_save_vertex_elements(cso
);
234 cso_save_vertex_buffers(cso
);
237 void *vs
= lookup_shader(pipe
, numAttribs
,
238 semantic_names
, semantic_indexes
);
239 cso_set_vertex_shader_handle(cso
, vs
);
241 cso_set_geometry_shader_handle(cso
, NULL
);
243 for (i
= 0; i
< numAttribs
; i
++) {
244 velements
[i
].src_offset
= i
* 4 * sizeof(float);
245 velements
[i
].instance_divisor
= 0;
246 velements
[i
].vertex_buffer_index
= 0;
247 velements
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
249 cso_set_vertex_elements(cso
, numAttribs
, velements
);
250 cso_set_stream_outputs(st
->cso_context
, 0, NULL
, 0);
252 /* viewport state: viewport matching window dims */
254 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
255 const GLboolean invert
= (st_fb_orientation(fb
) == Y_0_TOP
);
256 const GLfloat width
= (GLfloat
)fb
->Width
;
257 const GLfloat height
= (GLfloat
)fb
->Height
;
258 struct pipe_viewport_state vp
;
259 vp
.scale
[0] = 0.5f
* width
;
260 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
263 vp
.translate
[0] = 0.5f
* width
;
264 vp
.translate
[1] = 0.5f
* height
;
265 vp
.translate
[2] = 0.0f
;
266 vp
.translate
[3] = 0.0f
;
267 cso_set_viewport(cso
, &vp
);
271 util_draw_vertex_buffer(pipe
, cso
, vbuffer
,
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_vertex_buffers(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;
311 #endif /* FEATURE_OES_draw_texture */