2009-03-30 23:54:53 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
|
2009-11-26 19:37:58 +01:00
|
|
|
* Copyright 2009 Marek Olšák <maraeo@gmail.com>
|
2009-03-30 23:54:53 -07:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* on the rights to use, copy, modify, merge, publish, distribute, sub
|
|
|
|
* license, and/or sell copies of the Software, and to permit persons to whom
|
|
|
|
* the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
2009-06-26 01:08:13 +02:00
|
|
|
#include "r300_vs.h"
|
2009-03-30 23:54:53 -07:00
|
|
|
|
2009-07-27 20:23:49 +02:00
|
|
|
#include "r300_context.h"
|
2009-11-26 19:37:58 +01:00
|
|
|
#include "r300_screen.h"
|
2009-07-27 20:23:49 +02:00
|
|
|
#include "r300_tgsi_to_rc.h"
|
2009-11-26 19:37:58 +01:00
|
|
|
#include "r300_reg.h"
|
2009-03-30 23:54:53 -07:00
|
|
|
|
2009-07-27 20:23:49 +02:00
|
|
|
#include "tgsi/tgsi_dump.h"
|
|
|
|
#include "tgsi/tgsi_parse.h"
|
2010-04-11 10:15:12 +02:00
|
|
|
#include "tgsi/tgsi_ureg.h"
|
2009-04-04 22:57:45 -07:00
|
|
|
|
2011-07-26 21:15:05 +02:00
|
|
|
#include "compiler/radeon_compiler.h"
|
2009-05-09 00:38:07 -07:00
|
|
|
|
2009-11-26 19:37:58 +01:00
|
|
|
/* Convert info about VS output semantics into r300_shader_semantics. */
|
|
|
|
static void r300_shader_read_vs_outputs(
|
2012-04-03 22:37:30 +02:00
|
|
|
struct r300_context *r300,
|
2009-11-26 19:37:58 +01:00
|
|
|
struct tgsi_shader_info* info,
|
|
|
|
struct r300_shader_semantics* vs_outputs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
r300_shader_semantics_reset(vs_outputs);
|
|
|
|
|
|
|
|
for (i = 0; i < info->num_outputs; i++) {
|
|
|
|
index = info->output_semantic_index[i];
|
|
|
|
|
|
|
|
switch (info->output_semantic_name[i]) {
|
|
|
|
case TGSI_SEMANTIC_POSITION:
|
|
|
|
assert(index == 0);
|
|
|
|
vs_outputs->pos = i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TGSI_SEMANTIC_PSIZE:
|
|
|
|
assert(index == 0);
|
|
|
|
vs_outputs->psize = i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TGSI_SEMANTIC_COLOR:
|
2010-02-09 01:26:11 -08:00
|
|
|
assert(index < ATTR_COLOR_COUNT);
|
2009-11-26 19:37:58 +01:00
|
|
|
vs_outputs->color[index] = i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TGSI_SEMANTIC_BCOLOR:
|
2010-02-09 01:26:11 -08:00
|
|
|
assert(index < ATTR_COLOR_COUNT);
|
2009-11-26 19:37:58 +01:00
|
|
|
vs_outputs->bcolor[index] = i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TGSI_SEMANTIC_GENERIC:
|
2010-02-09 01:26:11 -08:00
|
|
|
assert(index < ATTR_GENERIC_COUNT);
|
2009-11-26 19:37:58 +01:00
|
|
|
vs_outputs->generic[index] = i;
|
2013-02-02 06:20:25 +01:00
|
|
|
vs_outputs->num_generic++;
|
2009-11-26 19:37:58 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TGSI_SEMANTIC_FOG:
|
|
|
|
assert(index == 0);
|
|
|
|
vs_outputs->fog = i;
|
|
|
|
break;
|
|
|
|
|
2009-12-19 00:18:43 +01:00
|
|
|
case TGSI_SEMANTIC_EDGEFLAG:
|
|
|
|
assert(index == 0);
|
2010-04-11 08:28:39 +02:00
|
|
|
fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");
|
2009-12-19 00:18:43 +01:00
|
|
|
break;
|
2010-04-11 08:28:39 +02:00
|
|
|
|
2012-04-03 22:37:30 +02:00
|
|
|
case TGSI_SEMANTIC_CLIPVERTEX:
|
|
|
|
assert(index == 0);
|
|
|
|
/* Draw does clip vertex for us. */
|
|
|
|
if (r300->screen->caps.has_tcl) {
|
|
|
|
fprintf(stderr, "r300 VP: cannot handle clip vertex output.\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-11-26 19:37:58 +01:00
|
|
|
default:
|
2010-04-11 08:28:39 +02:00
|
|
|
fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",
|
|
|
|
info->output_semantic_name[i]);
|
2009-11-26 19:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-07 02:32:09 +01:00
|
|
|
|
|
|
|
/* WPOS is a straight copy of POSITION and it's always emitted. */
|
|
|
|
vs_outputs->wpos = i;
|
2009-11-26 19:37:58 +01:00
|
|
|
}
|
|
|
|
|
2009-07-27 20:23:49 +02:00
|
|
|
static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
|
2009-04-25 16:53:38 -07:00
|
|
|
{
|
2009-07-27 20:23:49 +02:00
|
|
|
struct r300_vertex_shader * vs = c->UserData;
|
2009-11-27 10:19:20 +01:00
|
|
|
struct r300_shader_semantics* outputs = &vs->outputs;
|
2009-04-25 16:53:38 -07:00
|
|
|
struct tgsi_shader_info* info = &vs->info;
|
2009-11-27 10:19:20 +01:00
|
|
|
int i, reg = 0;
|
2009-12-19 23:55:34 +01:00
|
|
|
boolean any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||
|
|
|
|
outputs->bcolor[1] != ATTR_UNUSED;
|
2009-04-25 16:53:38 -07:00
|
|
|
|
2009-07-27 20:23:49 +02:00
|
|
|
/* Fill in the input mapping */
|
|
|
|
for (i = 0; i < info->num_inputs; i++)
|
|
|
|
c->code->inputs[i] = i;
|
|
|
|
|
2009-11-27 10:19:20 +01:00
|
|
|
/* Position. */
|
|
|
|
if (outputs->pos != ATTR_UNUSED) {
|
|
|
|
c->code->outputs[outputs->pos] = reg++;
|
|
|
|
} else {
|
|
|
|
assert(0);
|
2009-04-25 16:53:38 -07:00
|
|
|
}
|
2009-05-17 10:33:56 -07:00
|
|
|
|
2009-11-27 10:19:20 +01:00
|
|
|
/* Point size. */
|
|
|
|
if (outputs->psize != ATTR_UNUSED) {
|
|
|
|
c->code->outputs[outputs->psize] = reg++;
|
|
|
|
}
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2009-12-19 23:55:34 +01:00
|
|
|
/* If we're writing back facing colors we need to send
|
|
|
|
* four colors to make front/back face colors selection work.
|
|
|
|
* If the vertex program doesn't write all 4 colors, lets
|
|
|
|
* pretend it does by skipping output index reg so the colors
|
|
|
|
* get written into appropriate output vectors.
|
|
|
|
*/
|
|
|
|
|
2009-11-27 10:19:20 +01:00
|
|
|
/* Colors. */
|
|
|
|
for (i = 0; i < ATTR_COLOR_COUNT; i++) {
|
|
|
|
if (outputs->color[i] != ATTR_UNUSED) {
|
|
|
|
c->code->outputs[outputs->color[i]] = reg++;
|
2010-01-17 04:49:07 +01:00
|
|
|
} else if (any_bcolor_used ||
|
|
|
|
outputs->color[1] != ATTR_UNUSED) {
|
2009-12-19 23:55:34 +01:00
|
|
|
reg++;
|
2009-11-27 10:19:20 +01:00
|
|
|
}
|
|
|
|
}
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2009-12-19 23:55:34 +01:00
|
|
|
/* Back-face colors. */
|
|
|
|
for (i = 0; i < ATTR_COLOR_COUNT; i++) {
|
|
|
|
if (outputs->bcolor[i] != ATTR_UNUSED) {
|
|
|
|
c->code->outputs[outputs->bcolor[i]] = reg++;
|
|
|
|
} else if (any_bcolor_used) {
|
|
|
|
reg++;
|
|
|
|
}
|
|
|
|
}
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2009-11-27 10:19:20 +01:00
|
|
|
/* Texture coordinates. */
|
|
|
|
for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
|
|
|
|
if (outputs->generic[i] != ATTR_UNUSED) {
|
|
|
|
c->code->outputs[outputs->generic[i]] = reg++;
|
2009-03-30 23:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-27 10:19:20 +01:00
|
|
|
/* Fog coordinates. */
|
|
|
|
if (outputs->fog != ATTR_UNUSED) {
|
|
|
|
c->code->outputs[outputs->fog] = reg++;
|
|
|
|
}
|
2009-12-24 03:10:33 +01:00
|
|
|
|
|
|
|
/* WPOS. */
|
2010-05-02 06:24:55 +02:00
|
|
|
c->code->outputs[outputs->wpos] = reg++;
|
2009-12-24 03:10:33 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 22:37:30 +02:00
|
|
|
void r300_init_vs_outputs(struct r300_context *r300,
|
|
|
|
struct r300_vertex_shader *vs)
|
2010-05-22 13:46:30 +02:00
|
|
|
{
|
|
|
|
tgsi_scan_shader(vs->state.tokens, &vs->info);
|
2012-04-03 22:37:30 +02:00
|
|
|
r300_shader_read_vs_outputs(r300, &vs->info, &vs->outputs);
|
2010-05-22 13:46:30 +02:00
|
|
|
}
|
|
|
|
|
2010-04-11 10:15:12 +02:00
|
|
|
static void r300_dummy_vertex_shader(
|
|
|
|
struct r300_context* r300,
|
|
|
|
struct r300_vertex_shader* shader)
|
2009-12-24 03:10:33 +01:00
|
|
|
{
|
2010-04-11 10:15:12 +02:00
|
|
|
struct ureg_program *ureg;
|
|
|
|
struct ureg_dst dst;
|
|
|
|
struct ureg_src imm;
|
2009-12-24 03:10:33 +01:00
|
|
|
|
2010-04-11 10:15:12 +02:00
|
|
|
/* Make a simple vertex shader which outputs (0, 0, 0, 1),
|
|
|
|
* effectively rendering nothing. */
|
2016-04-16 14:41:57 +02:00
|
|
|
ureg = ureg_create(PIPE_SHADER_VERTEX);
|
2010-04-11 10:15:12 +02:00
|
|
|
dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
|
|
|
|
imm = ureg_imm4f(ureg, 0, 0, 0, 1);
|
|
|
|
|
|
|
|
ureg_MOV(ureg, dst, imm);
|
|
|
|
ureg_END(ureg);
|
|
|
|
|
2010-05-22 13:46:30 +02:00
|
|
|
shader->state.tokens = tgsi_dup_tokens(ureg_finalize(ureg));
|
|
|
|
ureg_destroy(ureg);
|
2010-04-11 10:15:12 +02:00
|
|
|
|
|
|
|
shader->dummy = TRUE;
|
2012-04-03 22:37:30 +02:00
|
|
|
r300_init_vs_outputs(r300, shader);
|
2010-05-15 22:55:17 +02:00
|
|
|
r300_translate_vertex_shader(r300, shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void r300_translate_vertex_shader(struct r300_context *r300,
|
|
|
|
struct r300_vertex_shader *vs)
|
2009-07-27 20:23:49 +02:00
|
|
|
{
|
|
|
|
struct r300_vertex_program_compiler compiler;
|
|
|
|
struct tgsi_to_rc ttr;
|
2010-08-23 05:56:48 +02:00
|
|
|
unsigned i;
|
2009-04-05 01:32:55 -07:00
|
|
|
|
2009-07-27 20:23:49 +02:00
|
|
|
/* Setup the compiler */
|
2010-09-05 05:07:02 +02:00
|
|
|
memset(&compiler, 0, sizeof(compiler));
|
2012-09-06 00:20:27 -04:00
|
|
|
rc_init(&compiler.Base, NULL);
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2010-10-17 23:17:01 -07:00
|
|
|
DBG_ON(r300, DBG_VP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;
|
2009-07-27 20:23:49 +02:00
|
|
|
compiler.code = &vs->code;
|
|
|
|
compiler.UserData = vs;
|
2021-12-07 10:18:03 -08:00
|
|
|
if (!vs->dummy)
|
|
|
|
compiler.Base.debug = &r300->debug;
|
2010-05-26 04:18:27 +02:00
|
|
|
compiler.Base.is_r500 = r300->screen->caps.is_r500;
|
2010-09-01 08:12:51 +02:00
|
|
|
compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);
|
2010-09-01 06:14:58 +02:00
|
|
|
compiler.Base.has_half_swizzles = FALSE;
|
2010-07-13 21:25:27 -07:00
|
|
|
compiler.Base.has_presub = FALSE;
|
2011-06-15 08:00:53 -07:00
|
|
|
compiler.Base.has_omod = FALSE;
|
2022-02-09 15:48:04 +01:00
|
|
|
compiler.Base.needs_trig_input_transform = DBG_ON(r300, DBG_USE_TGSI);
|
2010-05-26 04:18:27 +02:00
|
|
|
compiler.Base.max_temp_regs = 32;
|
2010-09-01 04:59:22 +02:00
|
|
|
compiler.Base.max_constants = 256;
|
2010-09-01 00:56:57 +02:00
|
|
|
compiler.Base.max_alu_insts = r300->screen->caps.is_r500 ? 1024 : 256;
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2010-10-17 23:17:01 -07:00
|
|
|
if (compiler.Base.Debug & RC_DBG_LOG) {
|
2010-08-09 17:15:37 +02:00
|
|
|
DBG(r300, DBG_VP, "r300: Initial vertex program\n");
|
2010-05-15 22:55:17 +02:00
|
|
|
tgsi_dump(vs->state.tokens, 0);
|
2009-07-27 20:23:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Translate TGSI to our internal representation */
|
|
|
|
ttr.compiler = &compiler.Base;
|
|
|
|
ttr.info = &vs->info;
|
2010-02-10 18:38:53 -08:00
|
|
|
ttr.use_half_swizzles = FALSE;
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2010-05-15 22:55:17 +02:00
|
|
|
r300_tgsi_to_rc(&ttr, vs->state.tokens);
|
2009-07-27 20:23:49 +02:00
|
|
|
|
2011-01-23 12:03:59 +01:00
|
|
|
if (ttr.error) {
|
|
|
|
fprintf(stderr, "r300 VP: Cannot translate a shader. "
|
|
|
|
"Using a dummy shader instead.\n");
|
|
|
|
r300_dummy_vertex_shader(r300, vs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-03 06:32:10 +01:00
|
|
|
if (compiler.Base.Program.Constants.Count > 200) {
|
|
|
|
compiler.Base.remove_unused_constants = TRUE;
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:53:52 +02:00
|
|
|
compiler.RequiredOutputs = ~(~0U << (vs->info.num_outputs + 1));
|
2009-07-27 20:23:49 +02:00
|
|
|
compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
|
|
|
|
|
2009-12-24 03:10:33 +01:00
|
|
|
/* Insert the WPOS output. */
|
2010-05-02 06:24:55 +02:00
|
|
|
rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
|
2009-12-24 03:10:33 +01:00
|
|
|
|
2009-07-27 20:23:49 +02:00
|
|
|
/* Invoke the compiler */
|
|
|
|
r3xx_compile_vertex_program(&compiler);
|
|
|
|
if (compiler.Base.Error) {
|
2010-08-23 05:56:48 +02:00
|
|
|
fprintf(stderr, "r300 VP: Compiler error:\n%sUsing a dummy shader"
|
|
|
|
" instead.\n", compiler.Base.ErrorMsg);
|
2010-04-11 10:15:12 +02:00
|
|
|
|
|
|
|
if (vs->dummy) {
|
|
|
|
fprintf(stderr, "r300 VP: Cannot compile the dummy shader! "
|
|
|
|
"Giving up...\n");
|
|
|
|
abort();
|
|
|
|
}
|
2010-04-14 20:23:15 +02:00
|
|
|
|
|
|
|
rc_destroy(&compiler.Base);
|
2010-04-11 10:15:12 +02:00
|
|
|
r300_dummy_vertex_shader(r300, vs);
|
2010-04-14 20:23:15 +02:00
|
|
|
return;
|
2009-07-27 20:23:49 +02:00
|
|
|
}
|
2009-07-22 23:58:35 -07:00
|
|
|
|
2010-04-14 02:21:06 +02:00
|
|
|
/* Initialize numbers of constants for each type. */
|
2010-08-23 05:56:48 +02:00
|
|
|
vs->externals_count = 0;
|
|
|
|
for (i = 0;
|
|
|
|
i < vs->code.constants.Count &&
|
|
|
|
vs->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {
|
|
|
|
vs->externals_count = i+1;
|
|
|
|
}
|
|
|
|
for (; i < vs->code.constants.Count; i++) {
|
|
|
|
assert(vs->code.constants.Constants[i].Type == RC_CONSTANT_IMMEDIATE);
|
|
|
|
}
|
2010-04-14 02:21:06 +02:00
|
|
|
vs->immediates_count = vs->code.constants.Count - vs->externals_count;
|
|
|
|
|
2009-07-22 23:58:35 -07:00
|
|
|
/* And, finally... */
|
2009-07-27 20:23:49 +02:00
|
|
|
rc_destroy(&compiler.Base);
|
2009-03-30 23:54:53 -07:00
|
|
|
}
|