draw: Remove unnecessary headers.
[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 float (*vertex_data)[4],
137 unsigned vertex_count,
138 unsigned stride )
139 {
140 struct draw_context *draw = emit->draw;
141 struct translate *translate = emit->translate;
142 struct vbuf_render *render = draw->render;
143 void *so_buffer;
144
145 if (!emit->has_so)
146 return;
147
148 so_buffer = draw->so.buffers[0];
149
150 /* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
151 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
152
153 if (vertex_count == 0)
154 return;
155
156 if (vertex_count >= UNDEFINED_VERTEX_ID) {
157 assert(0);
158 return;
159 }
160
161 /* XXX we only support single output buffer */
162 if (draw->so.num_buffers != 1) {
163 debug_printf("NOT_IMPLEMENTED(multiple stream output buffers) at %s: %s:%d\n",
164 __FUNCTION__, __FILE__, __LINE__);
165 }
166
167 translate->set_buffer(translate, 0, vertex_data,
168 stride, ~0);
169 translate->run(translate, 0, vertex_count,
170 draw->instance_id, so_buffer);
171
172 render->set_stream_output_info(render, 0, vertex_count);
173 }
174
175
176 struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )
177 {
178 struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);
179 if (!emit)
180 return NULL;
181
182 emit->draw = draw;
183 emit->cache = translate_cache_create();
184 if (!emit->cache) {
185 FREE(emit);
186 return NULL;
187 }
188
189 return emit;
190 }
191
192 void draw_pt_so_emit_destroy( struct pt_so_emit *emit )
193 {
194 if (emit->cache)
195 translate_cache_destroy(emit->cache);
196
197 FREE(emit);
198 }