| 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 "tgsi/tgsi_parse.h" |
| 28 |
#include "nir/nir_to_tgsi.h" |
| 29 |
|
| 30 |
#include "nv_object.xml.h" |
| 31 |
#include "nv30/nv30-40_3d.xml.h" |
| 32 |
#include "nv30/nv30_context.h" |
| 33 |
#include "nv30/nv30_winsys.h" |
| 34 |
#include "nv30/nvfx_shader.h" |
| 35 |
|
| 36 |
static void |
| 37 |
nv30_fragprog_upload(struct nv30_context *nv30) |
| 38 |
{ |
| 39 |
struct nouveau_context *nv = &nv30->base; |
| 40 |
struct nv30_fragprog *fp = nv30->fragprog.program; |
| 41 |
struct pipe_context *pipe = &nv30->base.pipe; |
| 42 |
|
| 43 |
if (unlikely(!fp->buffer)) |
| 44 |
fp->buffer = pipe_buffer_create(pipe->screen, 0, 0, fp->insn_len * 4); |
| 45 |
|
| 46 |
#if !UTIL_ARCH_BIG_ENDIAN |
| 47 |
pipe_buffer_write(pipe, fp->buffer, 0, fp->insn_len * 4, fp->insn); |
| 48 |
#else |
| 49 |
{ |
| 50 |
struct pipe_transfer *transfer; |
| 51 |
uint32_t *map; |
| 52 |
int i; |
| 53 |
|
| 54 |
map = pipe_buffer_map(pipe, fp->buffer, |
| 55 |
PIPE_MAP_WRITE | PIPE_MAP_DISCARD_WHOLE_RESOURCE, |
| 56 |
&transfer); |
| 57 |
for (i = 0; i < fp->insn_len; i++) |
| 58 |
*map++ = (fp->insn[i] >> 16) | (fp->insn[i] << 16); |
| 59 |
pipe_buffer_unmap(pipe, transfer); |
| 60 |
} |
| 61 |
#endif |
| 62 |
|
| 63 |
if (nv04_resource(fp->buffer)->domain != NOUVEAU_BO_VRAM) |
| 64 |
nouveau_buffer_migrate(nv, nv04_resource(fp->buffer), NOUVEAU_BO_VRAM); |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
nv30_fragprog_validate(struct nv30_context *nv30) |
| 69 |
{ |
| 70 |
struct nouveau_pushbuf *push = nv30->base.pushbuf; |
| 71 |
struct nouveau_object *eng3d = nv30->screen->eng3d; |
| 72 |
struct nv30_fragprog *fp = nv30->fragprog.program; |
| 73 |
bool upload = false; |
| 74 |
int i; |
| 75 |
|
| 76 |
if (!fp->translated) { |
| 77 |
_nvfx_fragprog_translate(eng3d->oclass, fp); |
| 78 |
if (!fp->translated) |
| 79 |
return; |
| 80 |
|
| 81 |
upload = true; |
| 82 |
} |
| 83 |
|
| 84 |
/* update constants, also needs to be done on every fp switch as we |
| 85 |
* have no idea whether the constbuf changed in the meantime |
| 86 |
*/ |
| 87 |
if (nv30->fragprog.constbuf) { |
| 88 |
struct pipe_resource *constbuf = nv30->fragprog.constbuf; |
| 89 |
uint32_t *cbuf = (uint32_t *)nv04_resource(constbuf)->data; |
| 90 |
|
| 91 |
for (i = 0; i < fp->nr_consts; i++) { |
| 92 |
unsigned off = fp->consts[i].offset; |
| 93 |
unsigned idx = fp->consts[i].index * 4; |
| 94 |
|
| 95 |
if (!memcmp(&fp->insn[off], &cbuf[idx], 4 * 4)) |
| 96 |
continue; |
| 97 |
memcpy(&fp->insn[off], &cbuf[idx], 4 * 4); |
| 98 |
upload = true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if (upload) |
| 103 |
nv30_fragprog_upload(nv30); |
| 104 |
|
| 105 |
/* FP_ACTIVE_PROGRAM needs to be done again even if only the consts |
| 106 |
* were updated. TEX_CACHE_CTL magic is not enough to convince the |
| 107 |
* GPU that it should re-read the fragprog from VRAM... sigh. |
| 108 |
*/ |
| 109 |
if (nv30->state.fragprog != fp || upload) { |
| 110 |
struct nv04_resource *r = nv04_resource(fp->buffer); |
| 111 |
|
| 112 |
if (!PUSH_SPACE(push, 8)) |
| 113 |
return; |
| 114 |
PUSH_RESET(push, BUFCTX_FRAGPROG); |
| 115 |
|
| 116 |
BEGIN_NV04(push, NV30_3D(FP_ACTIVE_PROGRAM), 1); |
| 117 |
PUSH_RESRC(push, NV30_3D(FP_ACTIVE_PROGRAM), BUFCTX_FRAGPROG, r, 0, |
| 118 |
NOUVEAU_BO_LOW | NOUVEAU_BO_RD | NOUVEAU_BO_OR, |
| 119 |
NV30_3D_FP_ACTIVE_PROGRAM_DMA0, |
| 120 |
NV30_3D_FP_ACTIVE_PROGRAM_DMA1); |
| 121 |
BEGIN_NV04(push, NV30_3D(FP_CONTROL), 1); |
| 122 |
PUSH_DATA (push, fp->fp_control); |
| 123 |
if (eng3d->oclass < NV40_3D_CLASS) { |
| 124 |
BEGIN_NV04(push, NV30_3D(FP_REG_CONTROL), 1); |
| 125 |
PUSH_DATA (push, 0x00010004); |
| 126 |
BEGIN_NV04(push, NV30_3D(TEX_UNITS_ENABLE), 1); |
| 127 |
PUSH_DATA (push, fp->texcoords); |
| 128 |
} else { |
| 129 |
BEGIN_NV04(push, SUBC_3D(0x0b40), 1); |
| 130 |
PUSH_DATA (push, 0x00000000); |
| 131 |
} |
| 132 |
|
| 133 |
nv30->state.fragprog = fp; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static void * |
| 138 |
nv30_fp_state_create(struct pipe_context *pipe, |
| 139 |
const struct pipe_shader_state *cso) |
| 140 |
{ |
| 141 |
struct nv30_fragprog *fp = CALLOC_STRUCT(nv30_fragprog); |
| 142 |
if (!fp) |
| 143 |
return NULL; |
| 144 |
|
| 145 |
if (cso->type == PIPE_SHADER_IR_NIR) { |
| 146 |
fp->pipe.tokens = nir_to_tgsi(cso->ir.nir, pipe->screen); |
| 147 |
} else { |
| 148 |
assert(cso->type == PIPE_SHADER_IR_TGSI); |
| 149 |
/* we need to keep a local copy of the tokens */ |
| 150 |
fp->pipe.tokens = tgsi_dup_tokens(cso->tokens); |
| 151 |
} |
| 152 |
|
| 153 |
tgsi_scan_shader(fp->pipe.tokens, &fp->info); |
| 154 |
return fp; |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
nv30_fp_state_delete(struct pipe_context *pipe, void *hwcso) |
| 159 |
{ |
| 160 |
struct nv30_fragprog *fp = hwcso; |
| 161 |
|
| 162 |
pipe_resource_reference(&fp->buffer, NULL); |
| 163 |
|
| 164 |
if (fp->draw) |
| 165 |
draw_delete_fragment_shader(nv30_context(pipe)->draw, fp->draw); |
| 166 |
|
| 167 |
FREE((void *)fp->pipe.tokens); |
| 168 |
FREE(fp->insn); |
| 169 |
FREE(fp->consts); |
| 170 |
FREE(fp); |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
nv30_fp_state_bind(struct pipe_context *pipe, void *hwcso) |
| 175 |
{ |
| 176 |
struct nv30_context *nv30 = nv30_context(pipe); |
| 177 |
struct nv30_fragprog *fp = hwcso; |
| 178 |
|
| 179 |
/* reset the bucftx so that we don't keep a dangling reference to the fp |
| 180 |
* code |
| 181 |
*/ |
| 182 |
if (fp != nv30->state.fragprog) |
| 183 |
nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FRAGPROG); |
| 184 |
|
| 185 |
nv30->fragprog.program = fp; |
| 186 |
nv30->dirty |= NV30_NEW_FRAGPROG; |
| 187 |
} |
| 188 |
|
| 189 |
void |
| 190 |
nv30_fragprog_init(struct pipe_context *pipe) |
| 191 |
{ |
| 192 |
pipe->create_fs_state = nv30_fp_state_create; |
| 193 |
pipe->bind_fs_state = nv30_fp_state_bind; |
| 194 |
pipe->delete_fs_state = nv30_fp_state_delete; |
| 195 |
} |