draw_vs_exec.c revision e3309197855b5caf7c4c167d1e7beedf33ed2fdd
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 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 TUNGSTEN GRAPHICS 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 /* 29 * Authors: 30 * Keith Whitwell <keith@tungstengraphics.com> 31 * Brian Paul 32 */ 33 34#include "pipe/p_util.h" 35#include "pipe/p_shader_tokens.h" 36 37#include "draw_private.h" 38#include "draw_context.h" 39#include "draw_vs.h" 40 41#include "tgsi/util/tgsi_parse.h" 42 43#define MAX_TGSI_VERTICES 4 44 45static void 46vs_exec_prepare( struct draw_vertex_shader *shader, 47 struct draw_context *draw ) 48{ 49 /* specify the vertex program to interpret/execute */ 50 tgsi_exec_machine_bind_shader(&draw->machine, 51 shader->state.tokens, 52 PIPE_MAX_SAMPLERS, 53 NULL /*samplers*/ ); 54 55 draw_update_vertex_fetch( draw ); 56} 57 58 59/** 60 * Transform vertices with the current vertex program/shader 61 * Up to four vertices can be shaded at a time. 62 * \param vbuffer the input vertex data 63 * \param elts indexes of four input vertices 64 * \param count number of vertices to shade [1..4] 65 * \param vOut array of pointers to four output vertices 66 */ 67static boolean 68vs_exec_run( struct draw_vertex_shader *shader, 69 struct draw_context *draw, 70 const unsigned *elts, 71 unsigned count, 72 void *vOut, 73 unsigned vertex_size) 74{ 75 struct tgsi_exec_machine *machine = &draw->machine; 76 unsigned int i, j; 77 unsigned int clipped = 0; 78 79 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_MAX_ATTRIBS); 80 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_MAX_ATTRIBS); 81 const float *scale = draw->viewport.scale; 82 const float *trans = draw->viewport.translate; 83 84 assert(draw->vertex_shader->info.output_semantic_name[0] 85 == TGSI_SEMANTIC_POSITION); 86 87 machine->Consts = (float (*)[4]) draw->user.constants; 88 machine->Inputs = ALIGN16_ASSIGN(inputs); 89 if (draw->rasterizer->bypass_vs) { 90 /* outputs are just the inputs */ 91 machine->Outputs = machine->Inputs; 92 } 93 else { 94 machine->Outputs = ALIGN16_ASSIGN(outputs); 95 } 96 97 for (i = 0; i < count; i += MAX_TGSI_VERTICES) { 98 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i); 99 draw->vertex_fetch.fetch_func( draw, machine, &elts[i], max_vertices ); 100 101 if (!draw->rasterizer->bypass_vs) { 102 /* run interpreter */ 103 tgsi_exec_machine_run( machine ); 104 } 105 106 /* store machine results */ 107 for (j = 0; j < max_vertices; j++) { 108 unsigned slot; 109 float x, y, z, w; 110 struct vertex_header *out = 111 draw_header_from_block(vOut, vertex_size, i + j); 112 113 /* Handle attr[0] (position) specially: 114 * 115 * XXX: Computing the clipmask should be done in the vertex 116 * program as a set of DP4 instructions appended to the 117 * user-provided code. 118 */ 119 x = out->clip[0] = machine->Outputs[0].xyzw[0].f[j]; 120 y = out->clip[1] = machine->Outputs[0].xyzw[1].f[j]; 121 z = out->clip[2] = machine->Outputs[0].xyzw[2].f[j]; 122 w = out->clip[3] = machine->Outputs[0].xyzw[3].f[j]; 123 124 if (!draw->rasterizer->bypass_clipping) { 125 out->clipmask = compute_clipmask(out->clip, draw->plane, 126 draw->nr_planes); 127 clipped += out->clipmask; 128 129 /* divide by w */ 130 w = 1.0f / w; 131 x *= w; 132 y *= w; 133 z *= w; 134 } 135 else { 136 out->clipmask = 0; 137 } 138 out->edgeflag = 1; 139 out->vertex_id = UNDEFINED_VERTEX_ID; 140 141 if (!draw->identity_viewport) { 142 /* Viewport mapping */ 143 out->data[0][0] = x * scale[0] + trans[0]; 144 out->data[0][1] = y * scale[1] + trans[1]; 145 out->data[0][2] = z * scale[2] + trans[2]; 146 out->data[0][3] = w; 147 } 148 else { 149 out->data[0][0] = x; 150 out->data[0][1] = y; 151 out->data[0][2] = z; 152 out->data[0][3] = w; 153 } 154 155 /* Remaining attributes are packed into sequential post-transform 156 * vertex attrib slots. 157 */ 158 for (slot = 1; slot < draw->num_vs_outputs; slot++) { 159 out->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j]; 160 out->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j]; 161 out->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j]; 162 out->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j]; 163 } 164 165#if 0 /*DEBUG*/ 166 printf("%d) Post xform vert:\n", i + j); 167 for (slot = 0; slot < draw->num_vs_outputs; slot++) { 168 printf("\t%d: %f %f %f %f\n", slot, 169 out->data[slot][0], 170 out->data[slot][1], 171 out->data[slot][2], 172 out->data[slot][3]); 173 } 174#endif 175 } /* loop over vertices */ 176 } 177 return clipped != 0; 178} 179 180 181 182static void 183vs_exec_delete( struct draw_vertex_shader *dvs ) 184{ 185 FREE((void*) dvs->state.tokens); 186 FREE( dvs ); 187} 188 189 190struct draw_vertex_shader * 191draw_create_vs_exec(struct draw_context *draw, 192 const struct pipe_shader_state *state) 193{ 194 struct draw_vertex_shader *vs = CALLOC_STRUCT( draw_vertex_shader ); 195 uint nt = tgsi_num_tokens(state->tokens); 196 197 if (vs == NULL) 198 return NULL; 199 200 /* we make a private copy of the tokens */ 201 vs->state.tokens = mem_dup(state->tokens, nt * sizeof(state->tokens[0])); 202 vs->prepare = vs_exec_prepare; 203 vs->run = vs_exec_run; 204 vs->delete = vs_exec_delete; 205 206 return vs; 207} 208