freedreno/a3xx/compiler: refactor trans_samp()
[mesa.git] / src / gallium / drivers / freedreno / freedreno_texture.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@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@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 *prog,
53 unsigned nr, void **hwcso)
54 {
55 unsigned i;
56 unsigned new_nr = 0;
57
58 for (i = 0; i < nr; i++) {
59 if (hwcso[i])
60 new_nr++;
61 prog->samplers[i] = hwcso[i];
62 prog->dirty_samplers |= (1 << i);
63 }
64
65 for (; i < prog->num_samplers; i++) {
66 prog->samplers[i] = NULL;
67 prog->dirty_samplers |= (1 << i);
68 }
69
70 prog->num_samplers = new_nr;
71 }
72
73 static void set_sampler_views(struct fd_texture_stateobj *prog,
74 unsigned nr, struct pipe_sampler_view **views)
75 {
76 unsigned i;
77 unsigned new_nr = 0;
78
79 for (i = 0; i < nr; i++) {
80 if (views[i])
81 new_nr++;
82 pipe_sampler_view_reference(&prog->textures[i], views[i]);
83 prog->dirty_samplers |= (1 << i);
84 }
85
86 for (; i < prog->num_textures; i++) {
87 pipe_sampler_view_reference(&prog->textures[i], NULL);
88 prog->dirty_samplers |= (1 << i);
89 }
90
91 prog->num_textures = new_nr;
92 }
93
94 static void
95 fd_sampler_states_bind(struct pipe_context *pctx,
96 unsigned shader, unsigned start,
97 unsigned nr, void **hwcso)
98 {
99 struct fd_context *ctx = fd_context(pctx);
100
101 assert(start == 0);
102
103 if (shader == PIPE_SHADER_FRAGMENT) {
104 /* on a2xx, since there is a flat address space for textures/samplers,
105 * a change in # of fragment textures/samplers will trigger patching and
106 * re-emitting the vertex shader:
107 */
108 if (nr != ctx->fragtex.num_samplers)
109 ctx->dirty |= FD_DIRTY_TEXSTATE;
110
111 bind_sampler_states(&ctx->fragtex, nr, hwcso);
112 ctx->dirty |= FD_DIRTY_FRAGTEX;
113 }
114 else if (shader == PIPE_SHADER_VERTEX) {
115 bind_sampler_states(&ctx->verttex, nr, hwcso);
116 ctx->dirty |= FD_DIRTY_VERTTEX;
117 }
118 }
119
120
121 static void
122 fd_fragtex_set_sampler_views(struct pipe_context *pctx, unsigned nr,
123 struct pipe_sampler_view **views)
124 {
125 struct fd_context *ctx = fd_context(pctx);
126
127 /* on a2xx, since there is a flat address space for textures/samplers,
128 * a change in # of fragment textures/samplers will trigger patching and
129 * re-emitting the vertex shader:
130 */
131 if (nr != ctx->fragtex.num_textures)
132 ctx->dirty |= FD_DIRTY_TEXSTATE;
133
134 set_sampler_views(&ctx->fragtex, nr, views);
135 ctx->dirty |= FD_DIRTY_FRAGTEX;
136 }
137
138 static void
139 fd_verttex_set_sampler_views(struct pipe_context *pctx, unsigned nr,
140 struct pipe_sampler_view **views)
141 {
142 struct fd_context *ctx = fd_context(pctx);
143 set_sampler_views(&ctx->verttex, nr, views);
144 ctx->dirty |= FD_DIRTY_VERTTEX;
145 }
146
147 static void
148 fd_set_sampler_views(struct pipe_context *pctx, unsigned shader,
149 unsigned start, unsigned nr,
150 struct pipe_sampler_view **views)
151 {
152 assert(start == 0);
153 switch (shader) {
154 case PIPE_SHADER_FRAGMENT:
155 fd_fragtex_set_sampler_views(pctx, nr, views);
156 break;
157 case PIPE_SHADER_VERTEX:
158 fd_verttex_set_sampler_views(pctx, nr, views);
159 break;
160 default:
161 ;
162 }
163 }
164
165 void
166 fd_texture_init(struct pipe_context *pctx)
167 {
168 pctx->delete_sampler_state = fd_sampler_state_delete;
169
170 pctx->sampler_view_destroy = fd_sampler_view_destroy;
171
172 pctx->bind_sampler_states = fd_sampler_states_bind;
173 pctx->set_sampler_views = fd_set_sampler_views;
174 }