1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2012 Red Hat Inc. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 12bf215546Sopenharmony_ci * all copies or substantial portions of the Software. 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 21bf215546Sopenharmony_ci * 22bf215546Sopenharmony_ci * Authors: Ben Skeggs 23bf215546Sopenharmony_ci * 24bf215546Sopenharmony_ci */ 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include "draw/draw_context.h" 27bf215546Sopenharmony_ci#include "util/u_dynarray.h" 28bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h" 29bf215546Sopenharmony_ci#include "nir/nir_to_tgsi.h" 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci#include "nv_object.xml.h" 32bf215546Sopenharmony_ci#include "nv30/nv30-40_3d.xml.h" 33bf215546Sopenharmony_ci#include "nv30/nv30_context.h" 34bf215546Sopenharmony_ci#include "nv30/nvfx_shader.h" 35bf215546Sopenharmony_ci#include "nv30/nv30_state.h" 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_cistatic void 38bf215546Sopenharmony_cinv30_vertprog_destroy(struct nv30_vertprog *vp) 39bf215546Sopenharmony_ci{ 40bf215546Sopenharmony_ci util_dynarray_fini(&vp->branch_relocs); 41bf215546Sopenharmony_ci nouveau_heap_free(&vp->exec); 42bf215546Sopenharmony_ci FREE(vp->insns); 43bf215546Sopenharmony_ci vp->insns = NULL; 44bf215546Sopenharmony_ci vp->nr_insns = 0; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci util_dynarray_fini(&vp->const_relocs); 47bf215546Sopenharmony_ci nouveau_heap_free(&vp->data); 48bf215546Sopenharmony_ci FREE(vp->consts); 49bf215546Sopenharmony_ci vp->consts = NULL; 50bf215546Sopenharmony_ci vp->nr_consts = 0; 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci vp->translated = false; 53bf215546Sopenharmony_ci} 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_civoid 56bf215546Sopenharmony_cinv30_vertprog_validate(struct nv30_context *nv30) 57bf215546Sopenharmony_ci{ 58bf215546Sopenharmony_ci struct nouveau_pushbuf *push = nv30->base.pushbuf; 59bf215546Sopenharmony_ci struct nouveau_object *eng3d = nv30->screen->eng3d; 60bf215546Sopenharmony_ci struct nv30_vertprog *vp = nv30->vertprog.program; 61bf215546Sopenharmony_ci struct nv30_fragprog *fp = nv30->fragprog.program; 62bf215546Sopenharmony_ci bool upload_code = false; 63bf215546Sopenharmony_ci bool upload_data = false; 64bf215546Sopenharmony_ci unsigned i; 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci if (nv30->dirty & NV30_NEW_FRAGPROG) { 67bf215546Sopenharmony_ci if (memcmp(vp->texcoord, fp->texcoord, sizeof(vp->texcoord))) { 68bf215546Sopenharmony_ci if (vp->translated) 69bf215546Sopenharmony_ci nv30_vertprog_destroy(vp); 70bf215546Sopenharmony_ci memcpy(vp->texcoord, fp->texcoord, sizeof(vp->texcoord)); 71bf215546Sopenharmony_ci } 72bf215546Sopenharmony_ci } 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci if (nv30->rast && nv30->rast->pipe.clip_plane_enable != vp->enabled_ucps) { 75bf215546Sopenharmony_ci vp->enabled_ucps = nv30->rast->pipe.clip_plane_enable; 76bf215546Sopenharmony_ci if (vp->translated) 77bf215546Sopenharmony_ci nv30_vertprog_destroy(vp); 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci if (!vp->translated) { 81bf215546Sopenharmony_ci vp->translated = _nvfx_vertprog_translate(eng3d->oclass, vp); 82bf215546Sopenharmony_ci if (!vp->translated) { 83bf215546Sopenharmony_ci nv30->draw_flags |= NV30_NEW_VERTPROG; 84bf215546Sopenharmony_ci return; 85bf215546Sopenharmony_ci } 86bf215546Sopenharmony_ci nv30->dirty |= NV30_NEW_VERTPROG; 87bf215546Sopenharmony_ci } 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci if (!vp->exec) { 90bf215546Sopenharmony_ci struct nouveau_heap *heap = nv30->screen->vp_exec_heap; 91bf215546Sopenharmony_ci struct nv30_shader_reloc *reloc = vp->branch_relocs.data; 92bf215546Sopenharmony_ci unsigned nr_reloc = vp->branch_relocs.size / sizeof(*reloc); 93bf215546Sopenharmony_ci uint32_t *inst, target; 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci if (nouveau_heap_alloc(heap, vp->nr_insns, &vp->exec, &vp->exec)) { 96bf215546Sopenharmony_ci while (heap->next && heap->size < vp->nr_insns) { 97bf215546Sopenharmony_ci struct nouveau_heap **evict = heap->next->priv; 98bf215546Sopenharmony_ci nouveau_heap_free(evict); 99bf215546Sopenharmony_ci } 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci if (nouveau_heap_alloc(heap, vp->nr_insns, &vp->exec, &vp->exec)) { 102bf215546Sopenharmony_ci nv30->draw_flags |= NV30_NEW_VERTPROG; 103bf215546Sopenharmony_ci return; 104bf215546Sopenharmony_ci } 105bf215546Sopenharmony_ci } 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci if (eng3d->oclass < NV40_3D_CLASS) { 108bf215546Sopenharmony_ci while (nr_reloc--) { 109bf215546Sopenharmony_ci inst = vp->insns[reloc->location].data; 110bf215546Sopenharmony_ci target = vp->exec->start + reloc->target; 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci inst[2] &= ~0x000007fc; 113bf215546Sopenharmony_ci inst[2] |= target << 2; 114bf215546Sopenharmony_ci reloc++; 115bf215546Sopenharmony_ci } 116bf215546Sopenharmony_ci } else { 117bf215546Sopenharmony_ci while (nr_reloc--) { 118bf215546Sopenharmony_ci inst = vp->insns[reloc->location].data; 119bf215546Sopenharmony_ci target = vp->exec->start + reloc->target; 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci inst[2] &= ~0x0000003f; 122bf215546Sopenharmony_ci inst[2] |= target >> 3; 123bf215546Sopenharmony_ci inst[3] &= ~0xe0000000; 124bf215546Sopenharmony_ci inst[3] |= target << 29; 125bf215546Sopenharmony_ci reloc++; 126bf215546Sopenharmony_ci } 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci upload_code = true; 130bf215546Sopenharmony_ci } 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci if (vp->nr_consts && !vp->data) { 133bf215546Sopenharmony_ci struct nouveau_heap *heap = nv30->screen->vp_data_heap; 134bf215546Sopenharmony_ci struct nv30_shader_reloc *reloc = vp->const_relocs.data; 135bf215546Sopenharmony_ci unsigned nr_reloc = vp->const_relocs.size / sizeof(*reloc); 136bf215546Sopenharmony_ci uint32_t *inst, target; 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci if (nouveau_heap_alloc(heap, vp->nr_consts, vp, &vp->data)) { 139bf215546Sopenharmony_ci while (heap->next && heap->size < vp->nr_consts) { 140bf215546Sopenharmony_ci struct nv30_vertprog *evp = heap->next->priv; 141bf215546Sopenharmony_ci nouveau_heap_free(&evp->data); 142bf215546Sopenharmony_ci } 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci if (nouveau_heap_alloc(heap, vp->nr_consts, vp, &vp->data)) { 145bf215546Sopenharmony_ci nv30->draw_flags |= NV30_NEW_VERTPROG; 146bf215546Sopenharmony_ci return; 147bf215546Sopenharmony_ci } 148bf215546Sopenharmony_ci } 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci if (eng3d->oclass < NV40_3D_CLASS) { 151bf215546Sopenharmony_ci while (nr_reloc--) { 152bf215546Sopenharmony_ci inst = vp->insns[reloc->location].data; 153bf215546Sopenharmony_ci target = vp->data->start + reloc->target; 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci inst[1] &= ~0x0007fc000; 156bf215546Sopenharmony_ci inst[1] |= (target & 0x1ff) << 14; 157bf215546Sopenharmony_ci reloc++; 158bf215546Sopenharmony_ci } 159bf215546Sopenharmony_ci } else { 160bf215546Sopenharmony_ci while (nr_reloc--) { 161bf215546Sopenharmony_ci inst = vp->insns[reloc->location].data; 162bf215546Sopenharmony_ci target = vp->data->start + reloc->target; 163bf215546Sopenharmony_ci 164bf215546Sopenharmony_ci inst[1] &= ~0x0001ff000; 165bf215546Sopenharmony_ci inst[1] |= (target & 0x1ff) << 12; 166bf215546Sopenharmony_ci reloc++; 167bf215546Sopenharmony_ci } 168bf215546Sopenharmony_ci } 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci upload_code = true; 171bf215546Sopenharmony_ci upload_data = true; 172bf215546Sopenharmony_ci } 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci if (vp->nr_consts) { 175bf215546Sopenharmony_ci struct nv04_resource *res = nv04_resource(nv30->vertprog.constbuf); 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci for (i = 0; i < vp->nr_consts; i++) { 178bf215546Sopenharmony_ci struct nv30_vertprog_data *data = &vp->consts[i]; 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci if (data->index < 0) { 181bf215546Sopenharmony_ci if (!upload_data) 182bf215546Sopenharmony_ci continue; 183bf215546Sopenharmony_ci } else { 184bf215546Sopenharmony_ci float *constbuf = (float *)res->data; 185bf215546Sopenharmony_ci if (!upload_data && 186bf215546Sopenharmony_ci !memcmp(data->value, &constbuf[data->index * 4], 16)) 187bf215546Sopenharmony_ci continue; 188bf215546Sopenharmony_ci memcpy(data->value, &constbuf[data->index * 4], 16); 189bf215546Sopenharmony_ci } 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci BEGIN_NV04(push, NV30_3D(VP_UPLOAD_CONST_ID), 5); 192bf215546Sopenharmony_ci PUSH_DATA (push, vp->data->start + i); 193bf215546Sopenharmony_ci PUSH_DATAp(push, data->value, 4); 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci } 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci if (upload_code) { 198bf215546Sopenharmony_ci BEGIN_NV04(push, NV30_3D(VP_UPLOAD_FROM_ID), 1); 199bf215546Sopenharmony_ci PUSH_DATA (push, vp->exec->start); 200bf215546Sopenharmony_ci for (i = 0; i < vp->nr_insns; i++) { 201bf215546Sopenharmony_ci BEGIN_NV04(push, NV30_3D(VP_UPLOAD_INST(0)), 4); 202bf215546Sopenharmony_ci PUSH_DATAp(push, vp->insns[i].data, 4); 203bf215546Sopenharmony_ci } 204bf215546Sopenharmony_ci } 205bf215546Sopenharmony_ci 206bf215546Sopenharmony_ci if (nv30->dirty & (NV30_NEW_VERTPROG | NV30_NEW_FRAGPROG)) { 207bf215546Sopenharmony_ci BEGIN_NV04(push, NV30_3D(VP_START_FROM_ID), 1); 208bf215546Sopenharmony_ci PUSH_DATA (push, vp->exec->start); 209bf215546Sopenharmony_ci if (eng3d->oclass < NV40_3D_CLASS) { 210bf215546Sopenharmony_ci BEGIN_NV04(push, NV30_3D(ENGINE), 1); 211bf215546Sopenharmony_ci PUSH_DATA (push, 0x00000013); /* vp instead of ff, somehow */ 212bf215546Sopenharmony_ci } else { 213bf215546Sopenharmony_ci BEGIN_NV04(push, NV40_3D(VP_ATTRIB_EN), 2); 214bf215546Sopenharmony_ci PUSH_DATA (push, vp->ir); 215bf215546Sopenharmony_ci PUSH_DATA (push, vp->or | fp->vp_or); 216bf215546Sopenharmony_ci BEGIN_NV04(push, NV30_3D(ENGINE), 1); 217bf215546Sopenharmony_ci PUSH_DATA (push, 0x00000011); 218bf215546Sopenharmony_ci } 219bf215546Sopenharmony_ci } 220bf215546Sopenharmony_ci} 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_cistatic void * 223bf215546Sopenharmony_cinv30_vp_state_create(struct pipe_context *pipe, 224bf215546Sopenharmony_ci const struct pipe_shader_state *cso) 225bf215546Sopenharmony_ci{ 226bf215546Sopenharmony_ci struct nv30_vertprog *vp = CALLOC_STRUCT(nv30_vertprog); 227bf215546Sopenharmony_ci if (!vp) 228bf215546Sopenharmony_ci return NULL; 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci if (cso->type == PIPE_SHADER_IR_NIR) { 231bf215546Sopenharmony_ci vp->pipe.tokens = nir_to_tgsi(cso->ir.nir, pipe->screen); 232bf215546Sopenharmony_ci } else { 233bf215546Sopenharmony_ci assert(cso->type == PIPE_SHADER_IR_TGSI); 234bf215546Sopenharmony_ci /* we need to keep a local copy of the tokens */ 235bf215546Sopenharmony_ci vp->pipe.tokens = tgsi_dup_tokens(cso->tokens); 236bf215546Sopenharmony_ci } 237bf215546Sopenharmony_ci 238bf215546Sopenharmony_ci tgsi_scan_shader(vp->pipe.tokens, &vp->info); 239bf215546Sopenharmony_ci return vp; 240bf215546Sopenharmony_ci} 241bf215546Sopenharmony_ci 242bf215546Sopenharmony_cistatic void 243bf215546Sopenharmony_cinv30_vp_state_delete(struct pipe_context *pipe, void *hwcso) 244bf215546Sopenharmony_ci{ 245bf215546Sopenharmony_ci struct nv30_vertprog *vp = hwcso; 246bf215546Sopenharmony_ci 247bf215546Sopenharmony_ci if (vp->translated) 248bf215546Sopenharmony_ci nv30_vertprog_destroy(vp); 249bf215546Sopenharmony_ci 250bf215546Sopenharmony_ci if (vp->draw) 251bf215546Sopenharmony_ci draw_delete_vertex_shader(nv30_context(pipe)->draw, vp->draw); 252bf215546Sopenharmony_ci 253bf215546Sopenharmony_ci FREE((void *)vp->pipe.tokens); 254bf215546Sopenharmony_ci FREE(vp); 255bf215546Sopenharmony_ci} 256bf215546Sopenharmony_ci 257bf215546Sopenharmony_cistatic void 258bf215546Sopenharmony_cinv30_vp_state_bind(struct pipe_context *pipe, void *hwcso) 259bf215546Sopenharmony_ci{ 260bf215546Sopenharmony_ci struct nv30_context *nv30 = nv30_context(pipe); 261bf215546Sopenharmony_ci 262bf215546Sopenharmony_ci nv30->vertprog.program = hwcso; 263bf215546Sopenharmony_ci nv30->dirty |= NV30_NEW_VERTPROG; 264bf215546Sopenharmony_ci} 265bf215546Sopenharmony_ci 266bf215546Sopenharmony_civoid 267bf215546Sopenharmony_cinv30_vertprog_init(struct pipe_context *pipe) 268bf215546Sopenharmony_ci{ 269bf215546Sopenharmony_ci pipe->create_vs_state = nv30_vp_state_create; 270bf215546Sopenharmony_ci pipe->bind_vs_state = nv30_vp_state_bind; 271bf215546Sopenharmony_ci pipe->delete_vs_state = nv30_vp_state_delete; 272bf215546Sopenharmony_ci} 273