1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ 2 3 /* 4 * Copyright (C) 2012 Rob Clark <robclark (at) freedesktop.org> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 * 25 * Authors: 26 * Rob Clark <robclark (at) freedesktop.org> 27 */ 28 29 #include "pipe/p_state.h" 30 #include "util/u_string.h" 31 #include "util/u_memory.h" 32 #include "util/u_inlines.h" 33 34 #include "freedreno_texture.h" 35 #include "freedreno_context.h" 36 #include "freedreno_util.h" 37 38 static void 39 fd_sampler_state_delete(struct pipe_context *pctx, void *hwcso) 40 { 41 FREE(hwcso); 42 } 43 44 static void 45 fd_sampler_view_destroy(struct pipe_context *pctx, 46 struct pipe_sampler_view *view) 47 { 48 pipe_resource_reference(&view->texture, NULL); 49 FREE(view); 50 } 51 52 static void bind_sampler_states(struct fd_texture_stateobj *tex, 53 unsigned start, unsigned nr, void **hwcso) 54 { 55 unsigned i; 56 57 for (i = 0; i < nr; i++) { 58 unsigned p = i + start; 59 tex->samplers[p] = hwcso[i]; 60 if (tex->samplers[p]) 61 tex->valid_samplers |= (1 << p); 62 else 63 tex->valid_samplers &= ~(1 << p); 64 } 65 66 tex->num_samplers = util_last_bit(tex->valid_samplers); 67 } 68 69 static void set_sampler_views(struct fd_texture_stateobj *tex, 70 unsigned start, unsigned nr, struct pipe_sampler_view **views) 71 { 72 unsigned i; 73 74 for (i = 0; i < nr; i++) { 75 struct pipe_sampler_view *view = views ? views[i] : NULL; 76 unsigned p = i + start; 77 pipe_sampler_view_reference(&tex->textures[p], view); 78 if (tex->textures[p]) 79 tex->valid_textures |= (1 << p); 80 else 81 tex->valid_textures &= ~(1 << p); 82 } 83 84 tex->num_textures = util_last_bit(tex->valid_textures); 85 } 86 87 void 88 fd_sampler_states_bind(struct pipe_context *pctx, 89 enum pipe_shader_type shader, unsigned start, 90 unsigned nr, void **hwcso) 91 { 92 struct fd_context *ctx = fd_context(pctx); 93 94 if (shader == PIPE_SHADER_FRAGMENT) { 95 bind_sampler_states(&ctx->fragtex, start, nr, hwcso); 96 ctx->dirty |= FD_DIRTY_FRAGTEX; 97 } 98 else if (shader == PIPE_SHADER_VERTEX) { 99 bind_sampler_states(&ctx->verttex, start, nr, hwcso); 100 ctx->dirty |= FD_DIRTY_VERTTEX; 101 } 102 } 103 104 void 105 fd_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader, 106 unsigned start, unsigned nr, 107 struct pipe_sampler_view **views) 108 { 109 struct fd_context *ctx = fd_context(pctx); 110 111 switch (shader) { 112 case PIPE_SHADER_FRAGMENT: 113 /* on a2xx, since there is a flat address space for textures/samplers, 114 * a change in # of fragment textures/samplers will trigger patching 115 * and re-emitting the vertex shader: 116 * 117 * (note: later gen's ignore FD_DIRTY_TEXSTATE so fine to set it) 118 */ 119 if (nr != ctx->fragtex.num_textures) 120 ctx->dirty |= FD_DIRTY_TEXSTATE; 121 122 set_sampler_views(&ctx->fragtex, start, nr, views); 123 ctx->dirty |= FD_DIRTY_FRAGTEX; 124 break; 125 case PIPE_SHADER_VERTEX: 126 set_sampler_views(&ctx->verttex, start, nr, views); 127 ctx->dirty |= FD_DIRTY_VERTTEX; 128 break; 129 default: 130 break; 131 } 132 } 133 134 void 135 fd_texture_init(struct pipe_context *pctx) 136 { 137 pctx->delete_sampler_state = fd_sampler_state_delete; 138 139 pctx->sampler_view_destroy = fd_sampler_view_destroy; 140 } 141 142 /* helper for setting up border-color buffer for a3xx/a4xx: */ 143 void 144 fd_setup_border_colors(struct fd_texture_stateobj *tex, void *ptr, 145 unsigned offset) 146 { 147 unsigned i, j; 148 149 for (i = 0; i < tex->num_samplers; i++) { 150 struct pipe_sampler_state *sampler = tex->samplers[i]; 151 uint16_t *bcolor = (uint16_t *)((uint8_t *)ptr + 152 (BORDERCOLOR_SIZE * offset) + 153 (BORDERCOLOR_SIZE * i)); 154 uint32_t *bcolor32 = (uint32_t *)&bcolor[16]; 155 156 if (!sampler) 157 continue; 158 159 /* 160 * XXX HACK ALERT XXX 161 * 162 * The border colors need to be swizzled in a particular 163 * format-dependent order. Even though samplers don't know about 164 * formats, we can assume that with a GL state tracker, there's a 165 * 1:1 correspondence between sampler and texture. Take advantage 166 * of that knowledge. 167 */ 168 if (i < tex->num_textures && tex->textures[i]) { 169 const struct util_format_description *desc = 170 util_format_description(tex->textures[i]->format); 171 for (j = 0; j < 4; j++) { 172 if (desc->swizzle[j] >= 4) 173 continue; 174 175 const struct util_format_channel_description *chan = 176 &desc->channel[desc->swizzle[j]]; 177 if (chan->pure_integer) { 178 bcolor32[desc->swizzle[j] + 4] = sampler->border_color.i[j]; 179 bcolor[desc->swizzle[j] + 8] = sampler->border_color.i[j]; 180 } else { 181 bcolor32[desc->swizzle[j]] = fui(sampler->border_color.f[j]); 182 bcolor[desc->swizzle[j]] = 183 util_float_to_half(sampler->border_color.f[j]); 184 } 185 } 186 } 187 } 188 } 189