Path: blob/21.2-virgl/src/gallium/drivers/nouveau/nv30/nv30_texture.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"26#include "util/format/u_format.h"2728#include "nv_object.xml.h"29#include "nv30/nv30-40_3d.xml.h"30#include "nv30/nv30_context.h"31#include "nv30/nv30_format.h"3233#define NV30_3D_TEX_WRAP_S_MIRROR_REPEAT NV30_3D_TEX_WRAP_S_MIRRORED_REPEAT34#define NV30_WRAP(n) \35case PIPE_TEX_WRAP_##n: ret = NV30_3D_TEX_WRAP_S_##n; break36#define NV40_WRAP(n) \37case PIPE_TEX_WRAP_##n: ret = NV40_3D_TEX_WRAP_S_##n; break3839static inline unsigned40wrap_mode(unsigned pipe)41{42unsigned ret = NV30_3D_TEX_WRAP_S_REPEAT;4344switch (pipe) {45NV30_WRAP(REPEAT);46NV30_WRAP(MIRROR_REPEAT);47NV30_WRAP(CLAMP_TO_EDGE);48NV30_WRAP(CLAMP_TO_BORDER);49NV30_WRAP(CLAMP);50NV40_WRAP(MIRROR_CLAMP_TO_EDGE);51NV40_WRAP(MIRROR_CLAMP_TO_BORDER);52NV40_WRAP(MIRROR_CLAMP);53default:54break;55}5657return ret >> NV30_3D_TEX_WRAP_S__SHIFT;58}5960static inline unsigned61filter_mode(const struct pipe_sampler_state *cso)62{63unsigned filter;6465switch (cso->mag_img_filter) {66case PIPE_TEX_FILTER_LINEAR:67filter = NV30_3D_TEX_FILTER_MAG_LINEAR;68break;69default:70filter = NV30_3D_TEX_FILTER_MAG_NEAREST;71break;72}7374switch (cso->min_img_filter) {75case PIPE_TEX_FILTER_LINEAR:76switch (cso->min_mip_filter) {77case PIPE_TEX_MIPFILTER_NEAREST:78filter |= NV30_3D_TEX_FILTER_MIN_LINEAR_MIPMAP_NEAREST;79break;80case PIPE_TEX_MIPFILTER_LINEAR:81filter |= NV30_3D_TEX_FILTER_MIN_LINEAR_MIPMAP_LINEAR;82break;83default:84filter |= NV30_3D_TEX_FILTER_MIN_LINEAR;85break;86}87break;88default:89switch (cso->min_mip_filter) {90case PIPE_TEX_MIPFILTER_NEAREST:91filter |= NV30_3D_TEX_FILTER_MIN_NEAREST_MIPMAP_NEAREST;92break;93case PIPE_TEX_MIPFILTER_LINEAR:94filter |= NV30_3D_TEX_FILTER_MIN_NEAREST_MIPMAP_LINEAR;95break;96default:97filter |= NV30_3D_TEX_FILTER_MIN_NEAREST;98break;99}100break;101}102103return filter;104}105106static inline unsigned107compare_mode(const struct pipe_sampler_state *cso)108{109if (cso->compare_mode != PIPE_TEX_COMPARE_R_TO_TEXTURE)110return 0;111112switch (cso->compare_func) {113case PIPE_FUNC_NEVER : return NV30_3D_TEX_WRAP_RCOMP_NEVER;114case PIPE_FUNC_GREATER : return NV30_3D_TEX_WRAP_RCOMP_GREATER;115case PIPE_FUNC_EQUAL : return NV30_3D_TEX_WRAP_RCOMP_EQUAL;116case PIPE_FUNC_GEQUAL : return NV30_3D_TEX_WRAP_RCOMP_GEQUAL;117case PIPE_FUNC_LESS : return NV30_3D_TEX_WRAP_RCOMP_LESS;118case PIPE_FUNC_NOTEQUAL: return NV30_3D_TEX_WRAP_RCOMP_NOTEQUAL;119case PIPE_FUNC_LEQUAL : return NV30_3D_TEX_WRAP_RCOMP_LEQUAL;120case PIPE_FUNC_ALWAYS : return NV30_3D_TEX_WRAP_RCOMP_ALWAYS;121default:122return 0;123}124}125126static void *127nv30_sampler_state_create(struct pipe_context *pipe,128const struct pipe_sampler_state *cso)129{130struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;131struct nv30_sampler_state *so;132const float max_lod = 15.0 + (255.0 / 256.0);133134so = MALLOC_STRUCT(nv30_sampler_state);135if (!so)136return NULL;137138so->pipe = *cso;139so->fmt = 0;140so->wrap = (wrap_mode(cso->wrap_s) << NV30_3D_TEX_WRAP_S__SHIFT) |141(wrap_mode(cso->wrap_t) << NV30_3D_TEX_WRAP_T__SHIFT) |142(wrap_mode(cso->wrap_r) << NV30_3D_TEX_WRAP_R__SHIFT);143so->en = 0;144so->wrap |= compare_mode(cso);145so->filt = filter_mode(cso) | 0x00002000;146so->bcol = (float_to_ubyte(cso->border_color.f[3]) << 24) |147(float_to_ubyte(cso->border_color.f[0]) << 16) |148(float_to_ubyte(cso->border_color.f[1]) << 8) |149(float_to_ubyte(cso->border_color.f[2]) << 0);150151if (eng3d->oclass >= NV40_3D_CLASS) {152unsigned aniso = cso->max_anisotropy;153154if (!cso->normalized_coords)155so->fmt |= NV40_3D_TEX_FORMAT_RECT;156157if (aniso > 1) {158if (aniso >= 16) so->en |= NV40_3D_TEX_ENABLE_ANISO_16X;159else if (aniso >= 12) so->en |= NV40_3D_TEX_ENABLE_ANISO_12X;160else if (aniso >= 10) so->en |= NV40_3D_TEX_ENABLE_ANISO_10X;161else if (aniso >= 8) so->en |= NV40_3D_TEX_ENABLE_ANISO_8X;162else if (aniso >= 6) so->en |= NV40_3D_TEX_ENABLE_ANISO_6X;163else if (aniso >= 4) so->en |= NV40_3D_TEX_ENABLE_ANISO_4X;164else so->en |= NV40_3D_TEX_ENABLE_ANISO_2X;165166so->wrap |= nv30_context(pipe)->config.aniso;167}168} else {169so->en |= NV30_3D_TEX_ENABLE_ENABLE;170171if (cso->max_anisotropy >= 8) so->en |= NV30_3D_TEX_ENABLE_ANISO_8X;172else if (cso->max_anisotropy >= 4) so->en |= NV30_3D_TEX_ENABLE_ANISO_4X;173else if (cso->max_anisotropy >= 2) so->en |= NV30_3D_TEX_ENABLE_ANISO_2X;174}175176so->filt |= (int)(cso->lod_bias * 256.0) & 0x1fff;177so->max_lod = (int)(CLAMP(cso->max_lod, 0.0, max_lod) * 256.0);178so->min_lod = (int)(CLAMP(cso->min_lod, 0.0, max_lod) * 256.0);179return so;180}181182static void183nv30_sampler_state_delete(struct pipe_context *pipe, void *hwcso)184{185FREE(hwcso);186}187188static void189nv30_bind_sampler_states(struct pipe_context *pipe,190enum pipe_shader_type shader, unsigned start_slot,191unsigned num_samplers, void **samplers)192{193switch (shader) {194case PIPE_SHADER_VERTEX:195nv40_verttex_sampler_states_bind(pipe, num_samplers, samplers);196break;197case PIPE_SHADER_FRAGMENT:198nv30_fragtex_sampler_states_bind(pipe, num_samplers, samplers);199break;200default:201assert(!"unexpected shader type");202break;203}204}205206static inline uint32_t207swizzle(const struct nv30_texfmt *fmt, unsigned cmp, unsigned swz)208{209uint32_t data = fmt->swz[swz].src << 8;210if (swz <= PIPE_SWIZZLE_W)211data |= fmt->swz[swz].cmp;212else213data |= fmt->swz[cmp].cmp;214return data;215}216217static struct pipe_sampler_view *218nv30_sampler_view_create(struct pipe_context *pipe, struct pipe_resource *pt,219const struct pipe_sampler_view *tmpl)220{221const struct nv30_texfmt *fmt = nv30_texfmt(pipe->screen, tmpl->format);222struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;223struct nv30_miptree *mt = nv30_miptree(pt);224struct nv30_sampler_view *so;225226so = MALLOC_STRUCT(nv30_sampler_view);227if (!so)228return NULL;229so->pipe = *tmpl;230so->pipe.reference.count = 1;231so->pipe.texture = NULL;232so->pipe.context = pipe;233pipe_resource_reference(&so->pipe.texture, pt);234235so->fmt = NV30_3D_TEX_FORMAT_NO_BORDER;236switch (pt->target) {237case PIPE_TEXTURE_1D:238so->fmt |= NV30_3D_TEX_FORMAT_DIMS_1D;239break;240case PIPE_TEXTURE_CUBE:241so->fmt |= NV30_3D_TEX_FORMAT_CUBIC;242FALLTHROUGH;243case PIPE_TEXTURE_2D:244case PIPE_TEXTURE_RECT:245so->fmt |= NV30_3D_TEX_FORMAT_DIMS_2D;246break;247case PIPE_TEXTURE_3D:248so->fmt |= NV30_3D_TEX_FORMAT_DIMS_3D;249break;250default:251assert(0);252so->fmt |= NV30_3D_TEX_FORMAT_DIMS_1D;253break;254}255256so->filt = fmt->filter;257so->wrap = fmt->wrap;258so->swz = fmt->swizzle;259so->swz |= swizzle(fmt, 3, tmpl->swizzle_a);260so->swz |= swizzle(fmt, 0, tmpl->swizzle_r) << 2;261so->swz |= swizzle(fmt, 1, tmpl->swizzle_g) << 4;262so->swz |= swizzle(fmt, 2, tmpl->swizzle_b) << 6;263264/* apparently, we need to ignore the t coordinate for 1D textures to265* fix piglit tex1d-2dborder266*/267so->wrap_mask = ~0;268if (pt->target == PIPE_TEXTURE_1D) {269so->wrap_mask &= ~NV30_3D_TEX_WRAP_T__MASK;270so->wrap |= NV30_3D_TEX_WRAP_T_REPEAT;271}272273/* yet more hardware suckage, can't filter 32-bit float formats */274switch (tmpl->format) {275case PIPE_FORMAT_R32_FLOAT:276case PIPE_FORMAT_R32G32B32A32_FLOAT:277so->filt_mask = ~(NV30_3D_TEX_FILTER_MIN__MASK |278NV30_3D_TEX_FILTER_MAG__MASK);279so->filt |= NV30_3D_TEX_FILTER_MIN_NEAREST |280NV30_3D_TEX_FILTER_MAG_NEAREST;281break;282default:283so->filt_mask = ~0;284break;285}286287so->npot_size0 = (pt->width0 << 16) | pt->height0;288if (eng3d->oclass >= NV40_3D_CLASS) {289so->npot_size1 = (pt->depth0 << 20) | mt->uniform_pitch;290if (mt->uniform_pitch)291so->fmt |= NV40_3D_TEX_FORMAT_LINEAR;292so->fmt |= 0x00008000;293so->fmt |= (pt->last_level + 1) << NV40_3D_TEX_FORMAT_MIPMAP_COUNT__SHIFT;294} else {295so->swz |= mt->uniform_pitch << NV30_3D_TEX_SWIZZLE_RECT_PITCH__SHIFT;296if (pt->last_level)297so->fmt |= NV30_3D_TEX_FORMAT_MIPMAP;298so->fmt |= util_logbase2(pt->width0) << 20;299so->fmt |= util_logbase2(pt->height0) << 24;300so->fmt |= util_logbase2(pt->depth0) << 28;301so->fmt |= 0x00010000;302}303304so->base_lod = so->pipe.u.tex.first_level << 8;305so->high_lod = MIN2(pt->last_level, so->pipe.u.tex.last_level) << 8;306return &so->pipe;307}308309static void310nv30_sampler_view_destroy(struct pipe_context *pipe,311struct pipe_sampler_view *view)312{313pipe_resource_reference(&view->texture, NULL);314FREE(view);315}316317void318nv30_texture_init(struct pipe_context *pipe)319{320pipe->create_sampler_state = nv30_sampler_state_create;321pipe->delete_sampler_state = nv30_sampler_state_delete;322pipe->bind_sampler_states = nv30_bind_sampler_states;323324pipe->create_sampler_view = nv30_sampler_view_create;325pipe->sampler_view_destroy = nv30_sampler_view_destroy;326}327328329