1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/imports.h"
29 #include "main/image.h"
30 #include "main/macros.h"
34 #include "st_context.h"
36 #include "st_cb_bitmap.h"
37 #include "st_cb_bufferobjects.h"
39 #include "st_program.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "util/u_inlines.h"
44 #include "util/u_draw.h"
46 #include "draw/draw_private.h"
47 #include "draw/draw_context.h"
51 * Set the (private) draw module's post-transformed vertex format when in
52 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
55 set_feedback_vertex_format(struct gl_context
*ctx
)
58 struct st_context
*st
= st_context(ctx
);
59 struct vertex_info vinfo
;
62 memset(&vinfo
, 0, sizeof(vinfo
));
64 if (ctx
->RenderMode
== GL_SELECT
) {
65 assert(ctx
->RenderMode
== GL_SELECT
);
66 vinfo
.num_attribs
= 1;
67 vinfo
.format
[0] = FORMAT_4F
;
68 vinfo
.interp_mode
[0] = INTERP_LINEAR
;
71 /* GL_FEEDBACK, or glRasterPos */
72 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
73 vinfo
.num_attribs
= st
->state
.vs
->cso
->state
.num_outputs
;
74 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
75 vinfo
.format
[i
] = FORMAT_4F
;
76 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
80 draw_set_vertex_info(st
->draw
, &vinfo
);
86 * Helper for drawing current vertex arrays.
89 draw_arrays(struct draw_context
*draw
, unsigned mode
,
90 unsigned start
, unsigned count
)
92 struct pipe_draw_info info
;
94 util_draw_init_info(&info
);
99 info
.min_index
= start
;
100 info
.max_index
= start
+ count
- 1;
102 draw_vbo(draw
, &info
);
107 * Called by VBO to draw arrays when in selection or feedback mode and
108 * to implement glRasterPos.
109 * This is very much like the normal draw_vbo() function above.
110 * Look at code refactoring some day.
113 st_feedback_draw_vbo(struct gl_context
*ctx
,
114 const struct _mesa_prim
*prims
,
116 const struct _mesa_index_buffer
*ib
,
117 GLboolean index_bounds_valid
,
120 struct gl_transform_feedback_object
*tfb_vertcount
,
122 struct gl_buffer_object
*indirect
)
124 struct st_context
*st
= st_context(ctx
);
125 struct pipe_context
*pipe
= st
->pipe
;
126 struct draw_context
*draw
= st
->draw
;
127 const struct st_vertex_program
*vp
;
128 const struct pipe_shader_state
*vs
;
129 struct pipe_vertex_buffer vbuffers
[PIPE_MAX_SHADER_INPUTS
];
130 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
131 struct pipe_index_buffer ibuffer
;
132 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
] = {NULL
};
133 struct pipe_transfer
*ib_transfer
= NULL
;
134 const struct gl_client_array
**arrays
= ctx
->Array
._DrawArrays
;
136 const GLubyte
*low_addr
= NULL
;
137 const void *mapped_indices
= NULL
;
141 st_flush_bitmap_cache(st
);
143 st_validate_state(st
);
145 if (!index_bounds_valid
)
146 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
, nr_prims
);
148 /* must get these after state validation! */
150 vs
= &st
->vp_variant
->tgsi
;
152 if (!st
->vp_variant
->draw_shader
) {
153 st
->vp_variant
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
157 * Set up the draw module's state.
159 * We'd like to do this less frequently, but the normal state-update
160 * code sends state updates to the pipe, not to our private draw module.
163 draw_set_viewport_states(draw
, 0, 1, &st
->state
.viewport
[0]);
164 draw_set_clip_state(draw
, &st
->state
.clip
);
165 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
, NULL
);
166 draw_bind_vertex_shader(draw
, st
->vp_variant
->draw_shader
);
167 set_feedback_vertex_format(ctx
);
169 /* Find the lowest address of the arrays we're drawing */
170 if (vp
->num_inputs
) {
171 low_addr
= arrays
[vp
->index_to_input
[0]]->Ptr
;
173 for (attr
= 1; attr
< vp
->num_inputs
; attr
++) {
174 const GLubyte
*start
= arrays
[vp
->index_to_input
[attr
]]->Ptr
;
175 low_addr
= MIN2(low_addr
, start
);
179 /* loop over TGSI shader inputs to determine vertex buffer
182 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
183 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
184 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
187 if (bufobj
&& bufobj
->Name
) {
188 /* Attribute data is in a VBO.
189 * Recall that for VBOs, the gl_client_array->Ptr field is
190 * really an offset from the start of the VBO, not a pointer.
192 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
193 assert(stobj
->buffer
);
195 vbuffers
[attr
].buffer
= NULL
;
196 vbuffers
[attr
].user_buffer
= NULL
;
197 pipe_resource_reference(&vbuffers
[attr
].buffer
, stobj
->buffer
);
198 vbuffers
[attr
].buffer_offset
= pointer_to_offset(low_addr
);
199 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- low_addr
;
201 /* map the attrib buffer */
202 map
= pipe_buffer_map(pipe
, vbuffers
[attr
].buffer
,
205 draw_set_mapped_vertex_buffer(draw
, attr
, map
,
206 vbuffers
[attr
].buffer
->width0
);
209 vbuffers
[attr
].buffer
= NULL
;
210 vbuffers
[attr
].user_buffer
= arrays
[mesaAttr
]->Ptr
;
211 vbuffers
[attr
].buffer_offset
= 0;
212 velements
[attr
].src_offset
= 0;
214 draw_set_mapped_vertex_buffer(draw
, attr
, vbuffers
[attr
].user_buffer
,
218 /* common-case setup */
219 vbuffers
[attr
].stride
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
220 velements
[attr
].instance_divisor
= 0;
221 velements
[attr
].vertex_buffer_index
= attr
;
222 velements
[attr
].src_format
=
223 st_pipe_vertex_format(arrays
[mesaAttr
]->Type
,
224 arrays
[mesaAttr
]->Size
,
225 arrays
[mesaAttr
]->Format
,
226 arrays
[mesaAttr
]->Normalized
,
227 arrays
[mesaAttr
]->Integer
);
228 assert(velements
[attr
].src_format
);
230 /* tell draw about this attribute */
232 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
236 draw_set_vertex_buffers(draw
, 0, vp
->num_inputs
, vbuffers
);
237 draw_set_vertex_elements(draw
, vp
->num_inputs
, velements
);
239 memset(&ibuffer
, 0, sizeof(ibuffer
));
241 struct gl_buffer_object
*bufobj
= ib
->obj
;
243 ibuffer
.index_size
= vbo_sizeof_ib_type(ib
->type
);
244 if (ibuffer
.index_size
== 0)
245 goto out_unref_vertex
;
247 if (bufobj
&& bufobj
->Name
) {
248 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
250 pipe_resource_reference(&ibuffer
.buffer
, stobj
->buffer
);
251 ibuffer
.offset
= pointer_to_offset(ib
->ptr
);
253 mapped_indices
= pipe_buffer_map(pipe
, stobj
->buffer
,
254 PIPE_TRANSFER_READ
, &ib_transfer
);
257 /* skip setting ibuffer.buffer as the draw module does not use it */
258 mapped_indices
= ib
->ptr
;
261 draw_set_indexes(draw
,
262 (ubyte
*) mapped_indices
+ ibuffer
.offset
,
263 ibuffer
.index_size
, ~0);
266 /* set the constant buffer */
267 draw_set_mapped_constant_buffer(st
->draw
, PIPE_SHADER_VERTEX
, 0,
268 st
->state
.constants
[PIPE_SHADER_VERTEX
].ptr
,
269 st
->state
.constants
[PIPE_SHADER_VERTEX
].size
);
273 for (i
= 0; i
< nr_prims
; i
++) {
274 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
279 * unmap vertex/index buffers
282 draw_set_indexes(draw
, NULL
, 0, 0);
284 pipe_buffer_unmap(pipe
, ib_transfer
);
285 pipe_resource_reference(&ibuffer
.buffer
, NULL
);
289 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
290 if (vb_transfer
[attr
])
291 pipe_buffer_unmap(pipe
, vb_transfer
[attr
]);
292 draw_set_mapped_vertex_buffer(draw
, attr
, NULL
, 0);
293 pipe_resource_reference(&vbuffers
[attr
].buffer
, NULL
);
295 draw_set_vertex_buffers(draw
, 0, vp
->num_inputs
, NULL
);