1 /*
2    Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3    Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4    develop this 3D driver.
5 
6    Permission is hereby granted, free of charge, to any person obtaining
7    a 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, sublicense, 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:
13 
14    The above copyright notice and this permission notice (including the
15    next paragraph) shall be included in all copies or substantial
16    portions of the Software.
17 
18    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21    IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 
26  **********************************************************************/
27 /*
28  * Authors:
29  *   Keith Whitwell <keith@tungstengraphics.com>
30  */
31 
32 #include "cairoint.h"
33 #include "cairo-drm-intel-brw-eu.h"
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 
40 /* How does predicate control work when execution_size != 8?  Do I
41  * need to test/set for 0xffff when execution_size is 16?
42  */
brw_set_predicate_control_flag_value(struct brw_compile * p,uint32_t value)43 void brw_set_predicate_control_flag_value( struct brw_compile *p, uint32_t value )
44 {
45     p->current->header.predicate_control = BRW_PREDICATE_NONE;
46 
47     if (value != 0xff) {
48 	if (value != p->flag_value) {
49 	    brw_push_insn_state(p);
50 	    brw_MOV(p, brw_flag_reg(), brw_imm_uw(value));
51 	    p->flag_value = value;
52 	    brw_pop_insn_state(p);
53 	}
54 
55 	p->current->header.predicate_control = BRW_PREDICATE_NORMAL;
56     }
57 }
58 
brw_set_predicate_control(struct brw_compile * p,uint32_t pc)59 void brw_set_predicate_control( struct brw_compile *p, uint32_t pc )
60 {
61     p->current->header.predicate_control = pc;
62 }
63 
brw_set_conditionalmod(struct brw_compile * p,uint32_t conditional)64 void brw_set_conditionalmod( struct brw_compile *p, uint32_t conditional )
65 {
66     p->current->header.destreg__conditonalmod = conditional;
67 }
68 
brw_set_access_mode(struct brw_compile * p,uint32_t access_mode)69 void brw_set_access_mode( struct brw_compile *p, uint32_t access_mode )
70 {
71     p->current->header.access_mode = access_mode;
72 }
73 
brw_set_compression_control(struct brw_compile * p,int compression_control)74 void brw_set_compression_control( struct brw_compile *p, int compression_control )
75 {
76     p->current->header.compression_control = compression_control;
77 }
78 
brw_set_mask_control(struct brw_compile * p,uint32_t value)79 void brw_set_mask_control( struct brw_compile *p, uint32_t value )
80 {
81     p->current->header.mask_control = value;
82 }
83 
brw_set_saturate(struct brw_compile * p,uint32_t value)84 void brw_set_saturate( struct brw_compile *p, uint32_t value )
85 {
86     p->current->header.saturate = value;
87 }
88 
brw_push_insn_state(struct brw_compile * p)89 void brw_push_insn_state( struct brw_compile *p )
90 {
91     assert(p->current != &p->stack[BRW_EU_MAX_INSN_STACK-1]);
92     memcpy(p->current+1, p->current, sizeof(struct brw_instruction));
93     p->current++;
94 }
95 
brw_pop_insn_state(struct brw_compile * p)96 void brw_pop_insn_state( struct brw_compile *p )
97 {
98     assert(p->current != p->stack);
99     p->current--;
100 }
101 
102 /************************************************************************/
103 void
brw_compile_init(struct brw_compile * p,cairo_bool_t is_g4x)104 brw_compile_init (struct brw_compile *p,
105 		  cairo_bool_t is_g4x)
106 {
107     p->nr_insn = 0;
108     p->current = p->stack;
109     memset (p->current, 0, sizeof (p->current[0]));
110 
111     p->is_g4x = is_g4x;
112 
113     /* Some defaults?  */
114     brw_set_mask_control (p, BRW_MASK_ENABLE); /* what does this do? */
115     brw_set_saturate (p, 0);
116     brw_set_compression_control (p, BRW_COMPRESSION_NONE);
117     brw_set_predicate_control_flag_value (p, 0xff);
118 }
119 
120 const uint32_t *
brw_get_program(struct brw_compile * p,uint32_t * sz)121 brw_get_program (struct brw_compile *p,
122 		 uint32_t *sz)
123 {
124     *sz = p->nr_insn * sizeof (struct brw_instruction);
125     return (const uint32_t *)p->store;
126 }
127 
128 
129 
130 /*
131  * Subroutine calls require special attention.
132  * Mesa instructions may be expanded into multiple hardware instructions
133  * so the prog_instruction::BranchTarget field can't be used as an index
134  * into the hardware instructions.
135  *
136  * The BranchTarget field isn't needed, however.  Mesa's GLSL compiler
137  * emits CAL and BGNSUB instructions with labels that can be used to map
138  * subroutine calls to actual subroutine code blocks.
139  *
140  * The structures and function here implement patching of CAL instructions
141  * so they jump to the right subroutine code...
142  */
143 
144 
145 /*
146  * For each OPCODE_BGNSUB we create one of these.
147  */
148 struct brw_glsl_label
149 {
150     const char *name; /*< the label string */
151     uint32_t position;  /*< the position of the brw instruction for this label */
152     struct brw_glsl_label *next;  /*< next in linked list */
153 };
154 
155 
156 /*
157  * For each OPCODE_CAL we create one of these.
158  */
159 struct brw_glsl_call
160 {
161     uint32_t call_inst_pos;  /*< location of the CAL instruction */
162     const char *sub_name;  /*< name of subroutine to call */
163     struct brw_glsl_call *next;  /*< next in linked list */
164 };
165 
166 
167 /*
168  * Called for each OPCODE_BGNSUB.
169  */
170     void
brw_save_label(struct brw_compile * c,const char * name,uint32_t position)171 brw_save_label(struct brw_compile *c, const char *name, uint32_t position)
172 {
173     struct brw_glsl_label *label = calloc(1, sizeof *label);
174     label->name = name;
175     label->position = position;
176     label->next = c->first_label;
177     c->first_label = label;
178 }
179 
180 
181 /*
182  * Called for each OPCODE_CAL.
183  */
184     void
brw_save_call(struct brw_compile * c,const char * name,uint32_t call_pos)185 brw_save_call(struct brw_compile *c, const char *name, uint32_t call_pos)
186 {
187     struct brw_glsl_call *call = calloc(1, sizeof *call);
188     call->call_inst_pos = call_pos;
189     call->sub_name = name;
190     call->next = c->first_call;
191     c->first_call = call;
192 }
193 
194 
195 /*
196  * Lookup a label, return label's position/offset.
197  */
198     static uint32_t
brw_lookup_label(struct brw_compile * c,const char * name)199 brw_lookup_label(struct brw_compile *c, const char *name)
200 {
201     const struct brw_glsl_label *label;
202     for (label = c->first_label; label; label = label->next) {
203 	if (strcmp(name, label->name) == 0) {
204 	    return label->position;
205 	}
206     }
207     abort();  /* should never happen */
208     return ~0;
209 }
210 
211 
212 /*
213  * When we're done generating code, this function is called to resolve
214  * subroutine calls.
215  */
216     void
brw_resolve_cals(struct brw_compile * c)217 brw_resolve_cals(struct brw_compile *c)
218 {
219     const struct brw_glsl_call *call;
220 
221     for (call = c->first_call; call; call = call->next) {
222 	const uint32_t sub_loc = brw_lookup_label(c, call->sub_name);
223 	struct brw_instruction *brw_call_inst = &c->store[call->call_inst_pos];
224 	struct brw_instruction *brw_sub_inst = &c->store[sub_loc];
225 	int32_t offset = brw_sub_inst - brw_call_inst;
226 
227 	/* patch brw_inst1 to point to brw_inst2 */
228 	brw_set_src1(brw_call_inst, brw_imm_d(offset * 16));
229     }
230 
231     /* free linked list of calls */
232     {
233 	struct brw_glsl_call *call, *next;
234 	for (call = c->first_call; call; call = next) {
235 	    next = call->next;
236 	    free(call);
237 	}
238 	c->first_call = NULL;
239     }
240 
241     /* free linked list of labels */
242     {
243 	struct brw_glsl_label *label, *next;
244 	for (label = c->first_label; label; label = next) {
245 	    next = label->next;
246 	    free(label);
247 	}
248 	c->first_label = NULL;
249     }
250 }
251