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