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