Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nouveau_compiler.c
4570 views
/*1* Copyright 2014 Ilia Mirkin2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*/2122#include <errno.h>2324#include "tgsi/tgsi_text.h"25#include "util/u_debug.h"2627#include "codegen/nv50_ir_driver.h"28#include "nv50/nv50_context.h"2930/* these headers weren't really meant to be included together */31#undef SB_DATA3233#include "nv30/nv30_state.h"34#include "nv30/nvfx_shader.h"3536static int37nv30_fp(int chipset, struct tgsi_token tokens[],38unsigned *size, unsigned **code) {39struct nv30_fragprog fp;40memset(&fp, 0, sizeof(fp));41fp.pipe.tokens = tokens;42tgsi_scan_shader(fp.pipe.tokens, &fp.info);43_nvfx_fragprog_translate(chipset >= 0x40 ? 0x4097 : 0x3097, &fp);44*size = fp.insn_len * 4;45*code = fp.insn;46return !fp.translated;47}4849static int50nv30_vp(int chipset, struct tgsi_token tokens[],51unsigned *size, unsigned **code) {52struct nv30_vertprog vp;53memset(&vp, 0, sizeof(vp));5455vp.pipe.tokens = tokens;56tgsi_scan_shader(vp.pipe.tokens, &vp.info);57_nvfx_vertprog_translate(chipset >= 0x40 ? 0x4097 : 0x3097, &vp);58*size = vp.nr_insns * 16;59*code = (unsigned *)vp.insns;60return !vp.translated;61}6263static int64nv30_codegen(int chipset, int type, struct tgsi_token tokens[],65unsigned *size, unsigned **code) {66switch (type) {67case PIPE_SHADER_FRAGMENT:68return nv30_fp(chipset, tokens, size, code);69case PIPE_SHADER_VERTEX:70return nv30_vp(chipset, tokens, size, code);71}72_debug_printf("Unexpected shader type: %d\n", type);73return 1;74}7576static int77dummy_assign_slots(struct nv50_ir_prog_info_out *info)78{79unsigned i, n, c;8081n = 0;82for (i = 0; i < info->numInputs; ++i) {83for (c = 0; c < 4; ++c)84if (info->in[i].mask & (1 << c))85info->in[i].slot[c] = n++;86}8788/* VertexID before InstanceID */89if (info->io.vertexId < info->numSysVals)90info->sv[info->io.vertexId].slot[0] = n++;91if (info->io.instanceId < info->numSysVals)92info->sv[info->io.instanceId].slot[0] = n++;9394n = 0;95for (i = 0; i < info->numOutputs; ++i) {96for (c = 0; c < 4; ++c)97if (info->out[i].mask & (1 << c))98info->out[i].slot[c] = n++;99}100return 0;101}102103static int104nouveau_codegen(int chipset, int type, struct tgsi_token tokens[],105unsigned *size, unsigned **code) {106struct nv50_ir_prog_info info = {0};107struct nv50_ir_prog_info_out info_out = {0};108int ret;109110info.type = type;111info.target = chipset;112info.bin.sourceRep = PIPE_SHADER_IR_TGSI;113info.bin.source = tokens;114115info.io.auxCBSlot = 15;116info.io.ucpBase = NV50_CB_AUX_UCP_OFFSET;117info.io.suInfoBase = NV50_CB_AUX_TEX_MS_OFFSET;118info.io.msInfoCBSlot = 15;119info.io.msInfoBase = NV50_CB_AUX_MS_OFFSET;120121info.assignSlots = dummy_assign_slots;122123info.optLevel = debug_get_num_option("NV50_PROG_OPTIMIZE", 3);124info.dbgFlags = debug_get_num_option("NV50_PROG_DEBUG", 0);125info.omitLineNum = debug_get_num_option("NV50_PROG_DEBUG_OMIT_LINENUM", 0);126127ret = nv50_ir_generate_code(&info, &info_out);128if (ret) {129_debug_printf("Error compiling program: %d\n", ret);130return ret;131}132133*size = info_out.bin.codeSize;134*code = info_out.bin.code;135return 0;136}137138int139main(int argc, char *argv[])140{141struct tgsi_token tokens[4096];142int i, chipset = 0, type = -1;143const char *filename = NULL;144FILE *f;145char text[65536] = {0};146unsigned size = 0, *code = NULL;147148for (i = 1; i < argc; i++) {149if (!strcmp(argv[i], "-a"))150chipset = strtol(argv[++i], NULL, 16);151else152filename = argv[i];153}154155if (!chipset) {156_debug_printf("Must specify a chipset (-a)\n");157return 1;158}159160if (!filename) {161_debug_printf("Must specify a filename\n");162return 1;163}164165if (!strcmp(filename, "-"))166f = stdin;167else168f = fopen(filename, "r");169170if (!f) {171_debug_printf("Error opening file '%s': %s\n", filename, strerror(errno));172return 1;173}174175if (!fread(text, 1, sizeof(text), f) || ferror(f)) {176_debug_printf("Error reading file '%s'\n", filename);177fclose(f);178return 1;179}180fclose(f);181182_debug_printf("Compiling for NV%X\n", chipset);183184if (!strncmp(text, "FRAG", 4))185type = PIPE_SHADER_FRAGMENT;186else if (!strncmp(text, "VERT", 4))187type = PIPE_SHADER_VERTEX;188else if (!strncmp(text, "GEOM", 4))189type = PIPE_SHADER_GEOMETRY;190else if (!strncmp(text, "COMP", 4))191type = PIPE_SHADER_COMPUTE;192else if (!strncmp(text, "TESS_CTRL", 9))193type = PIPE_SHADER_TESS_CTRL;194else if (!strncmp(text, "TESS_EVAL", 9))195type = PIPE_SHADER_TESS_EVAL;196else {197_debug_printf("Unrecognized TGSI header\n");198return 1;199}200201if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {202_debug_printf("Failed to parse TGSI shader\n");203return 1;204}205206if (chipset >= 0x50) {207i = nouveau_codegen(chipset, type, tokens, &size, &code);208} else if (chipset >= 0x30) {209i = nv30_codegen(chipset, type, tokens, &size, &code);210} else {211_debug_printf("chipset NV%02X not supported\n", chipset);212i = 1;213}214if (i)215return i;216217_debug_printf("program binary (%d bytes)\n", size);218for (i = 0; i < size; i += 4) {219printf("%08x ", code[i / 4]);220if (i % (8 * 4) == (7 * 4))221printf("\n");222}223if (i % (8 * 4) != 0)224printf("\n");225226return 0;227}228229230