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 **************************************************************************/
31 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "pipe/p_util.h"
36 #include "pipe/p_shader_tokens.h"
37 #include "draw_private.h"
38 #include "draw_context.h"
41 #include "tgsi/util/tgsi_parse.h"
45 #include "gallivm/gallivm.h"
47 struct draw_llvm_vertex_shader
{
48 struct draw_vertex_shader base
;
49 struct gallivm_prog
*llvm_prog
;
53 static INLINE
unsigned
54 compute_clipmask(const float *clip
, /*const*/ float plane
[][4], unsigned nr
)
59 /* Do the hardwired planes first:
61 if (-clip
[0] + clip
[3] < 0) mask
|= CLIP_RIGHT_BIT
;
62 if ( clip
[0] + clip
[3] < 0) mask
|= CLIP_LEFT_BIT
;
63 if (-clip
[1] + clip
[3] < 0) mask
|= CLIP_TOP_BIT
;
64 if ( clip
[1] + clip
[3] < 0) mask
|= CLIP_BOTTOM_BIT
;
65 if (-clip
[2] + clip
[3] < 0) mask
|= CLIP_FAR_BIT
;
66 if ( clip
[2] + clip
[3] < 0) mask
|= CLIP_NEAR_BIT
;
68 /* Followed by any remaining ones:
70 for (i
= 6; i
< nr
; i
++) {
71 if (dot4(clip
, plane
[i
]) < 0)
81 vs_llvm_prepare( struct draw_vertex_shader
*base
,
82 struct draw_context
*draw
)
84 draw_update_vertex_fetch( draw
);
90 * Transform vertices with the current vertex program/shader
91 * Up to four vertices can be shaded at a time.
92 * \param vbuffer the input vertex data
93 * \param elts indexes of four input vertices
94 * \param count number of vertices to shade [1..4]
95 * \param vOut array of pointers to four output vertices
98 vs_llvm_run( struct draw_vertex_shader
*base
,
99 struct draw_context
*draw
,
100 const unsigned *elts
,
102 struct vertex_header
*vOut
[] )
104 struct draw_llvm_vertex_shader
*shader
=
105 (struct draw_llvm_vertex_shader
*)base
;
107 struct tgsi_exec_machine
*machine
= &draw
->machine
;
110 ALIGN16_DECL(struct tgsi_exec_vector
, inputs
, PIPE_MAX_ATTRIBS
);
111 ALIGN16_DECL(struct tgsi_exec_vector
, outputs
, PIPE_MAX_ATTRIBS
);
112 const float *scale
= draw
->viewport
.scale
;
113 const float *trans
= draw
->viewport
.translate
;
117 assert(draw
->vertex_shader
->state
->output_semantic_name
[0]
118 == TGSI_SEMANTIC_POSITION
);
120 /* Consts does not require 16 byte alignment. */
121 machine
->Consts
= (float (*)[4]) draw
->user
.constants
;
123 machine
->Inputs
= ALIGN16_ASSIGN(inputs
);
124 if (draw
->rasterizer
->bypass_vs
) {
125 /* outputs are just the inputs */
126 machine
->Outputs
= machine
->Inputs
;
129 machine
->Outputs
= ALIGN16_ASSIGN(outputs
);
133 draw
->vertex_fetch
.fetch_func( draw
, machine
, elts
, count
);
135 if (!draw
->rasterizer
->bypass_vs
) {
137 gallivm_cpu_vs_exec(shader
->llvm_prog
,
144 /* store machine results */
145 for (j
= 0; j
< count
; j
++) {
149 x
= vOut
[j
]->clip
[0] = machine
->Outputs
[0].xyzw
[0].f
[j
];
150 y
= vOut
[j
]->clip
[1] = machine
->Outputs
[0].xyzw
[1].f
[j
];
151 z
= vOut
[j
]->clip
[2] = machine
->Outputs
[0].xyzw
[2].f
[j
];
152 w
= vOut
[j
]->clip
[3] = machine
->Outputs
[0].xyzw
[3].f
[j
];
154 if (!draw
->rasterizer
->bypass_clipping
) {
155 vOut
[j
]->clipmask
= compute_clipmask(vOut
[j
]->clip
, draw
->plane
, draw
->nr_planes
);
164 vOut
[j
]->clipmask
= 0;
166 vOut
[j
]->edgeflag
= 1;
168 if (!draw
->identity_viewport
) {
169 /* Viewport mapping */
170 vOut
[j
]->data
[0][0] = x
* scale
[0] + trans
[0];
171 vOut
[j
]->data
[0][1] = y
* scale
[1] + trans
[1];
172 vOut
[j
]->data
[0][2] = z
* scale
[2] + trans
[2];
173 vOut
[j
]->data
[0][3] = w
;
176 vOut
[j
]->data
[0][0] = x
;
177 vOut
[j
]->data
[0][1] = y
;
178 vOut
[j
]->data
[0][2] = z
;
179 vOut
[j
]->data
[0][3] = w
;
182 /* Remaining attributes are packed into sequential post-transform
183 * vertex attrib slots.
185 for (slot
= 1; slot
< draw
->num_vs_outputs
; slot
++) {
186 vOut
[j
]->data
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
187 vOut
[j
]->data
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
188 vOut
[j
]->data
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
189 vOut
[j
]->data
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
191 } /* loop over vertices */
195 vs_llvm_delete( struct draw_vertex_shader
*base
)
197 struct draw_llvm_vertex_shader
*shader
=
198 (struct draw_llvm_vertex_shader
*)base
;
200 /* Do something to free compiled shader:
203 FREE( (void*) shader
->base
.state
.tokens
);
210 struct draw_vertex_shader
*
211 draw_create_vs_llvm(struct draw_context
*draw
,
212 const struct pipe_shader_state
*templ
)
214 struct draw_llvm_vertex_shader
*vs
;
215 uint nt
= tgsi_num_tokens(templ
->tokens
);
217 vs
= CALLOC_STRUCT( draw_llvm_vertex_shader
);
221 /* we make a private copy of the tokens */
222 vs
->base
.state
.tokens
= mem_dup(templ
->tokens
, nt
* sizeof(templ
->tokens
[0]));
223 vs
->base
.prepare
= vs_llvm_prepare
;
224 vs
->base
.run
= vs_llvm_run
;
225 vs
->base
.delete = vs_llvm_delete
;
228 struct gallivm_ir
*ir
= gallivm_ir_new(GALLIVM_VS
);
229 gallivm_ir_set_layout(ir
, GALLIVM_SOA
);
230 gallivm_ir_set_components(ir
, 4);
231 gallivm_ir_fill_from_tgsi(ir
, vs
->base
.state
->tokens
);
232 vs
->llvm_prog
= gallivm_ir_compile(ir
);
233 gallivm_ir_delete(ir
);
236 draw
->engine
= gallivm_global_cpu_engine();
238 /* XXX: Why are there two versions of this? Shouldn't creating the
239 * engine be a separate operation to compiling a shader?
242 draw
->engine
= gallivm_cpu_engine_create(vs
->llvm_prog
);
245 gallivm_cpu_jit_compile(draw
->engine
, vs
->llvm_prog
);
257 struct draw_vertex_shader
*
258 draw_create_vs_llvm(struct draw_context
*draw
,
259 const struct pipe_shader_state
*shader
)