Path: blob/21.2-virgl/src/gallium/drivers/freedreno/freedreno_texture.c
4570 views
/*1* Copyright (C) 2012 Rob Clark <[email protected]>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 (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "pipe/p_state.h"27#include "util/u_inlines.h"28#include "util/u_memory.h"29#include "util/u_string.h"3031#include "freedreno_context.h"32#include "freedreno_resource.h"33#include "freedreno_texture.h"34#include "freedreno_util.h"3536static void37fd_sampler_state_delete(struct pipe_context *pctx, void *hwcso)38{39FREE(hwcso);40}4142static void43fd_sampler_view_destroy(struct pipe_context *pctx,44struct pipe_sampler_view *view)45{46pipe_resource_reference(&view->texture, NULL);47FREE(view);48}4950static void51bind_sampler_states(struct fd_texture_stateobj *tex, unsigned start,52unsigned nr, void **hwcso)53{54unsigned i;5556for (i = 0; i < nr; i++) {57unsigned p = i + start;58tex->samplers[p] = hwcso[i];59if (tex->samplers[p])60tex->valid_samplers |= (1 << p);61else62tex->valid_samplers &= ~(1 << p);63}6465tex->num_samplers = util_last_bit(tex->valid_samplers);66}6768static void69set_sampler_views(struct fd_texture_stateobj *tex, unsigned start, unsigned nr,70unsigned unbind_num_trailing_slots,71struct pipe_sampler_view **views)72{73unsigned i;7475for (i = 0; i < nr; i++) {76struct pipe_sampler_view *view = views ? views[i] : NULL;77unsigned p = i + start;78pipe_sampler_view_reference(&tex->textures[p], view);79if (tex->textures[p]) {80fd_resource_set_usage(tex->textures[p]->texture, FD_DIRTY_TEX);81tex->valid_textures |= (1 << p);82} else {83tex->valid_textures &= ~(1 << p);84}85}86for (; i < nr + unbind_num_trailing_slots; i++) {87unsigned p = i + start;88pipe_sampler_view_reference(&tex->textures[p], NULL);89tex->valid_textures &= ~(1 << p);90}9192tex->num_textures = util_last_bit(tex->valid_textures);93}9495void96fd_sampler_states_bind(struct pipe_context *pctx, enum pipe_shader_type shader,97unsigned start, unsigned nr, void **hwcso) in_dt98{99struct fd_context *ctx = fd_context(pctx);100101bind_sampler_states(&ctx->tex[shader], start, nr, hwcso);102fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);103}104105void106fd_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,107unsigned start, unsigned nr,108unsigned unbind_num_trailing_slots,109struct pipe_sampler_view **views) in_dt110{111struct fd_context *ctx = fd_context(pctx);112113set_sampler_views(&ctx->tex[shader], start, nr, unbind_num_trailing_slots,114views);115fd_context_dirty_shader(ctx, shader, FD_DIRTY_SHADER_TEX);116}117118void119fd_texture_init(struct pipe_context *pctx)120{121if (!pctx->delete_sampler_state)122pctx->delete_sampler_state = fd_sampler_state_delete;123if (!pctx->sampler_view_destroy)124pctx->sampler_view_destroy = fd_sampler_view_destroy;125}126127/* helper for setting up border-color buffer for a3xx/a4xx: */128void129fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr,130unsigned offset)131{132unsigned i, j;133134for (i = 0; i < tex->num_samplers; i++) {135struct pipe_sampler_state *sampler = tex->samplers[i];136uint16_t *bcolor =137(uint16_t *)((uint8_t *)ptr + (BORDERCOLOR_SIZE * offset) +138(BORDERCOLOR_SIZE * i));139uint32_t *bcolor32 = (uint32_t *)&bcolor[16];140141if (!sampler)142continue;143144/*145* XXX HACK ALERT XXX146*147* The border colors need to be swizzled in a particular148* format-dependent order. Even though samplers don't know about149* formats, we can assume that with a GL state tracker, there's a150* 1:1 correspondence between sampler and texture. Take advantage151* of that knowledge.152*/153if (i < tex->num_textures && tex->textures[i]) {154const struct util_format_description *desc =155util_format_description(tex->textures[i]->format);156for (j = 0; j < 4; j++) {157if (desc->swizzle[j] >= 4)158continue;159160const struct util_format_channel_description *chan =161&desc->channel[desc->swizzle[j]];162if (chan->pure_integer) {163bcolor32[desc->swizzle[j] + 4] = sampler->border_color.i[j];164bcolor[desc->swizzle[j] + 8] = sampler->border_color.i[j];165} else {166bcolor32[desc->swizzle[j]] = fui(sampler->border_color.f[j]);167bcolor[desc->swizzle[j]] =168_mesa_float_to_half(sampler->border_color.f[j]);169}170}171}172}173}174175176