1 /*
2  * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3  *
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
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
13  * Software.
14  *
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22 
23 #ifndef RADEON_COMPILER_H
24 #define RADEON_COMPILER_H
25 
26 #include "memory_pool.h"
27 #include "radeon_code.h"
28 #include "radeon_program.h"
29 #include "radeon_emulate_loops.h"
30 
31 #define RC_DBG_LOG        (1 << 0)
32 #define RC_DBG_STATS      (1 << 1)
33 
34 struct rc_swizzle_caps;
35 
36 enum rc_program_type {
37 	RC_VERTEX_PROGRAM,
38 	RC_FRAGMENT_PROGRAM,
39 	RC_NUM_PROGRAM_TYPES
40 };
41 
42 struct radeon_compiler {
43 	struct memory_pool Pool;
44 	struct rc_program Program;
45 	const struct rc_regalloc_state *regalloc_state;
46 	enum rc_program_type type;
47 	unsigned Debug:2;
48 	unsigned Error:1;
49 	char * ErrorMsg;
50 
51 	/* Hardware specification. */
52 	unsigned is_r400:1;
53 	unsigned is_r500:1;
54 	unsigned has_half_swizzles:1;
55 	unsigned has_presub:1;
56 	unsigned has_omod:1;
57 	unsigned disable_optimizations:1;
58 	unsigned max_temp_regs;
59 	unsigned max_constants;
60 	int max_alu_insts;
61 	unsigned max_tex_insts;
62 
63 	/* Whether to remove unused constants and empty holes in constant space. */
64 	unsigned remove_unused_constants:1;
65 
66 	/**
67 	 * Variables used internally, not be touched by callers
68 	 * of the compiler
69 	 */
70 	/*@{*/
71 	const struct rc_swizzle_caps * SwizzleCaps;
72 	/*@}*/
73 
74 	struct emulate_loop_state loop_state;
75 
76 	unsigned initial_num_insts; /* Number of instructions at start. */
77 };
78 
79 void rc_init(struct radeon_compiler * c, const struct rc_regalloc_state *rs);
80 void rc_destroy(struct radeon_compiler * c);
81 
82 void rc_debug(struct radeon_compiler * c, const char * fmt, ...);
83 void rc_error(struct radeon_compiler * c, const char * fmt, ...);
84 
85 int rc_if_fail_helper(struct radeon_compiler * c, const char * file, int line, const char * assertion);
86 
87 /**
88  * This macro acts like an if-statement that can be used to implement
89  * non-aborting assertions in the compiler.
90  *
91  * It checks whether \p cond is true. If not, an internal compiler error is
92  * flagged and the if-clause is run.
93  *
94  * A typical use-case would be:
95  *
96  *  if (rc_assert(c, condition-that-must-be-true))
97  *  	return;
98  */
99 #define rc_assert(c, cond) \
100 	(!(cond) && rc_if_fail_helper(c, __FILE__, __LINE__, #cond))
101 
102 void rc_calculate_inputs_outputs(struct radeon_compiler * c);
103 
104 void rc_move_input(struct radeon_compiler * c, unsigned input, struct rc_src_register new_input);
105 void rc_move_output(struct radeon_compiler * c, unsigned output, unsigned new_output, unsigned writemask);
106 void rc_copy_output(struct radeon_compiler * c, unsigned output, unsigned dup_output);
107 void rc_transform_fragment_wpos(struct radeon_compiler * c, unsigned wpos, unsigned new_input,
108                                 int full_vtransform);
109 void rc_transform_fragment_face(struct radeon_compiler *c, unsigned face);
110 
111 struct r300_fragment_program_compiler {
112 	struct radeon_compiler Base;
113 	struct rX00_fragment_program_code *code;
114 	/* Optional transformations and features. */
115 	struct r300_fragment_program_external_state state;
116 	/* Register corresponding to the depthbuffer. */
117 	unsigned OutputDepth;
118 	/* Registers corresponding to the four colorbuffers. */
119 	unsigned OutputColor[4];
120 
121 	void * UserData;
122 	void (*AllocateHwInputs)(
123 		struct r300_fragment_program_compiler * c,
124 		void (*allocate)(void * data, unsigned input, unsigned hwreg),
125 		void * mydata);
126 };
127 
128 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c);
129 
130 struct r300_vertex_program_compiler {
131 	struct radeon_compiler Base;
132 	struct r300_vertex_program_code *code;
133 	uint32_t RequiredOutputs;
134 
135 	void * UserData;
136 	void (*SetHwInputOutput)(struct r300_vertex_program_compiler * c);
137 
138 };
139 
140 void r3xx_compile_vertex_program(struct r300_vertex_program_compiler* c);
141 void rc_vert_fc(struct radeon_compiler *compiler, void *user);
142 void r300_vertex_program_dump(struct radeon_compiler *compiler, void *user);
143 
144 struct radeon_compiler_pass {
145 	const char *name;	/* Name of the pass. */
146 	int dump;		/* Dump the program if Debug == 1? */
147 	int predicate;		/* Run this pass? */
148 	void (*run)(struct radeon_compiler *c, void *user); /* The main entrypoint. */
149 	void *user;		/* Optional parameter which is passed to the run function. */
150 };
151 
152 struct rc_program_stats {
153 	unsigned num_insts;
154 	unsigned num_fc_insts;
155 	unsigned num_tex_insts;
156 	unsigned num_rgb_insts;
157 	unsigned num_alpha_insts;
158 	unsigned num_presub_ops;
159 	unsigned num_temp_regs;
160 	unsigned num_omod_ops;
161 	unsigned num_inline_literals;
162 };
163 
164 void rc_get_stats(struct radeon_compiler *c, struct rc_program_stats *s);
165 
166 /* Executes a list of compiler passes given in the parameter 'list'. */
167 void rc_run_compiler_passes(struct radeon_compiler *c, struct radeon_compiler_pass *list);
168 void rc_run_compiler(struct radeon_compiler *c, struct radeon_compiler_pass *list);
169 void rc_validate_final_shader(struct radeon_compiler *c, void *user);
170 
171 #endif /* RADEON_COMPILER_H */
172