1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_context.h"
36 #include "freedreno_util.h"
39 fd_sampler_state_delete(struct pipe_context
*pctx
, void *hwcso
)
45 fd_sampler_view_destroy(struct pipe_context
*pctx
,
46 struct pipe_sampler_view
*view
)
48 pipe_resource_reference(&view
->texture
, NULL
);
52 static void bind_sampler_states(struct fd_texture_stateobj
*tex
,
53 unsigned nr
, void **hwcso
)
58 for (i
= 0; i
< nr
; i
++) {
61 tex
->samplers
[i
] = hwcso
[i
];
62 tex
->dirty_samplers
|= (1 << i
);
65 for (; i
< tex
->num_samplers
; i
++) {
66 tex
->samplers
[i
] = NULL
;
67 tex
->dirty_samplers
|= (1 << i
);
70 tex
->num_samplers
= new_nr
;
73 static void set_sampler_views(struct fd_texture_stateobj
*tex
,
74 unsigned nr
, struct pipe_sampler_view
**views
)
79 for (i
= 0; i
< nr
; i
++) {
82 pipe_sampler_view_reference(&tex
->textures
[i
], views
[i
]);
83 tex
->dirty_samplers
|= (1 << i
);
86 for (; i
< tex
->num_textures
; i
++) {
87 pipe_sampler_view_reference(&tex
->textures
[i
], NULL
);
88 tex
->dirty_samplers
|= (1 << i
);
91 tex
->num_textures
= new_nr
;
95 fd_sampler_states_bind(struct pipe_context
*pctx
,
96 unsigned shader
, unsigned start
,
97 unsigned nr
, void **hwcso
)
99 struct fd_context
*ctx
= fd_context(pctx
);
103 if (shader
== PIPE_SHADER_FRAGMENT
) {
104 bind_sampler_states(&ctx
->fragtex
, nr
, hwcso
);
105 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
107 else if (shader
== PIPE_SHADER_VERTEX
) {
108 bind_sampler_states(&ctx
->verttex
, nr
, hwcso
);
109 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
115 fd_fragtex_set_sampler_views(struct pipe_context
*pctx
, unsigned nr
,
116 struct pipe_sampler_view
**views
)
118 struct fd_context
*ctx
= fd_context(pctx
);
120 /* on a2xx, since there is a flat address space for textures/samplers,
121 * a change in # of fragment textures/samplers will trigger patching and
122 * re-emitting the vertex shader:
124 if (nr
!= ctx
->fragtex
.num_textures
)
125 ctx
->dirty
|= FD_DIRTY_TEXSTATE
;
127 set_sampler_views(&ctx
->fragtex
, nr
, views
);
128 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
132 fd_verttex_set_sampler_views(struct pipe_context
*pctx
, unsigned nr
,
133 struct pipe_sampler_view
**views
)
135 struct fd_context
*ctx
= fd_context(pctx
);
136 set_sampler_views(&ctx
->verttex
, nr
, views
);
137 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
141 fd_set_sampler_views(struct pipe_context
*pctx
, unsigned shader
,
142 unsigned start
, unsigned nr
,
143 struct pipe_sampler_view
**views
)
147 case PIPE_SHADER_FRAGMENT
:
148 fd_fragtex_set_sampler_views(pctx
, nr
, views
);
150 case PIPE_SHADER_VERTEX
:
151 fd_verttex_set_sampler_views(pctx
, nr
, views
);
159 fd_texture_init(struct pipe_context
*pctx
)
161 pctx
->delete_sampler_state
= fd_sampler_state_delete
;
163 pctx
->sampler_view_destroy
= fd_sampler_view_destroy
;
166 /* helper for setting up border-color buffer for a3xx/a4xx: */
168 fd_setup_border_colors(struct fd_texture_stateobj
*tex
, void *ptr
,
173 for (i
= 0; i
< tex
->num_samplers
; i
++) {
174 struct pipe_sampler_state
*sampler
= tex
->samplers
[i
];
175 uint16_t *bcolor
= (uint16_t *)((uint8_t *)ptr
+
176 (BORDERCOLOR_SIZE
* offset
) +
177 (BORDERCOLOR_SIZE
* i
));
178 uint32_t *bcolor32
= (uint32_t *)&bcolor
[16];
186 * The border colors need to be swizzled in a particular
187 * format-dependent order. Even though samplers don't know about
188 * formats, we can assume that with a GL state tracker, there's a
189 * 1:1 correspondence between sampler and texture. Take advantage
192 if (i
< tex
->num_textures
&& tex
->textures
[i
]) {
193 const struct util_format_description
*desc
=
194 util_format_description(tex
->textures
[i
]->format
);
195 for (j
= 0; j
< 4; j
++) {
196 if (desc
->swizzle
[j
] >= 4)
199 const struct util_format_channel_description
*chan
=
200 &desc
->channel
[desc
->swizzle
[j
]];
201 int size
= chan
->size
;
203 /* The Z16 texture format we use seems to look in the
204 * 32-bit border color slots
206 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
)
209 /* Formats like R11G11B10 or RGB9_E5 don't specify
210 * per-channel sizes properly.
212 if (desc
->layout
== UTIL_FORMAT_LAYOUT_OTHER
)
215 /* We fake RGTC as if it were RGBA8 */
216 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
)
219 if (chan
->pure_integer
&& size
> 16)
220 bcolor32
[desc
->swizzle
[j
] + 4] =
221 sampler
->border_color
.i
[j
];
223 bcolor32
[desc
->swizzle
[j
]] =
224 fui(sampler
->border_color
.f
[j
]);
225 else if (chan
->pure_integer
)
226 bcolor
[desc
->swizzle
[j
] + 8] =
227 sampler
->border_color
.i
[j
];
229 bcolor
[desc
->swizzle
[j
]] =
230 util_float_to_half(sampler
->border_color
.f
[j
]);