blob: 0b2d8af64d99fd73daa54bfb51d9e3caadc05562 [file] [log] [blame]
Jakob Bornecrantz31926332009-11-16 19:56:18 +01001/**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27#include "pipe/p_compiler.h"
28#include "pipe/p_shader_tokens.h"
29#include "pipe/p_defines.h"
30#include "tgsi/tgsi_parse.h"
31#include "tgsi/tgsi_dump.h"
32#include "tgsi/tgsi_scan.h"
Brian Paul58ea42b2011-11-03 17:40:56 -060033#include "util/u_math.h"
Jakob Bornecrantz31926332009-11-16 19:56:18 +010034#include "util/u_memory.h"
José Fonsecacdb445f2010-01-03 00:47:30 +000035#include "util/u_bitmask.h"
Jakob Bornecrantz31926332009-11-16 19:56:18 +010036
José Fonseca178407f2009-11-24 14:37:45 +000037#include "svgadump/svga_shader_dump.h"
Jakob Bornecrantz31926332009-11-16 19:56:18 +010038
39#include "svga_context.h"
Brian Paule0542512015-08-13 11:00:58 -070040#include "svga_shader.h"
Jakob Bornecrantz31926332009-11-16 19:56:18 +010041#include "svga_tgsi.h"
42#include "svga_tgsi_emit.h"
43#include "svga_debug.h"
44
45#include "svga_hw_reg.h"
46#include "svga3d_shaderdefs.h"
47
48
49/* Sinkhole used only in error conditions.
50 */
51static char err_buf[128];
52
Jakob Bornecrantz31926332009-11-16 19:56:18 +010053
Brian Paul2e8c51c2013-06-19 10:39:43 -060054static boolean
55svga_shader_expand(struct svga_shader_emitter *emit)
Jakob Bornecrantz31926332009-11-16 19:56:18 +010056{
57 char *new_buf;
58 unsigned newsize = emit->size * 2;
59
Brian Paul2e8c51c2013-06-19 10:39:43 -060060 if (emit->buf != err_buf)
Jakob Bornecrantz31926332009-11-16 19:56:18 +010061 new_buf = REALLOC(emit->buf, emit->size, newsize);
62 else
63 new_buf = NULL;
64
Edward O'Callaghan13eb5f52015-12-04 22:08:22 +110065 if (!new_buf) {
Jakob Bornecrantz31926332009-11-16 19:56:18 +010066 emit->ptr = err_buf;
67 emit->buf = err_buf;
68 emit->size = sizeof(err_buf);
69 return FALSE;
70 }
71
72 emit->size = newsize;
73 emit->ptr = new_buf + (emit->ptr - emit->buf);
74 emit->buf = new_buf;
75 return TRUE;
Brian Paul2e8c51c2013-06-19 10:39:43 -060076}
Jakob Bornecrantz31926332009-11-16 19:56:18 +010077
Brian Paul2e8c51c2013-06-19 10:39:43 -060078
Ilia Mirkina2a1a582015-07-20 19:58:43 -040079static inline boolean
Brian Paul2e8c51c2013-06-19 10:39:43 -060080reserve(struct svga_shader_emitter *emit, unsigned nr_dwords)
Jakob Bornecrantz31926332009-11-16 19:56:18 +010081{
82 if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
Brian Paul2e8c51c2013-06-19 10:39:43 -060083 if (!svga_shader_expand(emit)) {
Jakob Bornecrantz31926332009-11-16 19:56:18 +010084 return FALSE;
Brian Paul2e8c51c2013-06-19 10:39:43 -060085 }
Jakob Bornecrantz31926332009-11-16 19:56:18 +010086 }
87
88 return TRUE;
89}
90
Brian Paul2e8c51c2013-06-19 10:39:43 -060091
92boolean
93svga_shader_emit_dword(struct svga_shader_emitter * emit, unsigned dword)
Jakob Bornecrantz31926332009-11-16 19:56:18 +010094{
95 if (!reserve(emit, 1))
96 return FALSE;
97
Brian Paul2e8c51c2013-06-19 10:39:43 -060098 *(unsigned *) emit->ptr = dword;
Jakob Bornecrantz31926332009-11-16 19:56:18 +010099 emit->ptr += sizeof dword;
100 return TRUE;
101}
102
Brian Paul2e8c51c2013-06-19 10:39:43 -0600103
104boolean
105svga_shader_emit_dwords(struct svga_shader_emitter * emit,
106 const unsigned *dwords, unsigned nr)
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100107{
108 if (!reserve(emit, nr))
109 return FALSE;
110
Brian Paul2e8c51c2013-06-19 10:39:43 -0600111 memcpy(emit->ptr, dwords, nr * sizeof *dwords);
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100112 emit->ptr += nr * sizeof *dwords;
113 return TRUE;
114}
115
Brian Paul2e8c51c2013-06-19 10:39:43 -0600116
117boolean
118svga_shader_emit_opcode(struct svga_shader_emitter * emit, unsigned opcode)
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100119{
120 SVGA3dShaderInstToken *here;
121
122 if (!reserve(emit, 1))
123 return FALSE;
124
Brian Paul2e8c51c2013-06-19 10:39:43 -0600125 here = (SVGA3dShaderInstToken *) emit->ptr;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100126 here->value = opcode;
127
128 if (emit->insn_offset) {
Brian Paul2e8c51c2013-06-19 10:39:43 -0600129 SVGA3dShaderInstToken *prev =
130 (SVGA3dShaderInstToken *) (emit->buf + emit->insn_offset);
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100131 prev->size = (here - prev) - 1;
132 }
Brian Paul2e8c51c2013-06-19 10:39:43 -0600133
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100134 emit->insn_offset = emit->ptr - emit->buf;
135 emit->ptr += sizeof(unsigned);
136 return TRUE;
137}
138
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100139
Brian Paul2e8c51c2013-06-19 10:39:43 -0600140static boolean
141svga_shader_emit_header(struct svga_shader_emitter *emit)
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100142{
143 SVGA3dShaderVersion header;
144
Brian Paul2e8c51c2013-06-19 10:39:43 -0600145 memset(&header, 0, sizeof header);
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100146
147 switch (emit->unit) {
148 case PIPE_SHADER_FRAGMENT:
Brian Paul94b219b2011-10-11 09:30:09 -0600149 header.value = SVGA3D_PS_30;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100150 break;
151 case PIPE_SHADER_VERTEX:
Brian Paul94b219b2011-10-11 09:30:09 -0600152 header.value = SVGA3D_VS_30;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100153 break;
154 }
Brian Paul2e8c51c2013-06-19 10:39:43 -0600155
156 return svga_shader_emit_dword(emit, header.value);
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100157}
158
159
Brian Paul58ea42b2011-11-03 17:40:56 -0600160/**
Brian Paul2e8c51c2013-06-19 10:39:43 -0600161 * Parse TGSI shader and translate to SVGA/DX9 serialized
162 * representation.
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100163 *
164 * In this function SVGA shader is emitted to an in-memory buffer that
165 * can be dynamically grown. Once we've finished and know how large
166 * it is, it will be copied to a hardware buffer for upload.
167 */
Brian Paule0542512015-08-13 11:00:58 -0700168struct svga_shader_variant *
Brian Paul8d0d5dc2015-10-08 21:03:27 -0600169svga_tgsi_vgpu9_translate(struct svga_context *svga,
170 const struct svga_shader *shader,
Brian Paule0542512015-08-13 11:00:58 -0700171 const struct svga_compile_key *key, unsigned unit)
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100172{
Brian Paul2a303792014-01-18 03:45:41 -0800173 struct svga_shader_variant *variant = NULL;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100174 struct svga_shader_emitter emit;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100175
Charmaine Lee2e1cfcc2016-08-19 08:49:17 -0600176 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_TGSIVGPU9TRANSLATE);
177
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100178 memset(&emit, 0, sizeof(emit));
179
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100180 emit.size = 1024;
181 emit.buf = MALLOC(emit.size);
182 if (emit.buf == NULL) {
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100183 goto fail;
184 }
185
186 emit.ptr = emit.buf;
187 emit.unit = unit;
Brian Paulba497982013-06-28 08:09:48 -0600188 emit.key = *key;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100189
Brian Paul2e8c51c2013-06-19 10:39:43 -0600190 tgsi_scan_shader(shader->tokens, &emit.info);
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100191
192 emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
Brian Paul2e8c51c2013-06-19 10:39:43 -0600193
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100194 if (unit == PIPE_SHADER_FRAGMENT)
Brian Paule0542512015-08-13 11:00:58 -0700195 emit.imm_start += key->num_unnormalized_coords;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100196
197 if (unit == PIPE_SHADER_VERTEX) {
Brian Paule0542512015-08-13 11:00:58 -0700198 emit.imm_start += key->vs.need_prescale ? 2 : 0;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100199 }
200
Brian Paul2e8c51c2013-06-19 10:39:43 -0600201 emit.nr_hw_float_const =
202 (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100203
204 emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
Brian Paul2e8c51c2013-06-19 10:39:43 -0600205
Brian Paul9b3d87b2012-06-29 16:53:35 -0600206 if (emit.nr_hw_temp >= SVGA3D_TEMPREG_MAX) {
Brian Paul2e8c51c2013-06-19 10:39:43 -0600207 debug_printf("svga: too many temporary registers (%u)\n",
208 emit.nr_hw_temp);
Brian Paulf12f67c2011-11-17 16:36:26 -0700209 goto fail;
Brian Paul9b3d87b2012-06-29 16:53:35 -0600210 }
Brian Paulf12f67c2011-11-17 16:36:26 -0700211
Charmaine Lee57d92222017-08-01 18:02:57 -0700212 if (emit.info.indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
213 debug_printf(
214 "svga: indirect indexing of temporary registers is not supported.\n");
215 goto fail;
216 }
217
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100218 emit.in_main_func = TRUE;
219
Brian Paul2e8c51c2013-06-19 10:39:43 -0600220 if (!svga_shader_emit_header(&emit)) {
Brian Paul9b3d87b2012-06-29 16:53:35 -0600221 debug_printf("svga: emit header failed\n");
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100222 goto fail;
Brian Paul9b3d87b2012-06-29 16:53:35 -0600223 }
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100224
Brian Paul2e8c51c2013-06-19 10:39:43 -0600225 if (!svga_shader_emit_instructions(&emit, shader->tokens)) {
Brian Paul9b3d87b2012-06-29 16:53:35 -0600226 debug_printf("svga: emit instructions failed\n");
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100227 goto fail;
Brian Paul9b3d87b2012-06-29 16:53:35 -0600228 }
229
Brian Paulf413f1a2015-10-08 21:06:18 -0600230 variant = svga_new_shader_variant(svga);
Edward O'Callaghan13eb5f52015-12-04 22:08:22 +1100231 if (!variant)
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100232 goto fail;
233
Brian Paul2a303792014-01-18 03:45:41 -0800234 variant->shader = shader;
235 variant->tokens = (const unsigned *) emit.buf;
236 variant->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
237 memcpy(&variant->key, key, sizeof(*key));
238 variant->id = UTIL_BITMASK_INVALID_INDEX;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100239
Brian Paule0542512015-08-13 11:00:58 -0700240 variant->pstipple_sampler_unit = emit.pstipple_sampler_unit;
241
Brian Paul10827352015-10-22 15:36:25 -0600242 /* If there was exactly one write to a fragment shader output register
243 * and it came from a constant buffer, we know all fragments will have
244 * the same color (except for blending).
245 */
246 variant->constant_color_output =
247 emit.constant_color_output && emit.num_output_writes == 1;
248
Brian Paule0542512015-08-13 11:00:58 -0700249#if 0
250 if (!svga_shader_verify(variant->tokens, variant->nr_tokens) ||
251 SVGA_DEBUG & DEBUG_TGSI) {
Brian Paul2e8c51c2013-06-19 10:39:43 -0600252 debug_printf("#####################################\n");
253 debug_printf("Shader %u below\n", shader->id);
254 tgsi_dump(shader->tokens, 0);
José Fonseca6dd96762009-11-27 13:59:37 +0000255 if (SVGA_DEBUG & DEBUG_TGSI) {
Brian Paul2e8c51c2013-06-19 10:39:43 -0600256 debug_printf("Shader %u compiled below\n", shader->id);
Brian Paul2a303792014-01-18 03:45:41 -0800257 svga_shader_dump(variant->tokens, variant->nr_tokens, FALSE);
José Fonseca6dd96762009-11-27 13:59:37 +0000258 }
Brian Paul2e8c51c2013-06-19 10:39:43 -0600259 debug_printf("#####################################\n");
José Fonseca6dd96762009-11-27 13:59:37 +0000260 }
Brian Paule0542512015-08-13 11:00:58 -0700261#endif
José Fonseca6dd96762009-11-27 13:59:37 +0000262
Charmaine Lee2e1cfcc2016-08-19 08:49:17 -0600263 goto done;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100264
Charmaine Lee2e1cfcc2016-08-19 08:49:17 -0600265fail:
Brian Paul2a303792014-01-18 03:45:41 -0800266 FREE(variant);
Brian Paul96e08942016-03-29 13:34:36 -0600267 if (emit.buf != err_buf)
268 FREE(emit.buf);
Charmaine Lee2e1cfcc2016-08-19 08:49:17 -0600269 variant = NULL;
270
271done:
272 SVGA_STATS_TIME_POP(svga_sws(svga));
273 return variant;
Jakob Bornecrantz31926332009-11-16 19:56:18 +0100274}