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
37 static void
nv30_vertprog_destroy(struct nv30_vertprog * vp)38 nv30_vertprog_destroy(struct nv30_vertprog *vp)
39 {
40 util_dynarray_fini(&vp->branch_relocs);
41 nouveau_heap_free(&vp->exec);
42 FREE(vp->insns);
43 vp->insns = NULL;
44 vp->nr_insns = 0;
45
46 util_dynarray_fini(&vp->const_relocs);
47 nouveau_heap_free(&vp->data);
48 FREE(vp->consts);
49 vp->consts = NULL;
50 vp->nr_consts = 0;
51
52 vp->translated = false;
53 }
54
55 void
nv30_vertprog_validate(struct nv30_context * nv30)56 nv30_vertprog_validate(struct nv30_context *nv30)
57 {
58 struct nouveau_pushbuf *push = nv30->base.pushbuf;
59 struct nouveau_object *eng3d = nv30->screen->eng3d;
60 struct nv30_vertprog *vp = nv30->vertprog.program;
61 struct nv30_fragprog *fp = nv30->fragprog.program;
62 bool upload_code = false;
63 bool upload_data = false;
64 unsigned i;
65
66 if (nv30->dirty & NV30_NEW_FRAGPROG) {
67 if (memcmp(vp->texcoord, fp->texcoord, sizeof(vp->texcoord))) {
68 if (vp->translated)
69 nv30_vertprog_destroy(vp);
70 memcpy(vp->texcoord, fp->texcoord, sizeof(vp->texcoord));
71 }
72 }
73
74 if (nv30->rast && nv30->rast->pipe.clip_plane_enable != vp->enabled_ucps) {
75 vp->enabled_ucps = nv30->rast->pipe.clip_plane_enable;
76 if (vp->translated)
77 nv30_vertprog_destroy(vp);
78 }
79
80 if (!vp->translated) {
81 vp->translated = _nvfx_vertprog_translate(eng3d->oclass, vp);
82 if (!vp->translated) {
83 nv30->draw_flags |= NV30_NEW_VERTPROG;
84 return;
85 }
86 nv30->dirty |= NV30_NEW_VERTPROG;
87 }
88
89 if (!vp->exec) {
90 struct nouveau_heap *heap = nv30->screen->vp_exec_heap;
91 struct nv30_shader_reloc *reloc = vp->branch_relocs.data;
92 unsigned nr_reloc = vp->branch_relocs.size / sizeof(*reloc);
93 uint32_t *inst, target;
94
95 if (nouveau_heap_alloc(heap, vp->nr_insns, &vp->exec, &vp->exec)) {
96 while (heap->next && heap->size < vp->nr_insns) {
97 struct nouveau_heap **evict = heap->next->priv;
98 nouveau_heap_free(evict);
99 }
100
101 if (nouveau_heap_alloc(heap, vp->nr_insns, &vp->exec, &vp->exec)) {
102 nv30->draw_flags |= NV30_NEW_VERTPROG;
103 return;
104 }
105 }
106
107 if (eng3d->oclass < NV40_3D_CLASS) {
108 while (nr_reloc--) {
109 inst = vp->insns[reloc->location].data;
110 target = vp->exec->start + reloc->target;
111
112 inst[2] &= ~0x000007fc;
113 inst[2] |= target << 2;
114 reloc++;
115 }
116 } else {
117 while (nr_reloc--) {
118 inst = vp->insns[reloc->location].data;
119 target = vp->exec->start + reloc->target;
120
121 inst[2] &= ~0x0000003f;
122 inst[2] |= target >> 3;
123 inst[3] &= ~0xe0000000;
124 inst[3] |= target << 29;
125 reloc++;
126 }
127 }
128
129 upload_code = true;
130 }
131
132 if (vp->nr_consts && !vp->data) {
133 struct nouveau_heap *heap = nv30->screen->vp_data_heap;
134 struct nv30_shader_reloc *reloc = vp->const_relocs.data;
135 unsigned nr_reloc = vp->const_relocs.size / sizeof(*reloc);
136 uint32_t *inst, target;
137
138 if (nouveau_heap_alloc(heap, vp->nr_consts, vp, &vp->data)) {
139 while (heap->next && heap->size < vp->nr_consts) {
140 struct nv30_vertprog *evp = heap->next->priv;
141 nouveau_heap_free(&evp->data);
142 }
143
144 if (nouveau_heap_alloc(heap, vp->nr_consts, vp, &vp->data)) {
145 nv30->draw_flags |= NV30_NEW_VERTPROG;
146 return;
147 }
148 }
149
150 if (eng3d->oclass < NV40_3D_CLASS) {
151 while (nr_reloc--) {
152 inst = vp->insns[reloc->location].data;
153 target = vp->data->start + reloc->target;
154
155 inst[1] &= ~0x0007fc000;
156 inst[1] |= (target & 0x1ff) << 14;
157 reloc++;
158 }
159 } else {
160 while (nr_reloc--) {
161 inst = vp->insns[reloc->location].data;
162 target = vp->data->start + reloc->target;
163
164 inst[1] &= ~0x0001ff000;
165 inst[1] |= (target & 0x1ff) << 12;
166 reloc++;
167 }
168 }
169
170 upload_code = true;
171 upload_data = true;
172 }
173
174 if (vp->nr_consts) {
175 struct nv04_resource *res = nv04_resource(nv30->vertprog.constbuf);
176
177 for (i = 0; i < vp->nr_consts; i++) {
178 struct nv30_vertprog_data *data = &vp->consts[i];
179
180 if (data->index < 0) {
181 if (!upload_data)
182 continue;
183 } else {
184 float *constbuf = (float *)res->data;
185 if (!upload_data &&
186 !memcmp(data->value, &constbuf[data->index * 4], 16))
187 continue;
188 memcpy(data->value, &constbuf[data->index * 4], 16);
189 }
190
191 BEGIN_NV04(push, NV30_3D(VP_UPLOAD_CONST_ID), 5);
192 PUSH_DATA (push, vp->data->start + i);
193 PUSH_DATAp(push, data->value, 4);
194 }
195 }
196
197 if (upload_code) {
198 BEGIN_NV04(push, NV30_3D(VP_UPLOAD_FROM_ID), 1);
199 PUSH_DATA (push, vp->exec->start);
200 for (i = 0; i < vp->nr_insns; i++) {
201 BEGIN_NV04(push, NV30_3D(VP_UPLOAD_INST(0)), 4);
202 PUSH_DATAp(push, vp->insns[i].data, 4);
203 }
204 }
205
206 if (nv30->dirty & (NV30_NEW_VERTPROG | NV30_NEW_FRAGPROG)) {
207 BEGIN_NV04(push, NV30_3D(VP_START_FROM_ID), 1);
208 PUSH_DATA (push, vp->exec->start);
209 if (eng3d->oclass < NV40_3D_CLASS) {
210 BEGIN_NV04(push, NV30_3D(ENGINE), 1);
211 PUSH_DATA (push, 0x00000013); /* vp instead of ff, somehow */
212 } else {
213 BEGIN_NV04(push, NV40_3D(VP_ATTRIB_EN), 2);
214 PUSH_DATA (push, vp->ir);
215 PUSH_DATA (push, vp->or | fp->vp_or);
216 BEGIN_NV04(push, NV30_3D(ENGINE), 1);
217 PUSH_DATA (push, 0x00000011);
218 }
219 }
220 }
221
222 static void *
nv30_vp_state_create(struct pipe_context * pipe,const struct pipe_shader_state * cso)223 nv30_vp_state_create(struct pipe_context *pipe,
224 const struct pipe_shader_state *cso)
225 {
226 struct nv30_vertprog *vp = CALLOC_STRUCT(nv30_vertprog);
227 if (!vp)
228 return NULL;
229
230 if (cso->type == PIPE_SHADER_IR_NIR) {
231 vp->pipe.tokens = nir_to_tgsi(cso->ir.nir, pipe->screen);
232 } else {
233 assert(cso->type == PIPE_SHADER_IR_TGSI);
234 /* we need to keep a local copy of the tokens */
235 vp->pipe.tokens = tgsi_dup_tokens(cso->tokens);
236 }
237
238 tgsi_scan_shader(vp->pipe.tokens, &vp->info);
239 return vp;
240 }
241
242 static void
nv30_vp_state_delete(struct pipe_context * pipe,void * hwcso)243 nv30_vp_state_delete(struct pipe_context *pipe, void *hwcso)
244 {
245 struct nv30_vertprog *vp = hwcso;
246
247 if (vp->translated)
248 nv30_vertprog_destroy(vp);
249
250 if (vp->draw)
251 draw_delete_vertex_shader(nv30_context(pipe)->draw, vp->draw);
252
253 FREE((void *)vp->pipe.tokens);
254 FREE(vp);
255 }
256
257 static void
nv30_vp_state_bind(struct pipe_context * pipe,void * hwcso)258 nv30_vp_state_bind(struct pipe_context *pipe, void *hwcso)
259 {
260 struct nv30_context *nv30 = nv30_context(pipe);
261
262 nv30->vertprog.program = hwcso;
263 nv30->dirty |= NV30_NEW_VERTPROG;
264 }
265
266 void
nv30_vertprog_init(struct pipe_context * pipe)267 nv30_vertprog_init(struct pipe_context *pipe)
268 {
269 pipe->create_vs_state = nv30_vp_state_create;
270 pipe->bind_vs_state = nv30_vp_state_bind;
271 pipe->delete_vs_state = nv30_vp_state_delete;
272 }
273