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/bufferobj.h"
18 #include "main/drawtex.h"
19 #include "main/macros.h"
20 #include "main/state.h"
21 #include "main/texformat.h"
22 #include "shader/program.h"
23 #include "shader/prog_parameter.h"
24 #include "shader/prog_print.h"
26 #include "st_context.h"
28 #include "st_atom_constbuf.h"
30 #include "st_cb_drawtex.h"
32 #include "pipe/p_context.h"
33 #include "pipe/p_defines.h"
34 #include "util/u_inlines.h"
35 #include "pipe/p_shader_tokens.h"
36 #include "util/u_tile.h"
37 #include "util/u_draw_quad.h"
38 #include "util/u_simple_shaders.h"
40 #include "cso_cache/cso_context.h"
45 //struct pipe_shader_state shader;
49 uint semantic_names
[2 + MAX_TEXTURE_UNITS
];
50 uint semantic_indexes
[2 + MAX_TEXTURE_UNITS
];
53 #define MAX_SHADERS (2 * MAX_TEXTURE_UNITS)
56 * Simple linear list cache.
57 * Most of the time there'll only be one cached shader.
59 static struct cached_shader CachedShaders
[MAX_SHADERS
];
60 static GLuint NumCachedShaders
= 0;
63 #if FEATURE_OES_draw_texture
67 lookup_shader(struct pipe_context
*pipe
,
69 const uint
*semantic_names
,
70 const uint
*semantic_indexes
)
74 /* look for existing shader with same attributes */
75 for (i
= 0; i
< NumCachedShaders
; i
++) {
76 if (CachedShaders
[i
].num_attribs
== num_attribs
) {
77 GLboolean match
= GL_TRUE
;
78 for (j
= 0; j
< num_attribs
; j
++) {
79 if (semantic_names
[j
] != CachedShaders
[i
].semantic_names
[j
] ||
80 semantic_indexes
[j
] != CachedShaders
[i
].semantic_indexes
[j
]) {
86 return CachedShaders
[i
].handle
;
90 /* not found - create new one now */
91 if (NumCachedShaders
>= MAX_SHADERS
) {
95 CachedShaders
[i
].num_attribs
= num_attribs
;
96 for (j
= 0; j
< num_attribs
; j
++) {
97 CachedShaders
[i
].semantic_names
[j
] = semantic_names
[j
];
98 CachedShaders
[i
].semantic_indexes
[j
] = semantic_indexes
[j
];
101 CachedShaders
[i
].handle
=
102 util_make_vertex_passthrough_shader(pipe
,
108 return CachedShaders
[i
].handle
;
112 st_DrawTex(GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
,
113 GLfloat width
, GLfloat height
)
115 struct st_context
*st
= ctx
->st
;
116 struct pipe_context
*pipe
= st
->pipe
;
117 struct cso_context
*cso
= ctx
->st
->cso_context
;
118 struct pipe_buffer
*vbuffer
;
119 GLuint i
, numTexCoords
, numAttribs
;
121 uint semantic_names
[2 + MAX_TEXTURE_UNITS
];
122 uint semantic_indexes
[2 + MAX_TEXTURE_UNITS
];
123 struct pipe_vertex_element velements
[2 + MAX_TEXTURE_UNITS
];
124 GLbitfield inputs
= VERT_BIT_POS
;
126 /* determine if we need vertex color */
127 if (ctx
->FragmentProgram
._Current
->Base
.InputsRead
& FRAG_BIT_COL0
)
130 emitColor
= GL_FALSE
;
132 /* determine how many enabled sets of texcoords */
134 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
135 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
& TEXTURE_2D_BIT
) {
136 inputs
|= VERT_BIT_TEX(i
);
141 /* total number of attributes per vertex */
142 numAttribs
= 1 + emitColor
+ numTexCoords
;
145 /* create the vertex buffer */
146 vbuffer
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
147 numAttribs
* 4 * 4 * sizeof(GLfloat
));
149 /* load vertex buffer */
151 #define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
153 GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
154 assert(k < 4 * 4 * numAttribs); \
161 const GLfloat x0
= x
, y0
= y
, x1
= x
+ width
, y1
= y
+ height
;
162 GLfloat
*vbuf
= (GLfloat
*) pipe_buffer_map(pipe
->screen
, vbuffer
,
163 PIPE_BUFFER_USAGE_CPU_WRITE
);
166 z
= CLAMP(z
, 0.0f
, 1.0f
);
168 /* positions (in clip coords) */
170 const struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
171 const GLfloat fb_width
= (GLfloat
)fb
->Width
;
172 const GLfloat fb_height
= (GLfloat
)fb
->Height
;
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);
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 */
184 semantic_names
[0] = TGSI_SEMANTIC_POSITION
;
185 semantic_indexes
[0] = 0;
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;
204 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
205 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
& TEXTURE_2D_BIT
) {
206 struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[i
]._Current
;
207 struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
208 const GLfloat wt
= (GLfloat
) img
->Width
;
209 const GLfloat ht
= (GLfloat
) img
->Height
;
210 const GLfloat s0
= obj
->CropRect
[0] / wt
;
211 const GLfloat t0
= obj
->CropRect
[1] / ht
;
212 const GLfloat s1
= (obj
->CropRect
[0] + obj
->CropRect
[2]) / wt
;
213 const GLfloat t1
= (obj
->CropRect
[1] + obj
->CropRect
[3]) / ht
;
215 /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
216 SET_ATTRIB(0, attr
, s0
, t0
, 0.0f
, 1.0f
); /* lower left */
217 SET_ATTRIB(1, attr
, s1
, t0
, 0.0f
, 1.0f
); /* lower right */
218 SET_ATTRIB(2, attr
, s1
, t1
, 0.0f
, 1.0f
); /* upper right */
219 SET_ATTRIB(3, attr
, s0
, t1
, 0.0f
, 1.0f
); /* upper left */
221 semantic_names
[attr
] = TGSI_SEMANTIC_GENERIC
;
222 semantic_indexes
[attr
] = 0;
228 pipe_buffer_unmap(pipe
->screen
, vbuffer
);
234 cso_save_viewport(cso
);
235 cso_save_vertex_shader(cso
);
236 cso_save_vertex_elements(cso
);
239 void *vs
= lookup_shader(pipe
, numAttribs
,
240 semantic_names
, semantic_indexes
);
241 cso_set_vertex_shader_handle(cso
, vs
);
244 for (i
= 0; i
< numAttribs
; i
++) {
245 velements
[i
].src_offset
= i
* 4 * sizeof(float);
246 velements
[i
].instance_divisor
= 0;
247 velements
[i
].vertex_buffer_index
= 0;
248 velements
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
250 cso_set_vertex_elements(cso
, numAttribs
, velements
);
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
, vbuffer
,
273 PIPE_PRIM_TRIANGLE_FAN
,
275 numAttribs
); /* attribs/vert */
278 pipe_buffer_reference(&vbuffer
, NULL
);
281 cso_restore_viewport(cso
);
282 cso_restore_vertex_shader(cso
);
283 cso_restore_vertex_elements(cso
);
287 #endif /* FEATURE_OES_draw_texture */
291 st_init_drawtex_functions(struct dd_function_table
*functions
)
293 _MESA_INIT_DRAWTEX_FUNCTIONS(functions
, st_
);
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;