1848b8605Smrg/**********************************************************
2848b8605Smrg * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person
5848b8605Smrg * obtaining a copy of this software and associated documentation
6848b8605Smrg * files (the "Software"), to deal in the Software without
7848b8605Smrg * restriction, including without limitation the rights to use, copy,
8848b8605Smrg * modify, merge, publish, distribute, sublicense, and/or sell copies
9848b8605Smrg * of the Software, and to permit persons to whom the Software is
10848b8605Smrg * furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice and this permission notice shall be
13848b8605Smrg * included in all copies or substantial portions of the Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16848b8605Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18848b8605Smrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19848b8605Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20848b8605Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21848b8605Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22848b8605Smrg * SOFTWARE.
23848b8605Smrg *
24848b8605Smrg **********************************************************/
25848b8605Smrg
26848b8605Smrg
27848b8605Smrg#include "pipe/p_compiler.h"
28848b8605Smrg#include "pipe/p_shader_tokens.h"
29848b8605Smrg#include "pipe/p_defines.h"
30848b8605Smrg#include "tgsi/tgsi_parse.h"
31848b8605Smrg#include "tgsi/tgsi_dump.h"
32848b8605Smrg#include "tgsi/tgsi_scan.h"
33848b8605Smrg#include "util/u_math.h"
34848b8605Smrg#include "util/u_memory.h"
35848b8605Smrg#include "util/u_bitmask.h"
36848b8605Smrg
37848b8605Smrg#include "svgadump/svga_shader_dump.h"
38848b8605Smrg
39848b8605Smrg#include "svga_context.h"
40b8e80941Smrg#include "svga_shader.h"
41848b8605Smrg#include "svga_tgsi.h"
42848b8605Smrg#include "svga_tgsi_emit.h"
43848b8605Smrg#include "svga_debug.h"
44848b8605Smrg
45848b8605Smrg#include "svga_hw_reg.h"
46848b8605Smrg#include "svga3d_shaderdefs.h"
47848b8605Smrg
48848b8605Smrg
49848b8605Smrg/* Sinkhole used only in error conditions.
50848b8605Smrg */
51848b8605Smrgstatic char err_buf[128];
52848b8605Smrg
53848b8605Smrg
54848b8605Smrgstatic boolean
55848b8605Smrgsvga_shader_expand(struct svga_shader_emitter *emit)
56848b8605Smrg{
57848b8605Smrg   char *new_buf;
58848b8605Smrg   unsigned newsize = emit->size * 2;
59848b8605Smrg
60848b8605Smrg   if (emit->buf != err_buf)
61848b8605Smrg      new_buf = REALLOC(emit->buf, emit->size, newsize);
62848b8605Smrg   else
63848b8605Smrg      new_buf = NULL;
64848b8605Smrg
65b8e80941Smrg   if (!new_buf) {
66848b8605Smrg      emit->ptr = err_buf;
67848b8605Smrg      emit->buf = err_buf;
68848b8605Smrg      emit->size = sizeof(err_buf);
69848b8605Smrg      return FALSE;
70848b8605Smrg   }
71848b8605Smrg
72848b8605Smrg   emit->size = newsize;
73848b8605Smrg   emit->ptr = new_buf + (emit->ptr - emit->buf);
74848b8605Smrg   emit->buf = new_buf;
75848b8605Smrg   return TRUE;
76848b8605Smrg}
77848b8605Smrg
78848b8605Smrg
79b8e80941Smrgstatic inline boolean
80848b8605Smrgreserve(struct svga_shader_emitter *emit, unsigned nr_dwords)
81848b8605Smrg{
82848b8605Smrg   if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
83848b8605Smrg      if (!svga_shader_expand(emit)) {
84848b8605Smrg         return FALSE;
85848b8605Smrg      }
86848b8605Smrg   }
87848b8605Smrg
88848b8605Smrg   return TRUE;
89848b8605Smrg}
90848b8605Smrg
91848b8605Smrg
92848b8605Smrgboolean
93848b8605Smrgsvga_shader_emit_dword(struct svga_shader_emitter * emit, unsigned dword)
94848b8605Smrg{
95848b8605Smrg   if (!reserve(emit, 1))
96848b8605Smrg      return FALSE;
97848b8605Smrg
98848b8605Smrg   *(unsigned *) emit->ptr = dword;
99848b8605Smrg   emit->ptr += sizeof dword;
100848b8605Smrg   return TRUE;
101848b8605Smrg}
102848b8605Smrg
103848b8605Smrg
104848b8605Smrgboolean
105848b8605Smrgsvga_shader_emit_dwords(struct svga_shader_emitter * emit,
106848b8605Smrg                        const unsigned *dwords, unsigned nr)
107848b8605Smrg{
108848b8605Smrg   if (!reserve(emit, nr))
109848b8605Smrg      return FALSE;
110848b8605Smrg
111848b8605Smrg   memcpy(emit->ptr, dwords, nr * sizeof *dwords);
112848b8605Smrg   emit->ptr += nr * sizeof *dwords;
113848b8605Smrg   return TRUE;
114848b8605Smrg}
115848b8605Smrg
116848b8605Smrg
117848b8605Smrgboolean
118848b8605Smrgsvga_shader_emit_opcode(struct svga_shader_emitter * emit, unsigned opcode)
119848b8605Smrg{
120848b8605Smrg   SVGA3dShaderInstToken *here;
121848b8605Smrg
122848b8605Smrg   if (!reserve(emit, 1))
123848b8605Smrg      return FALSE;
124848b8605Smrg
125848b8605Smrg   here = (SVGA3dShaderInstToken *) emit->ptr;
126848b8605Smrg   here->value = opcode;
127848b8605Smrg
128848b8605Smrg   if (emit->insn_offset) {
129848b8605Smrg      SVGA3dShaderInstToken *prev =
130848b8605Smrg         (SVGA3dShaderInstToken *) (emit->buf + emit->insn_offset);
131848b8605Smrg      prev->size = (here - prev) - 1;
132848b8605Smrg   }
133848b8605Smrg
134848b8605Smrg   emit->insn_offset = emit->ptr - emit->buf;
135848b8605Smrg   emit->ptr += sizeof(unsigned);
136848b8605Smrg   return TRUE;
137848b8605Smrg}
138848b8605Smrg
139848b8605Smrg
140848b8605Smrgstatic boolean
141848b8605Smrgsvga_shader_emit_header(struct svga_shader_emitter *emit)
142848b8605Smrg{
143848b8605Smrg   SVGA3dShaderVersion header;
144848b8605Smrg
145848b8605Smrg   memset(&header, 0, sizeof header);
146848b8605Smrg
147848b8605Smrg   switch (emit->unit) {
148848b8605Smrg   case PIPE_SHADER_FRAGMENT:
149848b8605Smrg      header.value = SVGA3D_PS_30;
150848b8605Smrg      break;
151848b8605Smrg   case PIPE_SHADER_VERTEX:
152848b8605Smrg      header.value = SVGA3D_VS_30;
153848b8605Smrg      break;
154848b8605Smrg   }
155848b8605Smrg
156848b8605Smrg   return svga_shader_emit_dword(emit, header.value);
157848b8605Smrg}
158848b8605Smrg
159848b8605Smrg
160848b8605Smrg/**
161848b8605Smrg * Parse TGSI shader and translate to SVGA/DX9 serialized
162848b8605Smrg * representation.
163848b8605Smrg *
164848b8605Smrg * In this function SVGA shader is emitted to an in-memory buffer that
165848b8605Smrg * can be dynamically grown.  Once we've finished and know how large
166848b8605Smrg * it is, it will be copied to a hardware buffer for upload.
167848b8605Smrg */
168b8e80941Smrgstruct svga_shader_variant *
169b8e80941Smrgsvga_tgsi_vgpu9_translate(struct svga_context *svga,
170b8e80941Smrg                          const struct svga_shader *shader,
171b8e80941Smrg                          const struct svga_compile_key *key,
172b8e80941Smrg                          enum pipe_shader_type unit)
173848b8605Smrg{
174848b8605Smrg   struct svga_shader_variant *variant = NULL;
175848b8605Smrg   struct svga_shader_emitter emit;
176848b8605Smrg
177b8e80941Smrg   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_TGSIVGPU9TRANSLATE);
178b8e80941Smrg
179848b8605Smrg   memset(&emit, 0, sizeof(emit));
180848b8605Smrg
181848b8605Smrg   emit.size = 1024;
182848b8605Smrg   emit.buf = MALLOC(emit.size);
183848b8605Smrg   if (emit.buf == NULL) {
184848b8605Smrg      goto fail;
185848b8605Smrg   }
186848b8605Smrg
187848b8605Smrg   emit.ptr = emit.buf;
188848b8605Smrg   emit.unit = unit;
189848b8605Smrg   emit.key = *key;
190848b8605Smrg
191848b8605Smrg   tgsi_scan_shader(shader->tokens, &emit.info);
192848b8605Smrg
193848b8605Smrg   emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
194848b8605Smrg
195848b8605Smrg   if (unit == PIPE_SHADER_FRAGMENT)
196b8e80941Smrg      emit.imm_start += key->num_unnormalized_coords;
197848b8605Smrg
198848b8605Smrg   if (unit == PIPE_SHADER_VERTEX) {
199b8e80941Smrg      emit.imm_start += key->vs.need_prescale ? 2 : 0;
200848b8605Smrg   }
201848b8605Smrg
202848b8605Smrg   emit.nr_hw_float_const =
203848b8605Smrg      (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
204848b8605Smrg
205848b8605Smrg   emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
206848b8605Smrg
207848b8605Smrg   if (emit.nr_hw_temp >= SVGA3D_TEMPREG_MAX) {
208848b8605Smrg      debug_printf("svga: too many temporary registers (%u)\n",
209848b8605Smrg                   emit.nr_hw_temp);
210848b8605Smrg      goto fail;
211848b8605Smrg   }
212848b8605Smrg
213b8e80941Smrg   if (emit.info.indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
214b8e80941Smrg      debug_printf(
215b8e80941Smrg         "svga: indirect indexing of temporary registers is not supported.\n");
216b8e80941Smrg      goto fail;
217b8e80941Smrg   }
218b8e80941Smrg
219848b8605Smrg   emit.in_main_func = TRUE;
220848b8605Smrg
221848b8605Smrg   if (!svga_shader_emit_header(&emit)) {
222848b8605Smrg      debug_printf("svga: emit header failed\n");
223848b8605Smrg      goto fail;
224848b8605Smrg   }
225848b8605Smrg
226848b8605Smrg   if (!svga_shader_emit_instructions(&emit, shader->tokens)) {
227848b8605Smrg      debug_printf("svga: emit instructions failed\n");
228848b8605Smrg      goto fail;
229848b8605Smrg   }
230848b8605Smrg
231b8e80941Smrg   variant = svga_new_shader_variant(svga, unit);
232b8e80941Smrg   if (!variant)
233848b8605Smrg      goto fail;
234848b8605Smrg
235848b8605Smrg   variant->shader = shader;
236848b8605Smrg   variant->tokens = (const unsigned *) emit.buf;
237848b8605Smrg   variant->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
238848b8605Smrg   memcpy(&variant->key, key, sizeof(*key));
239848b8605Smrg   variant->id = UTIL_BITMASK_INVALID_INDEX;
240848b8605Smrg
241b8e80941Smrg   variant->pstipple_sampler_unit = emit.pstipple_sampler_unit;
242b8e80941Smrg
243b8e80941Smrg   /* If there was exactly one write to a fragment shader output register
244b8e80941Smrg    * and it came from a constant buffer, we know all fragments will have
245b8e80941Smrg    * the same color (except for blending).
246b8e80941Smrg    */
247b8e80941Smrg   variant->constant_color_output =
248b8e80941Smrg      emit.constant_color_output && emit.num_output_writes == 1;
249b8e80941Smrg
250b8e80941Smrg#if 0
251b8e80941Smrg   if (!svga_shader_verify(variant->tokens, variant->nr_tokens) ||
252b8e80941Smrg       SVGA_DEBUG & DEBUG_TGSI) {
253848b8605Smrg      debug_printf("#####################################\n");
254848b8605Smrg      debug_printf("Shader %u below\n", shader->id);
255848b8605Smrg      tgsi_dump(shader->tokens, 0);
256848b8605Smrg      if (SVGA_DEBUG & DEBUG_TGSI) {
257848b8605Smrg         debug_printf("Shader %u compiled below\n", shader->id);
258848b8605Smrg         svga_shader_dump(variant->tokens, variant->nr_tokens, FALSE);
259848b8605Smrg      }
260848b8605Smrg      debug_printf("#####################################\n");
261848b8605Smrg   }
262b8e80941Smrg#endif
263848b8605Smrg
264b8e80941Smrg   goto done;
265848b8605Smrg
266b8e80941Smrgfail:
267848b8605Smrg   FREE(variant);
268b8e80941Smrg   if (emit.buf != err_buf)
269b8e80941Smrg      FREE(emit.buf);
270b8e80941Smrg   variant = NULL;
271848b8605Smrg
272b8e80941Smrgdone:
273b8e80941Smrg   SVGA_STATS_TIME_POP(svga_sws(svga));
274b8e80941Smrg   return variant;
275848b8605Smrg}
276