Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nv30/nv30_vertprog.c
4574 views
/*1* Copyright 2012 Red Hat Inc.2*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*21* Authors: Ben Skeggs22*23*/2425#include "draw/draw_context.h"26#include "util/u_dynarray.h"27#include "tgsi/tgsi_parse.h"2829#include "nv_object.xml.h"30#include "nv30/nv30-40_3d.xml.h"31#include "nv30/nv30_context.h"32#include "nv30/nvfx_shader.h"33#include "nv30/nv30_state.h"3435static void36nv30_vertprog_destroy(struct nv30_vertprog *vp)37{38util_dynarray_fini(&vp->branch_relocs);39nouveau_heap_free(&vp->exec);40FREE(vp->insns);41vp->insns = NULL;42vp->nr_insns = 0;4344util_dynarray_fini(&vp->const_relocs);45nouveau_heap_free(&vp->data);46FREE(vp->consts);47vp->consts = NULL;48vp->nr_consts = 0;4950vp->translated = false;51}5253void54nv30_vertprog_validate(struct nv30_context *nv30)55{56struct nouveau_pushbuf *push = nv30->base.pushbuf;57struct nouveau_object *eng3d = nv30->screen->eng3d;58struct nv30_vertprog *vp = nv30->vertprog.program;59struct nv30_fragprog *fp = nv30->fragprog.program;60bool upload_code = false;61bool upload_data = false;62unsigned i;6364if (nv30->dirty & NV30_NEW_FRAGPROG) {65if (memcmp(vp->texcoord, fp->texcoord, sizeof(vp->texcoord))) {66if (vp->translated)67nv30_vertprog_destroy(vp);68memcpy(vp->texcoord, fp->texcoord, sizeof(vp->texcoord));69}70}7172if (nv30->rast && nv30->rast->pipe.clip_plane_enable != vp->enabled_ucps) {73vp->enabled_ucps = nv30->rast->pipe.clip_plane_enable;74if (vp->translated)75nv30_vertprog_destroy(vp);76}7778if (!vp->translated) {79vp->translated = _nvfx_vertprog_translate(eng3d->oclass, vp);80if (!vp->translated) {81nv30->draw_flags |= NV30_NEW_VERTPROG;82return;83}84nv30->dirty |= NV30_NEW_VERTPROG;85}8687if (!vp->exec) {88struct nouveau_heap *heap = nv30->screen->vp_exec_heap;89struct nv30_shader_reloc *reloc = vp->branch_relocs.data;90unsigned nr_reloc = vp->branch_relocs.size / sizeof(*reloc);91uint32_t *inst, target;9293if (nouveau_heap_alloc(heap, vp->nr_insns, &vp->exec, &vp->exec)) {94while (heap->next && heap->size < vp->nr_insns) {95struct nouveau_heap **evict = heap->next->priv;96nouveau_heap_free(evict);97}9899if (nouveau_heap_alloc(heap, vp->nr_insns, &vp->exec, &vp->exec)) {100nv30->draw_flags |= NV30_NEW_VERTPROG;101return;102}103}104105if (eng3d->oclass < NV40_3D_CLASS) {106while (nr_reloc--) {107inst = vp->insns[reloc->location].data;108target = vp->exec->start + reloc->target;109110inst[2] &= ~0x000007fc;111inst[2] |= target << 2;112reloc++;113}114} else {115while (nr_reloc--) {116inst = vp->insns[reloc->location].data;117target = vp->exec->start + reloc->target;118119inst[2] &= ~0x0000003f;120inst[2] |= target >> 3;121inst[3] &= ~0xe0000000;122inst[3] |= target << 29;123reloc++;124}125}126127upload_code = true;128}129130if (vp->nr_consts && !vp->data) {131struct nouveau_heap *heap = nv30->screen->vp_data_heap;132struct nv30_shader_reloc *reloc = vp->const_relocs.data;133unsigned nr_reloc = vp->const_relocs.size / sizeof(*reloc);134uint32_t *inst, target;135136if (nouveau_heap_alloc(heap, vp->nr_consts, vp, &vp->data)) {137while (heap->next && heap->size < vp->nr_consts) {138struct nv30_vertprog *evp = heap->next->priv;139nouveau_heap_free(&evp->data);140}141142if (nouveau_heap_alloc(heap, vp->nr_consts, vp, &vp->data)) {143nv30->draw_flags |= NV30_NEW_VERTPROG;144return;145}146}147148if (eng3d->oclass < NV40_3D_CLASS) {149while (nr_reloc--) {150inst = vp->insns[reloc->location].data;151target = vp->data->start + reloc->target;152153inst[1] &= ~0x0007fc000;154inst[1] |= (target & 0x1ff) << 14;155reloc++;156}157} else {158while (nr_reloc--) {159inst = vp->insns[reloc->location].data;160target = vp->data->start + reloc->target;161162inst[1] &= ~0x0001ff000;163inst[1] |= (target & 0x1ff) << 12;164reloc++;165}166}167168upload_code = true;169upload_data = true;170}171172if (vp->nr_consts) {173struct nv04_resource *res = nv04_resource(nv30->vertprog.constbuf);174175for (i = 0; i < vp->nr_consts; i++) {176struct nv30_vertprog_data *data = &vp->consts[i];177178if (data->index < 0) {179if (!upload_data)180continue;181} else {182float *constbuf = (float *)res->data;183if (!upload_data &&184!memcmp(data->value, &constbuf[data->index * 4], 16))185continue;186memcpy(data->value, &constbuf[data->index * 4], 16);187}188189BEGIN_NV04(push, NV30_3D(VP_UPLOAD_CONST_ID), 5);190PUSH_DATA (push, vp->data->start + i);191PUSH_DATAp(push, data->value, 4);192}193}194195if (upload_code) {196BEGIN_NV04(push, NV30_3D(VP_UPLOAD_FROM_ID), 1);197PUSH_DATA (push, vp->exec->start);198for (i = 0; i < vp->nr_insns; i++) {199BEGIN_NV04(push, NV30_3D(VP_UPLOAD_INST(0)), 4);200PUSH_DATAp(push, vp->insns[i].data, 4);201}202}203204if (nv30->dirty & (NV30_NEW_VERTPROG | NV30_NEW_FRAGPROG)) {205BEGIN_NV04(push, NV30_3D(VP_START_FROM_ID), 1);206PUSH_DATA (push, vp->exec->start);207if (eng3d->oclass < NV40_3D_CLASS) {208BEGIN_NV04(push, NV30_3D(ENGINE), 1);209PUSH_DATA (push, 0x00000013); /* vp instead of ff, somehow */210} else {211BEGIN_NV04(push, NV40_3D(VP_ATTRIB_EN), 2);212PUSH_DATA (push, vp->ir);213PUSH_DATA (push, vp->or | fp->vp_or);214BEGIN_NV04(push, NV30_3D(ENGINE), 1);215PUSH_DATA (push, 0x00000011);216}217}218}219220static void *221nv30_vp_state_create(struct pipe_context *pipe,222const struct pipe_shader_state *cso)223{224struct nv30_vertprog *vp = CALLOC_STRUCT(nv30_vertprog);225if (!vp)226return NULL;227228vp->pipe.tokens = tgsi_dup_tokens(cso->tokens);229tgsi_scan_shader(vp->pipe.tokens, &vp->info);230return vp;231}232233static void234nv30_vp_state_delete(struct pipe_context *pipe, void *hwcso)235{236struct nv30_vertprog *vp = hwcso;237238if (vp->translated)239nv30_vertprog_destroy(vp);240241if (vp->draw)242draw_delete_vertex_shader(nv30_context(pipe)->draw, vp->draw);243244FREE((void *)vp->pipe.tokens);245FREE(vp);246}247248static void249nv30_vp_state_bind(struct pipe_context *pipe, void *hwcso)250{251struct nv30_context *nv30 = nv30_context(pipe);252253nv30->vertprog.program = hwcso;254nv30->dirty |= NV30_NEW_VERTPROG;255}256257void258nv30_vertprog_init(struct pipe_context *pipe)259{260pipe->create_vs_state = nv30_vp_state_create;261pipe->bind_vs_state = nv30_vp_state_bind;262pipe->delete_vs_state = nv30_vp_state_delete;263}264265266