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/arrayobj.h"
30 #include "main/image.h"
31 #include "main/macros.h"
32 #include "main/varray.h"
36 #include "st_context.h"
38 #include "st_cb_bitmap.h"
39 #include "st_cb_bufferobjects.h"
41 #include "st_program.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_inlines.h"
46 #include "util/u_draw.h"
48 #include "draw/draw_private.h"
49 #include "draw/draw_context.h"
53 * Set the (private) draw module's post-transformed vertex format when in
54 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
57 set_feedback_vertex_format(struct gl_context
*ctx
)
60 struct st_context
*st
= st_context(ctx
);
61 struct vertex_info vinfo
;
64 memset(&vinfo
, 0, sizeof(vinfo
));
66 if (ctx
->RenderMode
== GL_SELECT
) {
67 assert(ctx
->RenderMode
== GL_SELECT
);
68 vinfo
.num_attribs
= 1;
69 vinfo
.format
[0] = FORMAT_4F
;
70 vinfo
.interp_mode
[0] = INTERP_LINEAR
;
73 /* GL_FEEDBACK, or glRasterPos */
74 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
75 vinfo
.num_attribs
= st
->state
.vs
->cso
->state
.num_outputs
;
76 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
77 vinfo
.format
[i
] = FORMAT_4F
;
78 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
82 draw_set_vertex_info(st
->draw
, &vinfo
);
88 * Helper for drawing current vertex arrays.
91 draw_arrays(struct draw_context
*draw
, unsigned mode
,
92 unsigned start
, unsigned count
)
94 struct pipe_draw_info info
;
96 util_draw_init_info(&info
);
101 info
.min_index
= start
;
102 info
.max_index
= start
+ count
- 1;
104 draw_vbo(draw
, &info
);
109 * Called by VBO to draw arrays when in selection or feedback mode and
110 * to implement glRasterPos.
111 * This is very much like the normal draw_vbo() function above.
112 * Look at code refactoring some day.
115 st_feedback_draw_vbo(struct gl_context
*ctx
,
116 const struct _mesa_prim
*prims
,
118 const struct _mesa_index_buffer
*ib
,
119 GLboolean index_bounds_valid
,
122 struct gl_transform_feedback_object
*tfb_vertcount
,
124 struct gl_buffer_object
*indirect
)
126 struct st_context
*st
= st_context(ctx
);
127 struct pipe_context
*pipe
= st
->pipe
;
128 struct draw_context
*draw
= st_get_draw_context(st
);
129 const struct st_vertex_program
*vp
;
130 const struct pipe_shader_state
*vs
;
131 struct pipe_vertex_buffer vbuffers
[PIPE_MAX_SHADER_INPUTS
];
132 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
133 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
] = {NULL
};
134 struct pipe_transfer
*ib_transfer
= NULL
;
136 const void *mapped_indices
= NULL
;
141 st_flush_bitmap_cache(st
);
142 st_invalidate_readpix_cache(st
);
144 st_validate_state(st
, ST_PIPELINE_RENDER
);
146 if (!index_bounds_valid
)
147 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
, nr_prims
);
149 /* must get these after state validation! */
151 vs
= &st
->vp_variant
->tgsi
;
153 if (!st
->vp_variant
->draw_shader
) {
154 st
->vp_variant
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
158 * Set up the draw module's state.
160 * We'd like to do this less frequently, but the normal state-update
161 * code sends state updates to the pipe, not to our private draw module.
164 draw_set_viewport_states(draw
, 0, 1, &st
->state
.viewport
[0]);
165 draw_set_clip_state(draw
, &st
->state
.clip
);
166 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
, NULL
);
167 draw_bind_vertex_shader(draw
, st
->vp_variant
->draw_shader
);
168 set_feedback_vertex_format(ctx
);
170 /* loop over TGSI shader inputs to determine vertex buffer
173 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
174 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
175 const struct gl_vertex_buffer_binding
*binding
;
176 const struct gl_array_attributes
*attrib
;
179 _mesa_draw_attrib_and_binding(ctx
, mesaAttr
, &attrib
, &binding
);
181 if (_mesa_is_bufferobj(binding
->BufferObj
)) {
182 /* Attribute data is in a VBO. */
183 struct st_buffer_object
*stobj
= st_buffer_object(binding
->BufferObj
);
184 assert(stobj
->buffer
);
186 vbuffers
[attr
].buffer
.resource
= NULL
;
187 vbuffers
[attr
].is_user_buffer
= false;
188 pipe_resource_reference(&vbuffers
[attr
].buffer
.resource
, stobj
->buffer
);
189 vbuffers
[attr
].buffer_offset
= _mesa_draw_binding_offset(binding
);
190 velements
[attr
].src_offset
=
191 _mesa_draw_attributes_relative_offset(attrib
);
193 /* map the attrib buffer */
194 map
= pipe_buffer_map(pipe
, vbuffers
[attr
].buffer
.resource
,
197 draw_set_mapped_vertex_buffer(draw
, attr
, map
,
198 vbuffers
[attr
].buffer
.resource
->width0
);
201 /* Attribute data is in a user space array. */
202 vbuffers
[attr
].buffer
.user
= attrib
->Ptr
;
203 vbuffers
[attr
].is_user_buffer
= true;
204 vbuffers
[attr
].buffer_offset
= 0;
205 velements
[attr
].src_offset
= 0;
207 draw_set_mapped_vertex_buffer(draw
, attr
,
208 vbuffers
[attr
].buffer
.user
, ~0);
211 /* common-case setup */
212 vbuffers
[attr
].stride
= binding
->Stride
; /* in bytes */
213 velements
[attr
].instance_divisor
= 0;
214 velements
[attr
].vertex_buffer_index
= attr
;
215 velements
[attr
].src_format
= st_pipe_vertex_format(attrib
);
216 assert(velements
[attr
].src_format
);
218 /* tell draw about this attribute */
220 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
224 draw_set_vertex_buffers(draw
, 0, vp
->num_inputs
, vbuffers
);
225 draw_set_vertex_elements(draw
, vp
->num_inputs
, velements
);
230 struct gl_buffer_object
*bufobj
= ib
->obj
;
231 unsigned index_size
= ib
->index_size
;
234 goto out_unref_vertex
;
236 if (bufobj
&& bufobj
->Name
) {
237 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
239 start
= pointer_to_offset(ib
->ptr
) / index_size
;
240 mapped_indices
= pipe_buffer_map(pipe
, stobj
->buffer
,
241 PIPE_TRANSFER_READ
, &ib_transfer
);
244 mapped_indices
= ib
->ptr
;
247 draw_set_indexes(draw
,
248 (ubyte
*) mapped_indices
,
252 /* set the constant buffer */
253 draw_set_mapped_constant_buffer(st
->draw
, PIPE_SHADER_VERTEX
, 0,
254 st
->state
.constants
[PIPE_SHADER_VERTEX
].ptr
,
255 st
->state
.constants
[PIPE_SHADER_VERTEX
].size
);
259 for (i
= 0; i
< nr_prims
; i
++) {
260 draw_arrays(draw
, prims
[i
].mode
, start
+ prims
[i
].start
, prims
[i
].count
);
265 * unmap vertex/index buffers
268 draw_set_indexes(draw
, NULL
, 0, 0);
270 pipe_buffer_unmap(pipe
, ib_transfer
);
274 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
275 if (vb_transfer
[attr
])
276 pipe_buffer_unmap(pipe
, vb_transfer
[attr
]);
277 draw_set_mapped_vertex_buffer(draw
, attr
, NULL
, 0);
278 pipe_vertex_buffer_unreference(&vbuffers
[attr
]);
280 draw_set_vertex_buffers(draw
, 0, vp
->num_inputs
, NULL
);