draw: finish the new pipeline setup
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_so_emit.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "util/u_memory.h"
29 #include "draw/draw_context.h"
30 #include "draw/draw_private.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34 #include "translate/translate.h"
35 #include "translate/translate_cache.h"
36
37 struct pt_so_emit {
38 struct draw_context *draw;
39
40 struct translate *translate;
41
42 struct translate_cache *cache;
43 unsigned prim;
44
45 const struct vertex_info *vinfo;
46 boolean has_so;
47 };
48
49 static void
50 prepare_so_emit( struct pt_so_emit *emit,
51 const struct vertex_info *vinfo )
52 {
53 struct draw_context *draw = emit->draw;
54 unsigned i;
55 struct translate_key hw_key;
56 unsigned dst_offset = 0;
57
58 if (emit->has_so) {
59 for (i = 0; i < draw->so.state.num_outputs; ++i) {
60 unsigned src_offset = (draw->so.state.register_index[i] * 4 *
61 sizeof(float) );
62 unsigned output_format;
63 unsigned emit_sz = 0;
64 /*unsigned output_bytes = util_format_get_blocksize(output_format);
65 unsigned nr_compo = util_format_get_nr_components(output_format);*/
66
67 output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
68 emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
69
70 /* doesn't handle EMIT_OMIT */
71 assert(emit_sz != 0);
72
73 if (draw->so.state.register_mask[i] != TGSI_WRITEMASK_XYZW) {
74 /* we only support rendering with XYZW writemask*/
75 debug_printf("NOT_IMPLEMENTED(writemask with stream output) at %s: %s:%d\n",
76 __FUNCTION__, __FILE__, __LINE__);
77 }
78
79 hw_key.element[i].type = TRANSLATE_ELEMENT_NORMAL;
80 hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
81 hw_key.element[i].input_buffer = 0;
82 hw_key.element[i].input_offset = src_offset;
83 hw_key.element[i].instance_divisor = 0;
84 hw_key.element[i].output_format = output_format;
85 hw_key.element[i].output_offset = dst_offset;
86
87 dst_offset += emit_sz;
88 }
89 hw_key.nr_elements = draw->so.state.num_outputs;
90 hw_key.output_stride = draw->so.state.stride;
91
92 if (!emit->translate ||
93 translate_key_compare(&emit->translate->key, &hw_key) != 0)
94 {
95 translate_key_sanitize(&hw_key);
96 emit->translate = translate_cache_find(emit->cache, &hw_key);
97 }
98 } else {
99 /* no stream output */
100 emit->translate = NULL;
101 }
102 }
103
104
105 void draw_pt_so_emit_prepare( struct pt_so_emit *emit,
106 unsigned prim )
107 {
108 struct draw_context *draw = emit->draw;
109 boolean ok;
110
111 emit->has_so = (draw->so.state.num_outputs > 0);
112
113 if (!emit->has_so)
114 return;
115
116 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
117 */
118 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
119
120 emit->prim = prim;
121
122 ok = draw->render->set_primitive(draw->render, emit->prim);
123 if (!ok) {
124 assert(0);
125 return;
126 }
127
128 /* Must do this after set_primitive() above: */
129 emit->vinfo = draw->render->get_vertex_info(draw->render);
130
131 prepare_so_emit( emit, emit->vinfo );
132 }
133
134
135 void draw_pt_so_emit( struct pt_so_emit *emit,
136 const struct draw_vertex_info *vert_info,
137 const struct draw_prim_info *prim_info )
138 {
139 const float (*vertex_data)[4] = (const float (*)[4])vert_info->verts->data;
140 unsigned vertex_count = vert_info->count;
141 unsigned stride = vert_info->stride;
142 struct draw_context *draw = emit->draw;
143 struct translate *translate = emit->translate;
144 struct vbuf_render *render = draw->render;
145 void *so_buffer;
146 unsigned start, i;
147
148 if (!emit->has_so)
149 return;
150
151 so_buffer = draw->so.buffers[0];
152
153 /* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
154 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
155
156 if (vertex_count == 0)
157 return;
158
159 if (vertex_count >= UNDEFINED_VERTEX_ID) {
160 assert(0);
161 return;
162 }
163
164 /* XXX we only support single output buffer */
165 if (draw->so.num_buffers != 1) {
166 debug_printf("NOT_IMPLEMENTED(multiple stream output buffers) at %s: %s:%d\n",
167 __FUNCTION__, __FILE__, __LINE__);
168 }
169
170 translate->set_buffer(translate, 0, vertex_data,
171 stride, ~0);
172
173 for (start = i = 0; i < prim_info->primitive_count;
174 start += prim_info->primitive_lengths[i], i++)
175 {
176 unsigned count = prim_info->primitive_lengths[i];
177
178 if (prim_info->linear) {
179 translate->run(translate, start, count,
180 draw->instance_id, so_buffer);
181 }
182 else {
183 debug_assert(!"Stream output can't handle non-linear prims yet");
184 translate->run(translate, start, count,
185 draw->instance_id, so_buffer);
186 }
187 }
188
189 render->set_stream_output_info(render, 0, vertex_count);
190 }
191
192
193 struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )
194 {
195 struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);
196 if (!emit)
197 return NULL;
198
199 emit->draw = draw;
200 emit->cache = translate_cache_create();
201 if (!emit->cache) {
202 FREE(emit);
203 return NULL;
204 }
205
206 return emit;
207 }
208
209 void draw_pt_so_emit_destroy( struct pt_so_emit *emit )
210 {
211 if (emit->cache)
212 translate_cache_destroy(emit->cache);
213
214 FREE(emit);
215 }