44022b6e07771073f3dd4afd1c57da6a495720af
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"
43 #include "llvm/gallivm.h"
45 struct draw_llvm_vertex_shader
{
46 struct draw_vertex_shader base
;
47 struct gallivm_prog
*llvm_prog
;
51 static INLINE
unsigned
52 compute_clipmask(const float *clip
, /*const*/ float plane
[][4], unsigned nr
)
57 /* Do the hardwired planes first:
59 if (-clip
[0] + clip
[3] < 0) mask
|= CLIP_RIGHT_BIT
;
60 if ( clip
[0] + clip
[3] < 0) mask
|= CLIP_LEFT_BIT
;
61 if (-clip
[1] + clip
[3] < 0) mask
|= CLIP_TOP_BIT
;
62 if ( clip
[1] + clip
[3] < 0) mask
|= CLIP_BOTTOM_BIT
;
63 if (-clip
[2] + clip
[3] < 0) mask
|= CLIP_FAR_BIT
;
64 if ( clip
[2] + clip
[3] < 0) mask
|= CLIP_NEAR_BIT
;
66 /* Followed by any remaining ones:
68 for (i
= 6; i
< nr
; i
++) {
69 if (dot4(clip
, plane
[i
]) < 0)
79 vs_llvm_prepare( struct draw_vertex_shader
*base
,
80 struct draw_context
*draw
)
82 draw_update_vertex_fetch( draw
);
88 * Transform vertices with the current vertex program/shader
89 * Up to four vertices can be shaded at a time.
90 * \param vbuffer the input vertex data
91 * \param elts indexes of four input vertices
92 * \param count number of vertices to shade [1..4]
93 * \param vOut array of pointers to four output vertices
96 vs_llvm_run( struct draw_vertex_shader
*base
,
97 struct draw_context
*draw
,
100 struct vertex_header
*vOut
[] )
102 struct draw_llvm_vertex_shader
*shader
=
103 (struct draw_llvm_vertex_shader
*)base
;
105 struct tgsi_exec_machine
*machine
= &draw
->machine
;
108 ALIGN16_DECL(struct tgsi_exec_vector
, inputs
, PIPE_ATTRIB_MAX
);
109 ALIGN16_DECL(struct tgsi_exec_vector
, outputs
, PIPE_ATTRIB_MAX
);
110 const float *scale
= draw
->viewport
.scale
;
111 const float *trans
= draw
->viewport
.translate
;
115 assert(draw
->vertex_shader
->state
->output_semantic_name
[0]
116 == TGSI_SEMANTIC_POSITION
);
118 /* Consts does not require 16 byte alignment. */
119 machine
->Consts
= (float (*)[4]) draw
->user
.constants
;
121 machine
->Inputs
= ALIGN16_ASSIGN(inputs
);
122 machine
->Outputs
= ALIGN16_ASSIGN(outputs
);
124 draw
->vertex_fetch
.fetch_func( draw
, machine
, elts
, count
);
127 gallivm_cpu_vs_exec(shader
->llvm_prog
,
133 /* store machine results */
134 for (j
= 0; j
< count
; j
++) {
138 x
= vOut
[j
]->clip
[0] = machine
->Outputs
[0].xyzw
[0].f
[j
];
139 y
= vOut
[j
]->clip
[1] = machine
->Outputs
[0].xyzw
[1].f
[j
];
140 z
= vOut
[j
]->clip
[2] = machine
->Outputs
[0].xyzw
[2].f
[j
];
141 w
= vOut
[j
]->clip
[3] = machine
->Outputs
[0].xyzw
[3].f
[j
];
143 vOut
[j
]->clipmask
= compute_clipmask(vOut
[j
]->clip
, draw
->plane
, draw
->nr_planes
);
144 vOut
[j
]->edgeflag
= 1;
152 /* Viewport mapping */
153 vOut
[j
]->data
[0][0] = x
* scale
[0] + trans
[0];
154 vOut
[j
]->data
[0][1] = y
* scale
[1] + trans
[1];
155 vOut
[j
]->data
[0][2] = z
* scale
[2] + trans
[2];
156 vOut
[j
]->data
[0][3] = w
;
158 /* Remaining attributes are packed into sequential post-transform
159 * vertex attrib slots.
161 for (slot
= 1; slot
< draw
->num_vs_outputs
; slot
++) {
162 vOut
[j
]->data
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
163 vOut
[j
]->data
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
164 vOut
[j
]->data
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
165 vOut
[j
]->data
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
167 } /* loop over vertices */
171 vs_llvm_delete( struct draw_vertex_shader
*base
)
173 struct draw_llvm_vertex_shader
*shader
=
174 (struct draw_llvm_vertex_shader
*)base
;
176 /* Do something to free compiled shader:
185 struct draw_vertex_shader
*
186 draw_create_vs_llvm(struct draw_context
*draw
,
187 const struct pipe_shader_state
*templ
)
189 struct draw_llvm_vertex_shader
*vs
;
191 vs
= CALLOC_STRUCT( draw_llvm_vertex_shader
);
195 vs
->base
.state
= templ
;
196 vs
->base
.prepare
= vs_llvm_prepare
;
197 vs
->base
.run
= vs_llvm_run
;
198 vs
->base
.delete = vs_llvm_delete
;
201 struct gallivm_ir
*ir
= gallivm_ir_new(GALLIVM_VS
);
202 gallivm_ir_set_layout(ir
, GALLIVM_SOA
);
203 gallivm_ir_set_components(ir
, 4);
204 gallivm_ir_fill_from_tgsi(ir
, vs
->base
.state
->tokens
);
205 vs
->llvm_prog
= gallivm_ir_compile(ir
);
206 gallivm_ir_delete(ir
);
209 draw
->engine
= gallivm_global_cpu_engine();
211 /* XXX: Why are there two versions of this? Shouldn't creating the
212 * engine be a separate operation to compiling a shader?
215 draw
->engine
= gallivm_cpu_engine_create(vs
->llvm_prog
);
218 gallivm_cpu_jit_compile(draw
->engine
, vs
->llvm_prog
);
230 struct draw_vertex_shader
*
231 draw_create_vs_llvm(struct draw_context
*draw
,
232 const struct pipe_shader_state
*shader
)