Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_pipe_sampler.c
4570 views
/**********************************************************1* Copyright 2008-2009 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/2425#include "pipe/p_defines.h"26#include "util/u_bitmask.h"27#include "util/format/u_format.h"28#include "util/u_inlines.h"29#include "util/u_math.h"30#include "util/u_memory.h"31#include "tgsi/tgsi_parse.h"3233#include "svga_context.h"34#include "svga_cmd.h"35#include "svga_debug.h"36#include "svga_resource_texture.h"37#include "svga_surface.h"38#include "svga_sampler_view.h"394041static inline unsigned42translate_wrap_mode(unsigned wrap)43{44switch (wrap) {45case PIPE_TEX_WRAP_REPEAT:46return SVGA3D_TEX_ADDRESS_WRAP;47case PIPE_TEX_WRAP_CLAMP:48return SVGA3D_TEX_ADDRESS_CLAMP;49case PIPE_TEX_WRAP_CLAMP_TO_EDGE:50/* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by51* hardware.52*/53return SVGA3D_TEX_ADDRESS_CLAMP;54case PIPE_TEX_WRAP_CLAMP_TO_BORDER:55return SVGA3D_TEX_ADDRESS_BORDER;56case PIPE_TEX_WRAP_MIRROR_REPEAT:57return SVGA3D_TEX_ADDRESS_MIRROR;58case PIPE_TEX_WRAP_MIRROR_CLAMP:59case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:60case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:61return SVGA3D_TEX_ADDRESS_MIRRORONCE;62default:63assert(0);64return SVGA3D_TEX_ADDRESS_WRAP;65}66}676869static inline unsigned70translate_img_filter(unsigned filter)71{72switch (filter) {73case PIPE_TEX_FILTER_NEAREST:74return SVGA3D_TEX_FILTER_NEAREST;75case PIPE_TEX_FILTER_LINEAR:76return SVGA3D_TEX_FILTER_LINEAR;77default:78assert(0);79return SVGA3D_TEX_FILTER_NEAREST;80}81}828384static inline unsigned85translate_mip_filter(unsigned filter)86{87switch (filter) {88case PIPE_TEX_MIPFILTER_NONE:89return SVGA3D_TEX_FILTER_NONE;90case PIPE_TEX_MIPFILTER_NEAREST:91return SVGA3D_TEX_FILTER_NEAREST;92case PIPE_TEX_MIPFILTER_LINEAR:93return SVGA3D_TEX_FILTER_LINEAR;94default:95assert(0);96return SVGA3D_TEX_FILTER_NONE;97}98}99100101static uint8102translate_comparison_func(unsigned func)103{104switch (func) {105case PIPE_FUNC_NEVER:106return SVGA3D_COMPARISON_NEVER;107case PIPE_FUNC_LESS:108return SVGA3D_COMPARISON_LESS;109case PIPE_FUNC_EQUAL:110return SVGA3D_COMPARISON_EQUAL;111case PIPE_FUNC_LEQUAL:112return SVGA3D_COMPARISON_LESS_EQUAL;113case PIPE_FUNC_GREATER:114return SVGA3D_COMPARISON_GREATER;115case PIPE_FUNC_NOTEQUAL:116return SVGA3D_COMPARISON_NOT_EQUAL;117case PIPE_FUNC_GEQUAL:118return SVGA3D_COMPARISON_GREATER_EQUAL;119case PIPE_FUNC_ALWAYS:120return SVGA3D_COMPARISON_ALWAYS;121default:122assert(!"Invalid comparison function");123return SVGA3D_COMPARISON_ALWAYS;124}125}126127128/**129* Translate filtering state to vgpu10 format.130*/131static SVGA3dFilter132translate_filter_mode(unsigned img_filter,133unsigned min_filter,134unsigned mag_filter,135boolean anisotropic,136boolean compare)137{138SVGA3dFilter mode = 0;139140if (img_filter == PIPE_TEX_FILTER_LINEAR)141mode |= SVGA3D_FILTER_MIP_LINEAR;142if (min_filter == PIPE_TEX_FILTER_LINEAR)143mode |= SVGA3D_FILTER_MIN_LINEAR;144if (mag_filter == PIPE_TEX_FILTER_LINEAR)145mode |= SVGA3D_FILTER_MAG_LINEAR;146if (anisotropic)147mode |= SVGA3D_FILTER_ANISOTROPIC;148if (compare)149mode |= SVGA3D_FILTER_COMPARE;150151return mode;152}153154155/**156* Define a vgpu10 sampler state.157*/158static void159define_sampler_state_object(struct svga_context *svga,160struct svga_sampler_state *ss,161const struct pipe_sampler_state *ps)162{163uint8_t max_aniso = (uint8_t) 255; /* XXX fix me */164boolean anisotropic;165uint8 compare_func;166SVGA3dFilter filter;167SVGA3dRGBAFloat bcolor;168float min_lod, max_lod;169170assert(svga_have_vgpu10(svga));171172anisotropic = ss->aniso_level > 1.0f;173174filter = translate_filter_mode(ps->min_mip_filter,175ps->min_img_filter,176ps->mag_img_filter,177anisotropic,178ss->compare_mode);179180compare_func = translate_comparison_func(ss->compare_func);181182COPY_4V(bcolor.value, ps->border_color.f);183184assert(ps->min_lod <= ps->max_lod);185186if (ps->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {187/* just use the base level image */188min_lod = max_lod = 0.0f;189}190else {191min_lod = ps->min_lod;192max_lod = ps->max_lod;193}194195/* If shadow comparisons are enabled, create two sampler states: one196* with the given shadow compare mode, another with shadow comparison off.197* We need the later because in some cases, we have to do the shadow198* compare in the shader. So, we don't want to do it twice.199*/200STATIC_ASSERT(PIPE_TEX_COMPARE_NONE == 0);201STATIC_ASSERT(PIPE_TEX_COMPARE_R_TO_TEXTURE == 1);202ss->id[1] = SVGA3D_INVALID_ID;203204unsigned i;205for (i = 0; i <= ss->compare_mode; i++) {206ss->id[i] = util_bitmask_add(svga->sampler_object_id_bm);207208SVGA_RETRY(svga, SVGA3D_vgpu10_DefineSamplerState209(svga->swc,210ss->id[i],211filter,212ss->addressu,213ss->addressv,214ss->addressw,215ss->lod_bias, /* float */216max_aniso,217compare_func,218bcolor,219min_lod, /* float */220max_lod)); /* float */221222/* turn off the shadow compare option for second iteration */223filter &= ~SVGA3D_FILTER_COMPARE;224}225}226227228static void *229svga_create_sampler_state(struct pipe_context *pipe,230const struct pipe_sampler_state *sampler)231{232struct svga_context *svga = svga_context(pipe);233struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );234235if (!cso)236return NULL;237238cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);239cso->magfilter = translate_img_filter( sampler->mag_img_filter );240cso->minfilter = translate_img_filter( sampler->min_img_filter );241cso->aniso_level = MAX2( sampler->max_anisotropy, 1 );242if (sampler->max_anisotropy)243cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;244cso->lod_bias = sampler->lod_bias;245cso->addressu = translate_wrap_mode(sampler->wrap_s);246cso->addressv = translate_wrap_mode(sampler->wrap_t);247cso->addressw = translate_wrap_mode(sampler->wrap_r);248cso->normalized_coords = sampler->normalized_coords;249cso->compare_mode = sampler->compare_mode;250cso->compare_func = sampler->compare_func;251252{253uint32 r = float_to_ubyte(sampler->border_color.f[0]);254uint32 g = float_to_ubyte(sampler->border_color.f[1]);255uint32 b = float_to_ubyte(sampler->border_color.f[2]);256uint32 a = float_to_ubyte(sampler->border_color.f[3]);257258cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b;259}260261/* No SVGA3D support for:262* - min/max LOD clamping263*/264cso->min_lod = 0;265cso->view_min_lod = MAX2((int) (sampler->min_lod + 0.5), 0);266cso->view_max_lod = MAX2((int) (sampler->max_lod + 0.5), 0);267268/* Use min_mipmap */269if (svga->debug.use_min_mipmap) {270if (cso->view_min_lod == cso->view_max_lod) {271cso->min_lod = cso->view_min_lod;272cso->view_min_lod = 0;273cso->view_max_lod = 1000; /* Just a high number */274cso->mipfilter = SVGA3D_TEX_FILTER_NONE;275}276}277278if (svga_have_vgpu10(svga)) {279define_sampler_state_object(svga, cso, sampler);280}281282SVGA_DBG(DEBUG_SAMPLERS,283"New sampler: min %u, view(min %u, max %u) lod, mipfilter %s\n",284cso->min_lod, cso->view_min_lod, cso->view_max_lod,285cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");286287svga->hud.num_sampler_objects++;288SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,289SVGA_STATS_COUNT_SAMPLER);290291return cso;292}293294295static void296svga_bind_sampler_states(struct pipe_context *pipe,297enum pipe_shader_type shader,298unsigned start,299unsigned num,300void **samplers)301{302struct svga_context *svga = svga_context(pipe);303unsigned i;304boolean any_change = FALSE;305306assert(shader < PIPE_SHADER_TYPES);307assert(start + num <= PIPE_MAX_SAMPLERS);308309/* Pre-VGPU10 only supports FS textures */310if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)311return;312313for (i = 0; i < num; i++) {314if (svga->curr.sampler[shader][start + i] != samplers[i])315any_change = TRUE;316svga->curr.sampler[shader][start + i] = samplers[i];317}318319if (!any_change) {320return;321}322323/* find highest non-null sampler[] entry */324{325unsigned j = MAX2(svga->curr.num_samplers[shader], start + num);326while (j > 0 && svga->curr.sampler[shader][j - 1] == NULL)327j--;328svga->curr.num_samplers[shader] = j;329}330331svga->dirty |= SVGA_NEW_SAMPLER;332}333334335static void336svga_delete_sampler_state(struct pipe_context *pipe, void *sampler)337{338struct svga_sampler_state *ss = (struct svga_sampler_state *) sampler;339struct svga_context *svga = svga_context(pipe);340341if (svga_have_vgpu10(svga)) {342unsigned i;343for (i = 0; i < 2; i++) {344if (ss->id[i] != SVGA3D_INVALID_ID) {345svga_hwtnl_flush_retry(svga);346347SVGA_RETRY(svga, SVGA3D_vgpu10_DestroySamplerState(svga->swc,348ss->id[i]));349util_bitmask_clear(svga->sampler_object_id_bm, ss->id[i]);350}351}352}353354FREE(sampler);355svga->hud.num_sampler_objects--;356}357358359static struct pipe_sampler_view *360svga_create_sampler_view(struct pipe_context *pipe,361struct pipe_resource *texture,362const struct pipe_sampler_view *templ)363{364struct svga_context *svga = svga_context(pipe);365struct svga_pipe_sampler_view *sv = CALLOC_STRUCT(svga_pipe_sampler_view);366367if (!sv) {368return NULL;369}370371sv->base = *templ;372sv->base.reference.count = 1;373sv->base.texture = NULL;374pipe_resource_reference(&sv->base.texture, texture);375376sv->base.context = pipe;377sv->id = SVGA3D_INVALID_ID;378379svga->hud.num_samplerview_objects++;380SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,381SVGA_STATS_COUNT_SAMPLERVIEW);382383return &sv->base;384}385386387static void388svga_sampler_view_destroy(struct pipe_context *pipe,389struct pipe_sampler_view *view)390{391struct svga_context *svga = svga_context(pipe);392struct svga_pipe_sampler_view *sv = svga_pipe_sampler_view(view);393394if (svga_have_vgpu10(svga) && sv->id != SVGA3D_INVALID_ID) {395assert(view->context == pipe);396397svga_hwtnl_flush_retry(svga);398399SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc,400sv->id));401util_bitmask_clear(svga->sampler_view_id_bm, sv->id);402}403404pipe_resource_reference(&sv->base.texture, NULL);405406FREE(sv);407svga->hud.num_samplerview_objects--;408}409410411static void412svga_set_sampler_views(struct pipe_context *pipe,413enum pipe_shader_type shader,414unsigned start,415unsigned num,416unsigned unbind_num_trailing_slots,417struct pipe_sampler_view **views)418{419struct svga_context *svga = svga_context(pipe);420unsigned flag_1d = 0;421unsigned flag_srgb = 0;422uint i;423boolean any_change = FALSE;424425assert(shader < PIPE_SHADER_TYPES);426assert(start + num <= ARRAY_SIZE(svga->curr.sampler_views[shader]));427428/* Pre-VGPU10 only supports FS textures */429if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)430return;431432SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SETSAMPLERVIEWS);433434/* This bit of code works around a quirk in the CSO module.435* If start=num=0 it means all sampler views should be released.436* Note that the CSO module treats sampler views for fragment shaders437* differently than other shader types.438*/439if (start == 0 && num == 0 && svga->curr.num_sampler_views[shader] > 0) {440for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {441pipe_sampler_view_reference(&svga->curr.sampler_views[shader][i],442NULL);443}444any_change = TRUE;445}446447for (i = 0; i < num; i++) {448enum pipe_texture_target target;449450if (svga->curr.sampler_views[shader][start + i] != views[i]) {451pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],452views[i]);453any_change = TRUE;454}455456if (!views[i])457continue;458459if (util_format_is_srgb(views[i]->format))460flag_srgb |= 1 << (start + i);461462target = views[i]->target;463if (target == PIPE_TEXTURE_1D) {464flag_1d |= 1 << (start + i);465} else if (target == PIPE_TEXTURE_RECT) {466/* If the size of the bound texture changes, we need to emit new467* const buffer values.468*/469svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;470} else if (target == PIPE_BUFFER) {471/* If the size of the bound buffer changes, we need to emit new472* const buffer values.473*/474svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;475}476}477478for (; i < num + unbind_num_trailing_slots; i++) {479if (svga->curr.sampler_views[shader][start + i]) {480pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],481NULL);482any_change = TRUE;483}484}485486if (!any_change) {487goto done;488}489490/* find highest non-null sampler_views[] entry */491{492unsigned j = MAX2(svga->curr.num_sampler_views[shader], start + num);493while (j > 0 && svga->curr.sampler_views[shader][j - 1] == NULL)494j--;495svga->curr.num_sampler_views[shader] = j;496}497498svga->dirty |= SVGA_NEW_TEXTURE_BINDING;499500if (flag_srgb != svga->curr.tex_flags.flag_srgb ||501flag_1d != svga->curr.tex_flags.flag_1d) {502svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;503svga->curr.tex_flags.flag_1d = flag_1d;504svga->curr.tex_flags.flag_srgb = flag_srgb;505}506507/* Check if any of the sampler view resources collide with the framebuffer508* color buffers or depth stencil resource. If so, set the NEW_FRAME_BUFFER509* dirty bit so that emit_framebuffer can be invoked to create backed view510* for the conflicted surface view.511*/512if (svga_check_sampler_framebuffer_resource_collision(svga, shader)) {513svga->dirty |= SVGA_NEW_FRAME_BUFFER;514}515516done:517SVGA_STATS_TIME_POP(svga_sws(svga));518}519520/**521* Clean up sampler, sampler view state at context destruction time522*/523void524svga_cleanup_sampler_state(struct svga_context *svga)525{526enum pipe_shader_type shader;527528for (shader = 0; shader <= PIPE_SHADER_TESS_EVAL; shader++) {529unsigned i;530531for (i = 0; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {532pipe_sampler_view_reference(&svga->state.hw_draw.sampler_views[shader][i],533NULL);534}535}536537/* free polygon stipple state */538if (svga->polygon_stipple.sampler) {539svga->pipe.delete_sampler_state(&svga->pipe, svga->polygon_stipple.sampler);540}541542if (svga->polygon_stipple.sampler_view) {543svga->pipe.sampler_view_destroy(&svga->pipe,544&svga->polygon_stipple.sampler_view->base);545}546pipe_resource_reference(&svga->polygon_stipple.texture, NULL);547}548549void550svga_init_sampler_functions( struct svga_context *svga )551{552svga->pipe.create_sampler_state = svga_create_sampler_state;553svga->pipe.bind_sampler_states = svga_bind_sampler_states;554svga->pipe.delete_sampler_state = svga_delete_sampler_state;555svga->pipe.set_sampler_views = svga_set_sampler_views;556svga->pipe.create_sampler_view = svga_create_sampler_view;557svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;558}559560561