Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nv30/nv30_fragtex.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 "util/u_inlines.h"2627#include "nv_object.xml.h"28#include "nv30/nv30-40_3d.xml.h"29#include "nv30/nv30_context.h"30#include "nv30/nv30_format.h"3132void33nv30_fragtex_validate(struct nv30_context *nv30)34{35struct pipe_screen *pscreen = &nv30->screen->base.base;36struct nouveau_object *eng3d = nv30->screen->eng3d;37struct nouveau_pushbuf *push = nv30->base.pushbuf;38unsigned dirty = nv30->fragprog.dirty_samplers;3940while (dirty) {41unsigned unit = ffs(dirty) - 1;42struct nv30_sampler_view *sv = (void *)nv30->fragprog.textures[unit];43struct nv30_sampler_state *ss = nv30->fragprog.samplers[unit];4445PUSH_RESET(push, BUFCTX_FRAGTEX(unit));4647if (ss && sv) {48const struct nv30_texfmt *fmt = nv30_texfmt(pscreen, sv->pipe.format);49struct pipe_resource *pt = sv->pipe.texture;50struct nv30_miptree *mt = nv30_miptree(pt);51unsigned min_lod, max_lod;52u32 filter = sv->filt | (ss->filt & sv->filt_mask);53u32 format = sv->fmt | ss->fmt;54u32 enable = ss->en;5556/* handle base_level when not using a mip filter, min/max level57* is unfortunately ignored by the hardware otherwise58*/59if (ss->pipe.min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {60if (sv->base_lod)61filter += 0x00020000; /* N/L -> NMN/LMN */62max_lod = sv->base_lod;63min_lod = sv->base_lod;64} else {65max_lod = MIN2(ss->max_lod + sv->base_lod, sv->high_lod);66min_lod = MIN2(ss->min_lod + sv->base_lod, max_lod);67}6869if (eng3d->oclass >= NV40_3D_CLASS) {70/* this is a tad stupid of the hardware, but there's no non-rcomp71* z16/z24 texture formats to be had, we have to suffer and lose72* some precision to handle this case.73*/74if (ss->pipe.compare_mode != PIPE_TEX_COMPARE_R_TO_TEXTURE) {75if (fmt->nv40 == NV40_3D_TEX_FORMAT_FORMAT_Z16)76format |= NV40_3D_TEX_FORMAT_FORMAT_A8L8;77else78if (fmt->nv40 == NV40_3D_TEX_FORMAT_FORMAT_Z24)79format |= NV40_3D_TEX_FORMAT_FORMAT_A16L16;80else81format |= fmt->nv40;82} else {83format |= fmt->nv40;84}8586enable |= (min_lod << 19) | (max_lod << 7);87enable |= NV40_3D_TEX_ENABLE_ENABLE;8889BEGIN_NV04(push, NV40_3D(TEX_SIZE1(unit)), 1);90PUSH_DATA (push, sv->npot_size1);91} else {92/* this is a tad stupid of the hardware, but there's no non-rcomp93* z16/z24 texture formats to be had, we have to suffer and lose94* some precision to handle this case.95*/96if (ss->pipe.compare_mode != PIPE_TEX_COMPARE_R_TO_TEXTURE) {97if (fmt->nv30 == NV30_3D_TEX_FORMAT_FORMAT_Z16) {98if (ss->pipe.normalized_coords)99format |= NV30_3D_TEX_FORMAT_FORMAT_A8L8;100else101format |= NV30_3D_TEX_FORMAT_FORMAT_A8L8_RECT;102} else103if (fmt->nv30 == NV30_3D_TEX_FORMAT_FORMAT_Z24) {104if (ss->pipe.normalized_coords)105format |= NV30_3D_TEX_FORMAT_FORMAT_HILO16;106else107format |= NV30_3D_TEX_FORMAT_FORMAT_HILO16_RECT;108} else {109if (ss->pipe.normalized_coords)110format |= fmt->nv30;111else112format |= fmt->nv30_rect;113}114} else {115if (ss->pipe.normalized_coords)116format |= fmt->nv30;117else118format |= fmt->nv30_rect;119}120121enable |= NV30_3D_TEX_ENABLE_ENABLE;122enable |= (min_lod << 18) | (max_lod << 6);123}124125BEGIN_NV04(push, NV30_3D(TEX_OFFSET(unit)), 8);126PUSH_MTHDl(push, NV30_3D(TEX_OFFSET(unit)), BUFCTX_FRAGTEX(unit),127mt->base.bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);128PUSH_MTHDs(push, NV30_3D(TEX_FORMAT(unit)), BUFCTX_FRAGTEX(unit),129mt->base.bo, format, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD,130NV30_3D_TEX_FORMAT_DMA0,131NV30_3D_TEX_FORMAT_DMA1);132PUSH_DATA (push, sv->wrap | (ss->wrap & sv->wrap_mask));133PUSH_DATA (push, enable);134PUSH_DATA (push, sv->swz);135PUSH_DATA (push, filter);136PUSH_DATA (push, sv->npot_size0);137PUSH_DATA (push, ss->bcol);138BEGIN_NV04(push, NV30_3D(TEX_FILTER_OPTIMIZATION(unit)), 1);139PUSH_DATA (push, nv30->config.filter);140} else {141BEGIN_NV04(push, NV30_3D(TEX_ENABLE(unit)), 1);142PUSH_DATA (push, 0);143}144145dirty &= ~(1 << unit);146}147148nv30->fragprog.dirty_samplers = 0;149}150151void152nv30_fragtex_sampler_states_bind(struct pipe_context *pipe,153unsigned nr, void **hwcso)154{155struct nv30_context *nv30 = nv30_context(pipe);156unsigned i;157158for (i = 0; i < nr; i++) {159nv30->fragprog.samplers[i] = hwcso[i];160nv30->fragprog.dirty_samplers |= (1 << i);161}162163for (; i < nv30->fragprog.num_samplers; i++) {164nv30->fragprog.samplers[i] = NULL;165nv30->fragprog.dirty_samplers |= (1 << i);166}167168nv30->fragprog.num_samplers = nr;169nv30->dirty |= NV30_NEW_FRAGTEX;170}171172173void174nv30_fragtex_set_sampler_views(struct pipe_context *pipe, unsigned nr,175struct pipe_sampler_view **views)176{177struct nv30_context *nv30 = nv30_context(pipe);178unsigned i;179180for (i = 0; i < nr; i++) {181nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FRAGTEX(i));182pipe_sampler_view_reference(&nv30->fragprog.textures[i], views[i]);183nv30->fragprog.dirty_samplers |= (1 << i);184}185186for (; i < nv30->fragprog.num_textures; i++) {187nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FRAGTEX(i));188pipe_sampler_view_reference(&nv30->fragprog.textures[i], NULL);189nv30->fragprog.dirty_samplers |= (1 << i);190}191192nv30->fragprog.num_textures = nr;193nv30->dirty |= NV30_NEW_FRAGTEX;194}195196197static void198nv30_set_sampler_views(struct pipe_context *pipe, enum pipe_shader_type shader,199unsigned start, unsigned nr,200unsigned unbind_num_trailing_slots,201struct pipe_sampler_view **views)202{203assert(start == 0);204switch (shader) {205case PIPE_SHADER_FRAGMENT:206nv30_fragtex_set_sampler_views(pipe, nr, views);207break;208case PIPE_SHADER_VERTEX:209nv40_verttex_set_sampler_views(pipe, nr, views);210break;211default:212;213}214}215216217void218nv30_fragtex_init(struct pipe_context *pipe)219{220pipe->set_sampler_views = nv30_set_sampler_views;221}222223224