Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nv30/nv30_fragprog.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 "tgsi/tgsi_parse.h"2728#include "nv_object.xml.h"29#include "nv30/nv30-40_3d.xml.h"30#include "nv30/nv30_context.h"31#include "nv30/nvfx_shader.h"3233static void34nv30_fragprog_upload(struct nv30_context *nv30)35{36struct nouveau_context *nv = &nv30->base;37struct nv30_fragprog *fp = nv30->fragprog.program;38struct pipe_context *pipe = &nv30->base.pipe;3940if (unlikely(!fp->buffer))41fp->buffer = pipe_buffer_create(pipe->screen, 0, 0, fp->insn_len * 4);4243#if !UTIL_ARCH_BIG_ENDIAN44pipe_buffer_write(pipe, fp->buffer, 0, fp->insn_len * 4, fp->insn);45#else46{47struct pipe_transfer *transfer;48uint32_t *map;49int i;5051map = pipe_buffer_map(pipe, fp->buffer,52PIPE_MAP_WRITE | PIPE_MAP_DISCARD_WHOLE_RESOURCE,53&transfer);54for (i = 0; i < fp->insn_len; i++)55*map++ = (fp->insn[i] >> 16) | (fp->insn[i] << 16);56pipe_buffer_unmap(pipe, transfer);57}58#endif5960if (nv04_resource(fp->buffer)->domain != NOUVEAU_BO_VRAM)61nouveau_buffer_migrate(nv, nv04_resource(fp->buffer), NOUVEAU_BO_VRAM);62}6364void65nv30_fragprog_validate(struct nv30_context *nv30)66{67struct nouveau_pushbuf *push = nv30->base.pushbuf;68struct nouveau_object *eng3d = nv30->screen->eng3d;69struct nv30_fragprog *fp = nv30->fragprog.program;70bool upload = false;71int i;7273if (!fp->translated) {74_nvfx_fragprog_translate(eng3d->oclass, fp);75if (!fp->translated)76return;7778upload = true;79}8081/* update constants, also needs to be done on every fp switch as we82* have no idea whether the constbuf changed in the meantime83*/84if (nv30->fragprog.constbuf) {85struct pipe_resource *constbuf = nv30->fragprog.constbuf;86uint32_t *cbuf = (uint32_t *)nv04_resource(constbuf)->data;8788for (i = 0; i < fp->nr_consts; i++) {89unsigned off = fp->consts[i].offset;90unsigned idx = fp->consts[i].index * 4;9192if (!memcmp(&fp->insn[off], &cbuf[idx], 4 * 4))93continue;94memcpy(&fp->insn[off], &cbuf[idx], 4 * 4);95upload = true;96}97}9899if (upload)100nv30_fragprog_upload(nv30);101102/* FP_ACTIVE_PROGRAM needs to be done again even if only the consts103* were updated. TEX_CACHE_CTL magic is not enough to convince the104* GPU that it should re-read the fragprog from VRAM... sigh.105*/106if (nv30->state.fragprog != fp || upload) {107struct nv04_resource *r = nv04_resource(fp->buffer);108109if (!PUSH_SPACE(push, 8))110return;111PUSH_RESET(push, BUFCTX_FRAGPROG);112113BEGIN_NV04(push, NV30_3D(FP_ACTIVE_PROGRAM), 1);114PUSH_RESRC(push, NV30_3D(FP_ACTIVE_PROGRAM), BUFCTX_FRAGPROG, r, 0,115NOUVEAU_BO_LOW | NOUVEAU_BO_RD | NOUVEAU_BO_OR,116NV30_3D_FP_ACTIVE_PROGRAM_DMA0,117NV30_3D_FP_ACTIVE_PROGRAM_DMA1);118BEGIN_NV04(push, NV30_3D(FP_CONTROL), 1);119PUSH_DATA (push, fp->fp_control);120if (eng3d->oclass < NV40_3D_CLASS) {121BEGIN_NV04(push, NV30_3D(FP_REG_CONTROL), 1);122PUSH_DATA (push, 0x00010004);123BEGIN_NV04(push, NV30_3D(TEX_UNITS_ENABLE), 1);124PUSH_DATA (push, fp->texcoords);125} else {126BEGIN_NV04(push, SUBC_3D(0x0b40), 1);127PUSH_DATA (push, 0x00000000);128}129130nv30->state.fragprog = fp;131}132}133134static void *135nv30_fp_state_create(struct pipe_context *pipe,136const struct pipe_shader_state *cso)137{138struct nv30_fragprog *fp = CALLOC_STRUCT(nv30_fragprog);139if (!fp)140return NULL;141142fp->pipe.tokens = tgsi_dup_tokens(cso->tokens);143tgsi_scan_shader(fp->pipe.tokens, &fp->info);144return fp;145}146147static void148nv30_fp_state_delete(struct pipe_context *pipe, void *hwcso)149{150struct nv30_fragprog *fp = hwcso;151152pipe_resource_reference(&fp->buffer, NULL);153154if (fp->draw)155draw_delete_fragment_shader(nv30_context(pipe)->draw, fp->draw);156157FREE((void *)fp->pipe.tokens);158FREE(fp->insn);159FREE(fp->consts);160FREE(fp);161}162163static void164nv30_fp_state_bind(struct pipe_context *pipe, void *hwcso)165{166struct nv30_context *nv30 = nv30_context(pipe);167struct nv30_fragprog *fp = hwcso;168169/* reset the bucftx so that we don't keep a dangling reference to the fp170* code171*/172if (fp != nv30->state.fragprog)173nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FRAGPROG);174175nv30->fragprog.program = fp;176nv30->dirty |= NV30_NEW_FRAGPROG;177}178179void180nv30_fragprog_init(struct pipe_context *pipe)181{182pipe->create_fs_state = nv30_fp_state_create;183pipe->bind_fs_state = nv30_fp_state_bind;184pipe->delete_fs_state = nv30_fp_state_delete;185}186187188