1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#ifndef SVGA_TGSI_EMIT_H
27bf215546Sopenharmony_ci#define SVGA_TGSI_EMIT_H
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "tgsi/tgsi_scan.h"
30bf215546Sopenharmony_ci#include "svga_hw_reg.h"
31bf215546Sopenharmony_ci#include "svga_shader.h"
32bf215546Sopenharmony_ci#include "svga_tgsi.h"
33bf215546Sopenharmony_ci#include "svga3d_shaderdefs.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistruct src_register
36bf215546Sopenharmony_ci{
37bf215546Sopenharmony_ci   SVGA3dShaderSrcToken base;
38bf215546Sopenharmony_ci   SVGA3dShaderSrcToken indirect;
39bf215546Sopenharmony_ci};
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistruct svga_arl_consts
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci   int number;
45bf215546Sopenharmony_ci   int idx;
46bf215546Sopenharmony_ci   int swizzle;
47bf215546Sopenharmony_ci   int arl_num;
48bf215546Sopenharmony_ci};
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/**
52bf215546Sopenharmony_ci * This is the context/state used during TGSI->SVGA shader translation.
53bf215546Sopenharmony_ci */
54bf215546Sopenharmony_cistruct svga_shader_emitter
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   unsigned size;
57bf215546Sopenharmony_ci   char *buf;
58bf215546Sopenharmony_ci   char *ptr;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   struct svga_compile_key key;
61bf215546Sopenharmony_ci   struct tgsi_shader_info info;
62bf215546Sopenharmony_ci   int unit;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   int imm_start;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   int nr_hw_float_const;
67bf215546Sopenharmony_ci   int nr_hw_int_const;
68bf215546Sopenharmony_ci   int nr_hw_temp;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   int insn_offset;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   int internal_temp_count;
73bf215546Sopenharmony_ci   int internal_imm_count;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   int internal_color_idx[2]; /* diffuse, specular */
76bf215546Sopenharmony_ci   int internal_color_count;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   boolean emitted_vface;
79bf215546Sopenharmony_ci   boolean emit_frontface;
80bf215546Sopenharmony_ci   int internal_frontface_idx;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   int ps30_input_count;
83bf215546Sopenharmony_ci   int vs30_output_count;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   int dynamic_branching_level;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   unsigned num_output_writes;
88bf215546Sopenharmony_ci   boolean constant_color_output;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   boolean in_main_func;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   boolean created_common_immediate;
93bf215546Sopenharmony_ci   int common_immediate_idx[2];
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   boolean created_loop_const;
96bf215546Sopenharmony_ci   int loop_const_idx;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   unsigned inverted_texcoords;  /**< bitmask of which texcoords are flipped */
99bf215546Sopenharmony_ci   struct src_register ps_true_texcoord[PIPE_MAX_ATTRIBS];
100bf215546Sopenharmony_ci   struct src_register ps_inverted_texcoord[PIPE_MAX_ATTRIBS];
101bf215546Sopenharmony_ci   unsigned ps_inverted_texcoord_input[PIPE_MAX_ATTRIBS];
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   unsigned label[32];
104bf215546Sopenharmony_ci   unsigned nr_labels;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   /** input/output register mappings, indexed by register number */
107bf215546Sopenharmony_ci   struct src_register input_map[PIPE_MAX_ATTRIBS];
108bf215546Sopenharmony_ci   SVGA3dShaderDestToken output_map[PIPE_MAX_ATTRIBS];
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   boolean ps_reads_pos;
111bf215546Sopenharmony_ci   boolean emitted_depth_fog;
112bf215546Sopenharmony_ci   struct src_register ps_true_pos;
113bf215546Sopenharmony_ci   struct src_register ps_depth_pos;
114bf215546Sopenharmony_ci   SVGA3dShaderDestToken ps_temp_pos;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   /* shared input for depth and fog */
117bf215546Sopenharmony_ci   struct src_register ps_depth_fog;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   struct src_register imm_0055;
120bf215546Sopenharmony_ci   SVGA3dShaderDestToken temp_pos;
121bf215546Sopenharmony_ci   SVGA3dShaderDestToken true_pos;
122bf215546Sopenharmony_ci   SVGA3dShaderDestToken depth_pos;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   /* shared output for depth and fog */
125bf215546Sopenharmony_ci   SVGA3dShaderDestToken vs_depth_fog;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   /* PS output colors (indexed by color semantic index) */
128bf215546Sopenharmony_ci   SVGA3dShaderDestToken temp_color_output[PIPE_MAX_COLOR_BUFS];
129bf215546Sopenharmony_ci   SVGA3dShaderDestToken true_color_output[PIPE_MAX_COLOR_BUFS];
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   SVGA3dShaderDestToken temp_psiz;
132bf215546Sopenharmony_ci   SVGA3dShaderDestToken true_psiz;
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   struct svga_arl_consts arl_consts[12];
135bf215546Sopenharmony_ci   int num_arl_consts;
136bf215546Sopenharmony_ci   int current_arl;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   unsigned pstipple_sampler_unit;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   int num_samplers;
141bf215546Sopenharmony_ci   uint8_t sampler_target[PIPE_MAX_SAMPLERS];
142bf215546Sopenharmony_ci};
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ciboolean
146bf215546Sopenharmony_cisvga_shader_emit_dword(struct svga_shader_emitter *emit, unsigned dword);
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ciboolean
149bf215546Sopenharmony_cisvga_shader_emit_dwords(struct svga_shader_emitter *emit,
150bf215546Sopenharmony_ci                        const unsigned *dwords, unsigned nr);
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ciboolean
153bf215546Sopenharmony_cisvga_shader_emit_opcode(struct svga_shader_emitter *emit,
154bf215546Sopenharmony_ci                        unsigned opcode);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ciboolean
157bf215546Sopenharmony_cisvga_shader_emit_instructions(struct svga_shader_emitter *emit,
158bf215546Sopenharmony_ci                              const struct tgsi_token *tokens);
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ciboolean
161bf215546Sopenharmony_cisvga_shader_emit_samplers_decl(struct svga_shader_emitter *emit);
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ciboolean
164bf215546Sopenharmony_cisvga_translate_decl_sm30(struct svga_shader_emitter *emit,
165bf215546Sopenharmony_ci                         const struct tgsi_full_declaration *decl);
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci#define TRANSLATE_SWIZZLE(x,y,z,w)  ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
169bf215546Sopenharmony_ci#define SWIZZLE_XYZW  \
170bf215546Sopenharmony_ci TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
171bf215546Sopenharmony_ci#define SWIZZLE_XXXX  \
172bf215546Sopenharmony_ci TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
173bf215546Sopenharmony_ci#define SWIZZLE_YYYY  \
174bf215546Sopenharmony_ci TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
175bf215546Sopenharmony_ci#define SWIZZLE_ZZZZ  \
176bf215546Sopenharmony_ci TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
177bf215546Sopenharmony_ci#define SWIZZLE_WWWW  \
178bf215546Sopenharmony_ci TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci/** Emit the given SVGA3dShaderInstToken opcode */
182bf215546Sopenharmony_cistatic inline boolean
183bf215546Sopenharmony_ciemit_instruction(struct svga_shader_emitter *emit,
184bf215546Sopenharmony_ci                 SVGA3dShaderInstToken opcode)
185bf215546Sopenharmony_ci{
186bf215546Sopenharmony_ci   return svga_shader_emit_opcode(emit, opcode.value);
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci/** Generate a SVGA3dShaderInstToken for the given SVGA3D shader opcode */
191bf215546Sopenharmony_cistatic inline SVGA3dShaderInstToken
192bf215546Sopenharmony_ciinst_token(SVGA3dShaderOpCodeType opcode)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   SVGA3dShaderInstToken inst;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   inst.value = 0;
197bf215546Sopenharmony_ci   inst.op = opcode;
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   return inst;
200bf215546Sopenharmony_ci}
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci/**
204bf215546Sopenharmony_ci * Generate a SVGA3dShaderInstToken for the given SVGA3D shader opcode
205bf215546Sopenharmony_ci * with the predication flag set.
206bf215546Sopenharmony_ci */
207bf215546Sopenharmony_cistatic inline SVGA3dShaderInstToken
208bf215546Sopenharmony_ciinst_token_predicated(SVGA3dShaderOpCodeType opcode)
209bf215546Sopenharmony_ci{
210bf215546Sopenharmony_ci   SVGA3dShaderInstToken inst;
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   inst.value = 0;
213bf215546Sopenharmony_ci   inst.op = opcode;
214bf215546Sopenharmony_ci   inst.predicated = 1;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   return inst;
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci/**
221bf215546Sopenharmony_ci * Generate a SVGA3dShaderInstToken for a SETP instruction (set predicate)
222bf215546Sopenharmony_ci * using the given comparison operator (one of SVGA3DOPCOMP_xx).
223bf215546Sopenharmony_ci */
224bf215546Sopenharmony_cistatic inline SVGA3dShaderInstToken
225bf215546Sopenharmony_ciinst_token_setp(SVGA3dShaderOpCodeCompFnType operator)
226bf215546Sopenharmony_ci{
227bf215546Sopenharmony_ci   SVGA3dShaderInstToken inst;
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci   inst.value = 0;
230bf215546Sopenharmony_ci   inst.op = SVGA3DOP_SETP;
231bf215546Sopenharmony_ci   inst.control = operator;
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci   return inst;
234bf215546Sopenharmony_ci}
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci/**
238bf215546Sopenharmony_ci * Create an instance of a SVGA3dShaderDestToken.
239bf215546Sopenharmony_ci * Note that this function is used to create tokens for output registers,
240bf215546Sopenharmony_ci * temp registers AND constants (see emit_def_const()).
241bf215546Sopenharmony_ci */
242bf215546Sopenharmony_cistatic inline SVGA3dShaderDestToken
243bf215546Sopenharmony_cidst_register(SVGA3dShaderRegType file, int number)
244bf215546Sopenharmony_ci{
245bf215546Sopenharmony_ci   SVGA3dShaderDestToken dest;
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   /* check values against bitfield sizes */
248bf215546Sopenharmony_ci   assert(number < (1 << 11));
249bf215546Sopenharmony_ci   assert(file <= SVGA3DREG_PREDICATE);
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci   dest.value = 0;
252bf215546Sopenharmony_ci   dest.num = number;
253bf215546Sopenharmony_ci   dest.type_upper = file >> 3;
254bf215546Sopenharmony_ci   dest.relAddr = 0;
255bf215546Sopenharmony_ci   dest.reserved1 = 0;
256bf215546Sopenharmony_ci   dest.mask = 0xf;
257bf215546Sopenharmony_ci   dest.dstMod = 0;
258bf215546Sopenharmony_ci   dest.shfScale = 0;
259bf215546Sopenharmony_ci   dest.type_lower = file & 0x7;
260bf215546Sopenharmony_ci   dest.reserved0 = 1;          /* is_reg */
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   return dest;
263bf215546Sopenharmony_ci}
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci/**
267bf215546Sopenharmony_ci * Apply a writemask to the given SVGA3dShaderDestToken, returning a
268bf215546Sopenharmony_ci * new SVGA3dShaderDestToken.
269bf215546Sopenharmony_ci */
270bf215546Sopenharmony_cistatic inline SVGA3dShaderDestToken
271bf215546Sopenharmony_ciwritemask(SVGA3dShaderDestToken dest, unsigned mask)
272bf215546Sopenharmony_ci{
273bf215546Sopenharmony_ci   assert(dest.mask & mask);
274bf215546Sopenharmony_ci   dest.mask &= mask;
275bf215546Sopenharmony_ci   return dest;
276bf215546Sopenharmony_ci}
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci/** Create a SVGA3dShaderSrcToken given a register file and number */
280bf215546Sopenharmony_cistatic inline SVGA3dShaderSrcToken
281bf215546Sopenharmony_cisrc_token(SVGA3dShaderRegType file, int number)
282bf215546Sopenharmony_ci{
283bf215546Sopenharmony_ci   SVGA3dShaderSrcToken src;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   /* check values against bitfield sizes */
286bf215546Sopenharmony_ci   assert(number < (1 << 11));
287bf215546Sopenharmony_ci   assert(file <= SVGA3DREG_PREDICATE);
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci   src.value = 0;
290bf215546Sopenharmony_ci   src.num = number;
291bf215546Sopenharmony_ci   src.type_upper = file >> 3;
292bf215546Sopenharmony_ci   src.relAddr = 0;
293bf215546Sopenharmony_ci   src.reserved1 = 0;
294bf215546Sopenharmony_ci   src.swizzle = SWIZZLE_XYZW;
295bf215546Sopenharmony_ci   src.srcMod = 0;
296bf215546Sopenharmony_ci   src.type_lower = file & 0x7;
297bf215546Sopenharmony_ci   src.reserved0 = 1;           /* is_reg */
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci   return src;
300bf215546Sopenharmony_ci}
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci/** Create a src_register given a register file and register number */
304bf215546Sopenharmony_cistatic inline struct src_register
305bf215546Sopenharmony_cisrc_register(SVGA3dShaderRegType file, int number)
306bf215546Sopenharmony_ci{
307bf215546Sopenharmony_ci   struct src_register src;
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   src.base = src_token(file, number);
310bf215546Sopenharmony_ci   src.indirect.value = 0;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   return src;
313bf215546Sopenharmony_ci}
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci/** Translate src_register into SVGA3dShaderDestToken */
316bf215546Sopenharmony_cistatic inline SVGA3dShaderDestToken
317bf215546Sopenharmony_cidst(struct src_register src)
318bf215546Sopenharmony_ci{
319bf215546Sopenharmony_ci   return dst_register(SVGA3dShaderGetRegType(src.base.value), src.base.num);
320bf215546Sopenharmony_ci}
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci/** Translate SVGA3dShaderDestToken to a src_register */
324bf215546Sopenharmony_cistatic inline struct src_register
325bf215546Sopenharmony_cisrc(SVGA3dShaderDestToken dst)
326bf215546Sopenharmony_ci{
327bf215546Sopenharmony_ci   return src_register(SVGA3dShaderGetRegType(dst.value), dst.num);
328bf215546Sopenharmony_ci}
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci#endif
331