1
/**************************************************************************
3
* Copyright 2010 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 "draw/draw_private.h"
29
#include "draw/draw_vs.h"
30
#include "draw/draw_gs.h"
31
#include "draw/draw_tess.h"
32
#include "draw/draw_context.h"
33
#include "draw/draw_vbuf.h"
34
#include "draw/draw_vertex.h"
35
#include "draw/draw_pt.h"
37
#include "pipe/p_state.h"
39
#include "util/u_math.h"
40
#include "util/u_prim.h"
41
#include "util/u_memory.h"
44
struct draw_context *draw;
46
unsigned input_vertex_stride;
47
const float (*inputs)[4];
48
const float *pre_clip_pos;
50
boolean use_pre_clip_pos;
52
unsigned emitted_primitives;
53
unsigned generated_primitives;
57
static const struct pipe_stream_output_info *
58
draw_so_info(const struct draw_context *draw)
60
const struct pipe_stream_output_info *state = NULL;
62
if (draw->gs.geometry_shader) {
63
state = &draw->gs.geometry_shader->state.stream_output;
64
} else if (draw->tes.tess_eval_shader) {
65
state = &draw->tes.tess_eval_shader->state.stream_output;
67
state = &draw->vs.vertex_shader->state.stream_output;
74
draw_has_so(const struct draw_context *draw)
76
const struct pipe_stream_output_info *state = draw_so_info(draw);
78
if (state && state->num_outputs > 0)
84
void draw_pt_so_emit_prepare(struct pt_so_emit *emit, boolean use_pre_clip_pos)
86
struct draw_context *draw = emit->draw;
88
emit->use_pre_clip_pos = use_pre_clip_pos;
89
emit->has_so = draw_has_so(draw);
91
emit->pos_idx = draw_current_shader_position_output(draw);
93
/* if we have a state with outputs make sure we have
94
* buffers to output to */
96
boolean has_valid_buffer = FALSE;
98
for (i = 0; i < draw->so.num_targets; ++i) {
99
if (draw->so.targets[i]) {
100
has_valid_buffer = TRUE;
104
emit->has_so = has_valid_buffer;
110
/* XXX: need to flush to get prim_vbuf.c to release its allocation??
112
draw_do_flush( draw, DRAW_FLUSH_BACKEND );
115
static void so_emit_prim(struct pt_so_emit *so,
117
unsigned num_vertices)
120
unsigned input_vertex_stride = so->input_vertex_stride;
121
struct draw_context *draw = so->draw;
122
const float (*input_ptr)[4];
123
const float *pcp_ptr = NULL;
124
const struct pipe_stream_output_info *state = draw_so_info(draw);
126
int buffer_total_bytes[PIPE_MAX_SO_BUFFERS];
127
boolean buffer_written[PIPE_MAX_SO_BUFFERS] = {0};
129
input_ptr = so->inputs;
130
if (so->use_pre_clip_pos)
131
pcp_ptr = so->pre_clip_pos;
133
++so->generated_primitives;
135
for (i = 0; i < draw->so.num_targets; i++) {
136
struct draw_so_target *target = draw->so.targets[i];
138
buffer_total_bytes[i] = target->internal_offset;
140
buffer_total_bytes[i] = 0;
144
/* check have we space to emit prim first - if not don't do anything */
145
for (i = 0; i < num_vertices; ++i) {
147
for (slot = 0; slot < state->num_outputs; ++slot) {
148
unsigned num_comps = state->output[slot].num_components;
149
int ob = state->output[slot].output_buffer;
150
unsigned dst_offset = state->output[slot].dst_offset * sizeof(float);
151
unsigned write_size = num_comps * sizeof(float);
153
if (state->output[slot].stream != so->stream)
155
/* If a buffer is missing then that's equivalent to
157
if (!draw->so.targets[ob]) {
160
if ((buffer_total_bytes[ob] + write_size + dst_offset) >
161
draw->so.targets[ob]->target.buffer_size) {
165
for (ob = 0; ob < draw->so.num_targets; ++ob) {
166
buffer_total_bytes[ob] += state->stride[ob] * sizeof(float);
170
for (i = 0; i < num_vertices; ++i) {
171
const float (*input)[4];
172
const float *pre_clip_pos = NULL;
175
input = (const float (*)[4])(
176
(const char *)input_ptr + (indices[i] * input_vertex_stride));
179
pre_clip_pos = (const float *)(
180
(const char *)pcp_ptr + (indices[i] * input_vertex_stride));
182
for (slot = 0; slot < state->num_outputs; ++slot) {
183
unsigned idx = state->output[slot].register_index;
184
unsigned start_comp = state->output[slot].start_component;
185
unsigned num_comps = state->output[slot].num_components;
186
unsigned stream = state->output[slot].stream;
188
if (stream != so->stream)
190
ob = state->output[slot].output_buffer;
191
buffer_written[ob] = TRUE;
193
buffer = (float *)((char *)draw->so.targets[ob]->mapping +
194
draw->so.targets[ob]->target.buffer_offset +
195
draw->so.targets[ob]->internal_offset) +
196
state->output[slot].dst_offset;
198
if (idx == so->pos_idx && pcp_ptr && so->stream == 0)
199
memcpy(buffer, &pre_clip_pos[start_comp],
200
num_comps * sizeof(float));
202
memcpy(buffer, &input[idx][start_comp],
203
num_comps * sizeof(float));
207
debug_printf("VERT[%d], stream = %d, offset = %d, slot[%d] sc = %d, num_c = %d, idx = %d = [",
209
draw->so.targets[ob]->internal_offset,
210
slot, start_comp, num_comps, idx);
211
for (j = 0; j < num_comps; ++j) {
212
unsigned *ubuffer = (unsigned*)buffer;
213
debug_printf("%d (0x%x), ", ubuffer[j], ubuffer[j]);
219
for (ob = 0; ob < draw->so.num_targets; ++ob) {
220
struct draw_so_target *target = draw->so.targets[ob];
221
if (target && buffer_written[ob]) {
222
target->internal_offset += state->stride[ob] * sizeof(float);
226
++so->emitted_primitives;
229
static void so_point(struct pt_so_emit *so, int idx)
235
so_emit_prim(so, indices, 1);
238
static void so_line(struct pt_so_emit *so, int i0, int i1)
245
so_emit_prim(so, indices, 2);
248
static void so_tri(struct pt_so_emit *so, int i0, int i1, int i2)
256
so_emit_prim(so, indices, 3);
260
#define FUNC so_run_linear
261
#define GET_ELT(idx) (start + (idx))
262
#include "draw_so_emit_tmp.h"
265
#define FUNC so_run_elts
266
#define LOCAL_VARS const ushort *elts = input_prims->elts;
267
#define GET_ELT(idx) (elts[start + (idx)])
268
#include "draw_so_emit_tmp.h"
271
void draw_pt_so_emit( struct pt_so_emit *emit,
272
int num_vertex_streams,
273
const struct draw_vertex_info *input_verts,
274
const struct draw_prim_info *input_prims )
276
struct draw_context *draw = emit->draw;
277
struct vbuf_render *render = draw->render;
278
unsigned start, i, stream;
280
if (!emit->has_so && num_vertex_streams == 1) {
281
if (draw->collect_primgen) {
284
for (i = 0; i < input_prims->primitive_count; i++) {
286
u_decomposed_prims_for_vertices(input_prims->prim,
287
input_prims->primitive_lengths[i]);
289
render->set_stream_output_info(render,
295
if (!emit->has_so && !draw->collect_primgen)
298
/* XXX: need to flush to get prim_vbuf.c to release its allocation??*/
299
draw_do_flush( draw, DRAW_FLUSH_BACKEND );
301
for (stream = 0; stream < num_vertex_streams; stream++) {
302
emit->emitted_primitives = 0;
303
emit->generated_primitives = 0;
304
if (emit->use_pre_clip_pos)
305
emit->pre_clip_pos = input_verts[stream].verts->clip_pos;
307
emit->input_vertex_stride = input_verts[stream].stride;
308
emit->inputs = (const float (*)[4])input_verts[stream].verts->data;
309
emit->stream = stream;
310
for (start = i = 0; i < input_prims[stream].primitive_count;
311
start += input_prims[stream].primitive_lengths[i], i++)
313
unsigned count = input_prims[stream].primitive_lengths[i];
315
if (input_prims->linear) {
316
so_run_linear(emit, &input_prims[stream], &input_verts[stream],
319
so_run_elts(emit, &input_prims[stream], &input_verts[stream],
323
render->set_stream_output_info(render,
325
emit->has_so ? emit->emitted_primitives : 0,
326
emit->generated_primitives);
331
struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )
333
struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);
342
void draw_pt_so_emit_destroy( struct pt_so_emit *emit )