2 * Copyright 2011 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
30 #include <llvm-c/Core.h>
31 #include "gallivm/lp_bld_init.h"
32 #include "gallivm/lp_bld_tgsi.h"
34 #define RADEON_LLVM_MAX_INPUTS 32 * 4
35 #define RADEON_LLVM_MAX_OUTPUTS 32 * 4
36 #define RADEON_LLVM_MAX_BRANCH_DEPTH 16
37 #define RADEON_LLVM_MAX_LOOP_DEPTH 16
39 #define RADEON_LLVM_MAX_SYSTEM_VALUES 4
41 struct radeon_llvm_branch
{
42 LLVMBasicBlockRef endif_block
;
43 LLVMBasicBlockRef if_block
;
44 LLVMBasicBlockRef else_block
;
48 struct radeon_llvm_loop
{
49 LLVMBasicBlockRef loop_block
;
50 LLVMBasicBlockRef endloop_block
;
53 struct radeon_llvm_context
{
55 struct lp_build_tgsi_soa_context soa
;
62 struct r600_shader_io
* r600_inputs
;
63 struct r600_shader_io
* r600_outputs
;
64 struct pipe_stream_output_info
*stream_outputs
;
65 unsigned color_buffer_count
;
66 unsigned fs_color_all
;
67 unsigned alpha_to_one
;
69 /*=== Front end configuration ===*/
71 /* Special Intrinsics */
73 /** Write to an output register: float store_output(float, i32) */
74 const char * store_output_intr
;
76 /** Swizzle a vector value: <4 x float> swizzle(<4 x float>, i32)
77 * The swizzle is an unsigned integer that encodes a TGSI_SWIZZLE_* value
79 * Swizzle{0-1} = X Channel
80 * Swizzle{2-3} = Y Channel
81 * Swizzle{4-5} = Z Channel
82 * Swizzle{6-7} = W Channel
84 const char * swizzle_intr
;
86 /* Instructions that are not described by any of the TGSI opcodes. */
88 /** This function is responsible for initilizing the inputs array and will be
89 * called once for each input declared in the TGSI shader.
91 void (*load_input
)(struct radeon_llvm_context
*,
93 const struct tgsi_full_declaration
*decl
);
95 void (*load_system_value
)(struct radeon_llvm_context
*,
97 const struct tgsi_full_declaration
*decl
);
99 /** User data to use with the callbacks */
102 /** This array contains the input values for the shader. Typically these
103 * values will be in the form of a target intrinsic that will inform the
104 * backend how to load the actual inputs to the shader.
106 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
];
107 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
][TGSI_NUM_CHANNELS
];
108 unsigned output_reg_count
;
110 LLVMValueRef system_values
[RADEON_LLVM_MAX_SYSTEM_VALUES
];
112 /*=== Private Members ===*/
114 struct radeon_llvm_branch branch
[RADEON_LLVM_MAX_BRANCH_DEPTH
];
115 struct radeon_llvm_loop loop
[RADEON_LLVM_MAX_LOOP_DEPTH
];
117 unsigned branch_depth
;
121 LLVMValueRef main_fn
;
123 struct gallivm_state gallivm
;
126 static inline LLVMValueRef
bitcast(
127 struct lp_build_tgsi_context
* bld_base
,
128 enum tgsi_opcode_type type
,
132 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
133 LLVMContextRef ctx
= bld_base
->base
.gallivm
->context
;
134 LLVMTypeRef dst_type
;
137 case TGSI_TYPE_UNSIGNED
:
138 case TGSI_TYPE_SIGNED
:
139 dst_type
= LLVMInt32TypeInContext(ctx
);
141 case TGSI_TYPE_UNTYPED
:
142 case TGSI_TYPE_FLOAT
:
143 dst_type
= LLVMFloatTypeInContext(ctx
);
151 return LLVMBuildBitCast(builder
, value
, dst_type
, "");
157 void radeon_llvm_emit_prepare_cube_coords(struct lp_build_tgsi_context
* bld_base
,
158 struct lp_build_emit_data
* emit_data
,
159 LLVMValueRef
*coords_arg
);
161 void radeon_llvm_context_init(struct radeon_llvm_context
* ctx
);
163 void radeon_llvm_create_func(struct radeon_llvm_context
* ctx
,
164 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
);
166 void radeon_llvm_dispose(struct radeon_llvm_context
* ctx
);
168 inline static struct radeon_llvm_context
* radeon_llvm_context(
169 struct lp_build_tgsi_context
* bld_base
)
171 return (struct radeon_llvm_context
*)bld_base
;
174 unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
);
176 void radeon_llvm_finalize_module(struct radeon_llvm_context
* ctx
);
179 build_intrinsic(LLVMBuilderRef builder
,
181 LLVMTypeRef ret_type
,
187 build_tgsi_intrinsic_nomem(
188 const struct lp_build_tgsi_action
* action
,
189 struct lp_build_tgsi_context
* bld_base
,
190 struct lp_build_emit_data
* emit_data
);
194 #endif /* RADEON_LLVM_H */