1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "pipe/p_util.h"
35 #include "pipe/p_shader_tokens.h"
37 #include "draw_private.h"
38 #include "draw_context.h"
41 #include "tgsi/util/tgsi_parse.h"
43 #define MAX_TGSI_VERTICES 4
46 vs_exec_prepare( struct draw_vertex_shader
*shader
,
47 struct draw_context
*draw
)
49 /* specify the vertex program to interpret/execute */
50 tgsi_exec_machine_bind_shader(&draw
->machine
,
55 draw_update_vertex_fetch( draw
);
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
68 vs_exec_run( struct draw_vertex_shader
*shader
,
69 struct draw_context
*draw
,
72 struct vertex_header
*vOut
[] )
74 struct tgsi_exec_machine
*machine
= &draw
->machine
;
77 ALIGN16_DECL(struct tgsi_exec_vector
, inputs
, PIPE_MAX_ATTRIBS
);
78 ALIGN16_DECL(struct tgsi_exec_vector
, outputs
, PIPE_MAX_ATTRIBS
);
79 const float *scale
= draw
->viewport
.scale
;
80 const float *trans
= draw
->viewport
.translate
;
82 assert(draw
->vertex_shader
->info
.output_semantic_name
[0]
83 == TGSI_SEMANTIC_POSITION
);
85 machine
->Consts
= (float (*)[4]) draw
->user
.constants
;
86 machine
->Inputs
= ALIGN16_ASSIGN(inputs
);
87 if (draw
->rasterizer
->bypass_vs
) {
88 /* outputs are just the inputs */
89 machine
->Outputs
= machine
->Inputs
;
92 machine
->Outputs
= ALIGN16_ASSIGN(outputs
);
95 for (i
= 0; i
< count
; i
+= MAX_TGSI_VERTICES
) {
96 unsigned int max_vertices
= MIN2(MAX_TGSI_VERTICES
, count
- i
);
97 draw
->vertex_fetch
.fetch_func( draw
, machine
, &elts
[i
], max_vertices
);
99 if (!draw
->rasterizer
->bypass_vs
) {
100 /* run interpreter */
101 tgsi_exec_machine_run( machine
);
104 /* store machine results */
105 for (j
= 0; j
< max_vertices
; j
++) {
109 /* Handle attr[0] (position) specially:
111 * XXX: Computing the clipmask should be done in the vertex
112 * program as a set of DP4 instructions appended to the
113 * user-provided code.
115 x
= vOut
[i
+ j
]->clip
[0] = machine
->Outputs
[0].xyzw
[0].f
[j
];
116 y
= vOut
[i
+ j
]->clip
[1] = machine
->Outputs
[0].xyzw
[1].f
[j
];
117 z
= vOut
[i
+ j
]->clip
[2] = machine
->Outputs
[0].xyzw
[2].f
[j
];
118 w
= vOut
[i
+ j
]->clip
[3] = machine
->Outputs
[0].xyzw
[3].f
[j
];
120 if (!draw
->rasterizer
->bypass_clipping
) {
121 vOut
[i
+ j
]->clipmask
= compute_clipmask(vOut
[i
+ j
]->clip
, draw
->plane
,
131 vOut
[i
+ j
]->clipmask
= 0;
133 vOut
[i
+ j
]->edgeflag
= 1;
135 if (!draw
->identity_viewport
) {
136 /* Viewport mapping */
137 vOut
[i
+ j
]->data
[0][0] = x
* scale
[0] + trans
[0];
138 vOut
[i
+ j
]->data
[0][1] = y
* scale
[1] + trans
[1];
139 vOut
[i
+ j
]->data
[0][2] = z
* scale
[2] + trans
[2];
140 vOut
[i
+ j
]->data
[0][3] = w
;
143 vOut
[i
+ j
]->data
[0][0] = x
;
144 vOut
[i
+ j
]->data
[0][1] = y
;
145 vOut
[i
+ j
]->data
[0][2] = z
;
146 vOut
[i
+ j
]->data
[0][3] = w
;
149 /* Remaining attributes are packed into sequential post-transform
150 * vertex attrib slots.
152 for (slot
= 1; slot
< draw
->num_vs_outputs
; slot
++) {
153 vOut
[i
+ j
]->data
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
154 vOut
[i
+ j
]->data
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
155 vOut
[i
+ j
]->data
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
156 vOut
[i
+ j
]->data
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
160 printf("%d) Post xform vert:\n", i
+ j
);
161 for (slot
= 0; slot
< draw
->num_vs_outputs
; slot
++) {
162 printf("\t%d: %f %f %f %f\n", slot
,
163 vOut
[i
+ j
]->data
[slot
][0],
164 vOut
[i
+ j
]->data
[slot
][1],
165 vOut
[i
+ j
]->data
[slot
][2],
166 vOut
[i
+ j
]->data
[slot
][3]);
169 } /* loop over vertices */
176 vs_exec_delete( struct draw_vertex_shader
*dvs
)
178 FREE((void*) dvs
->state
.tokens
);
183 struct draw_vertex_shader
*
184 draw_create_vs_exec(struct draw_context
*draw
,
185 const struct pipe_shader_state
*state
)
187 struct draw_vertex_shader
*vs
= CALLOC_STRUCT( draw_vertex_shader
);
188 uint nt
= tgsi_num_tokens(state
->tokens
);
193 /* we make a private copy of the tokens */
194 vs
->state
.tokens
= mem_dup(state
->tokens
, nt
* sizeof(state
->tokens
[0]));
195 vs
->prepare
= vs_exec_prepare
;
196 vs
->run
= vs_exec_run
;
197 vs
->delete = vs_exec_delete
;