Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pt_so_emit.c
4565 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "draw/draw_private.h"28#include "draw/draw_vs.h"29#include "draw/draw_gs.h"30#include "draw/draw_tess.h"31#include "draw/draw_context.h"32#include "draw/draw_vbuf.h"33#include "draw/draw_vertex.h"34#include "draw/draw_pt.h"3536#include "pipe/p_state.h"3738#include "util/u_math.h"39#include "util/u_prim.h"40#include "util/u_memory.h"4142struct pt_so_emit {43struct draw_context *draw;4445unsigned input_vertex_stride;46const float (*inputs)[4];47const float *pre_clip_pos;48boolean has_so;49boolean use_pre_clip_pos;50int pos_idx;51unsigned emitted_primitives;52unsigned generated_primitives;53unsigned stream;54};5556static const struct pipe_stream_output_info *57draw_so_info(const struct draw_context *draw)58{59const struct pipe_stream_output_info *state = NULL;6061if (draw->gs.geometry_shader) {62state = &draw->gs.geometry_shader->state.stream_output;63} else if (draw->tes.tess_eval_shader) {64state = &draw->tes.tess_eval_shader->state.stream_output;65} else {66state = &draw->vs.vertex_shader->state.stream_output;67}6869return state;70}7172static inline boolean73draw_has_so(const struct draw_context *draw)74{75const struct pipe_stream_output_info *state = draw_so_info(draw);7677if (state && state->num_outputs > 0)78return TRUE;7980return FALSE;81}8283void draw_pt_so_emit_prepare(struct pt_so_emit *emit, boolean use_pre_clip_pos)84{85struct draw_context *draw = emit->draw;8687emit->use_pre_clip_pos = use_pre_clip_pos;88emit->has_so = draw_has_so(draw);89if (use_pre_clip_pos)90emit->pos_idx = draw_current_shader_position_output(draw);9192/* if we have a state with outputs make sure we have93* buffers to output to */94if (emit->has_so) {95boolean has_valid_buffer = FALSE;96unsigned i;97for (i = 0; i < draw->so.num_targets; ++i) {98if (draw->so.targets[i]) {99has_valid_buffer = TRUE;100break;101}102}103emit->has_so = has_valid_buffer;104}105106if (!emit->has_so)107return;108109/* XXX: need to flush to get prim_vbuf.c to release its allocation??110*/111draw_do_flush( draw, DRAW_FLUSH_BACKEND );112}113114static void so_emit_prim(struct pt_so_emit *so,115unsigned *indices,116unsigned num_vertices)117{118unsigned slot, i;119unsigned input_vertex_stride = so->input_vertex_stride;120struct draw_context *draw = so->draw;121const float (*input_ptr)[4];122const float *pcp_ptr = NULL;123const struct pipe_stream_output_info *state = draw_so_info(draw);124float *buffer;125int buffer_total_bytes[PIPE_MAX_SO_BUFFERS];126boolean buffer_written[PIPE_MAX_SO_BUFFERS] = {0};127128input_ptr = so->inputs;129if (so->use_pre_clip_pos)130pcp_ptr = so->pre_clip_pos;131132++so->generated_primitives;133134for (i = 0; i < draw->so.num_targets; i++) {135struct draw_so_target *target = draw->so.targets[i];136if (target) {137buffer_total_bytes[i] = target->internal_offset;138} else {139buffer_total_bytes[i] = 0;140}141}142143/* check have we space to emit prim first - if not don't do anything */144for (i = 0; i < num_vertices; ++i) {145unsigned ob;146for (slot = 0; slot < state->num_outputs; ++slot) {147unsigned num_comps = state->output[slot].num_components;148int ob = state->output[slot].output_buffer;149unsigned dst_offset = state->output[slot].dst_offset * sizeof(float);150unsigned write_size = num_comps * sizeof(float);151152if (state->output[slot].stream != so->stream)153continue;154/* If a buffer is missing then that's equivalent to155* an overflow */156if (!draw->so.targets[ob]) {157return;158}159if ((buffer_total_bytes[ob] + write_size + dst_offset) >160draw->so.targets[ob]->target.buffer_size) {161return;162}163}164for (ob = 0; ob < draw->so.num_targets; ++ob) {165buffer_total_bytes[ob] += state->stride[ob] * sizeof(float);166}167}168169for (i = 0; i < num_vertices; ++i) {170const float (*input)[4];171const float *pre_clip_pos = NULL;172unsigned ob;173174input = (const float (*)[4])(175(const char *)input_ptr + (indices[i] * input_vertex_stride));176177if (pcp_ptr)178pre_clip_pos = (const float *)(179(const char *)pcp_ptr + (indices[i] * input_vertex_stride));180181for (slot = 0; slot < state->num_outputs; ++slot) {182unsigned idx = state->output[slot].register_index;183unsigned start_comp = state->output[slot].start_component;184unsigned num_comps = state->output[slot].num_components;185unsigned stream = state->output[slot].stream;186187if (stream != so->stream)188continue;189ob = state->output[slot].output_buffer;190buffer_written[ob] = TRUE;191192buffer = (float *)((char *)draw->so.targets[ob]->mapping +193draw->so.targets[ob]->target.buffer_offset +194draw->so.targets[ob]->internal_offset) +195state->output[slot].dst_offset;196197if (idx == so->pos_idx && pcp_ptr && so->stream == 0)198memcpy(buffer, &pre_clip_pos[start_comp],199num_comps * sizeof(float));200else201memcpy(buffer, &input[idx][start_comp],202num_comps * sizeof(float));203#if 0204{205int j;206debug_printf("VERT[%d], stream = %d, offset = %d, slot[%d] sc = %d, num_c = %d, idx = %d = [",207i, stream,208draw->so.targets[ob]->internal_offset,209slot, start_comp, num_comps, idx);210for (j = 0; j < num_comps; ++j) {211unsigned *ubuffer = (unsigned*)buffer;212debug_printf("%d (0x%x), ", ubuffer[j], ubuffer[j]);213}214debug_printf("]\n");215}216#endif217}218for (ob = 0; ob < draw->so.num_targets; ++ob) {219struct draw_so_target *target = draw->so.targets[ob];220if (target && buffer_written[ob]) {221target->internal_offset += state->stride[ob] * sizeof(float);222}223}224}225++so->emitted_primitives;226}227228static void so_point(struct pt_so_emit *so, int idx)229{230unsigned indices[1];231232indices[0] = idx;233234so_emit_prim(so, indices, 1);235}236237static void so_line(struct pt_so_emit *so, int i0, int i1)238{239unsigned indices[2];240241indices[0] = i0;242indices[1] = i1;243244so_emit_prim(so, indices, 2);245}246247static void so_tri(struct pt_so_emit *so, int i0, int i1, int i2)248{249unsigned indices[3];250251indices[0] = i0;252indices[1] = i1;253indices[2] = i2;254255so_emit_prim(so, indices, 3);256}257258259#define FUNC so_run_linear260#define GET_ELT(idx) (start + (idx))261#include "draw_so_emit_tmp.h"262263264#define FUNC so_run_elts265#define LOCAL_VARS const ushort *elts = input_prims->elts;266#define GET_ELT(idx) (elts[start + (idx)])267#include "draw_so_emit_tmp.h"268269270void draw_pt_so_emit( struct pt_so_emit *emit,271int num_vertex_streams,272const struct draw_vertex_info *input_verts,273const struct draw_prim_info *input_prims )274{275struct draw_context *draw = emit->draw;276struct vbuf_render *render = draw->render;277unsigned start, i, stream;278279if (!emit->has_so) {280if (draw->collect_primgen) {281unsigned i;282unsigned total = 0;283for (i = 0; i < input_prims->primitive_count; i++) {284total +=285u_decomposed_prims_for_vertices(input_prims->prim,286input_prims->primitive_lengths[i]);287}288render->set_stream_output_info(render,2890, 0, total);290}291return;292}293294if (!draw->so.num_targets)295return;296297/* XXX: need to flush to get prim_vbuf.c to release its allocation??*/298draw_do_flush( draw, DRAW_FLUSH_BACKEND );299300for (stream = 0; stream < num_vertex_streams; stream++) {301emit->emitted_primitives = 0;302emit->generated_primitives = 0;303if (emit->use_pre_clip_pos)304emit->pre_clip_pos = input_verts[stream].verts->clip_pos;305306emit->input_vertex_stride = input_verts[stream].stride;307emit->inputs = (const float (*)[4])input_verts[stream].verts->data;308emit->stream = stream;309for (start = i = 0; i < input_prims[stream].primitive_count;310start += input_prims[stream].primitive_lengths[i], i++)311{312unsigned count = input_prims[stream].primitive_lengths[i];313314if (input_prims->linear) {315so_run_linear(emit, &input_prims[stream], &input_verts[stream],316start, count);317} else {318so_run_elts(emit, &input_prims[stream], &input_verts[stream],319start, count);320}321}322render->set_stream_output_info(render,323stream,324emit->emitted_primitives,325emit->generated_primitives);326}327}328329330struct pt_so_emit *draw_pt_so_emit_create( struct draw_context *draw )331{332struct pt_so_emit *emit = CALLOC_STRUCT(pt_so_emit);333if (!emit)334return NULL;335336emit->draw = draw;337338return emit;339}340341void draw_pt_so_emit_destroy( struct pt_so_emit *emit )342{343FREE(emit);344}345346347