2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "pipe/p_state.h"
28 #include "util/u_string.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
32 #include "freedreno_texture.h"
33 #include "freedreno_context.h"
34 #include "freedreno_util.h"
37 fd_sampler_state_delete(struct pipe_context
*pctx
, void *hwcso
)
43 fd_sampler_view_destroy(struct pipe_context
*pctx
,
44 struct pipe_sampler_view
*view
)
46 pipe_resource_reference(&view
->texture
, NULL
);
50 static void bind_sampler_states(struct fd_texture_stateobj
*tex
,
51 unsigned start
, unsigned nr
, void **hwcso
)
55 for (i
= 0; i
< nr
; i
++) {
56 unsigned p
= i
+ start
;
57 tex
->samplers
[p
] = hwcso
[i
];
59 tex
->valid_samplers
|= (1 << p
);
61 tex
->valid_samplers
&= ~(1 << p
);
64 tex
->num_samplers
= util_last_bit(tex
->valid_samplers
);
67 static void set_sampler_views(struct fd_texture_stateobj
*tex
,
68 unsigned start
, unsigned nr
, struct pipe_sampler_view
**views
)
71 unsigned samplers
= 0;
73 for (i
= 0; i
< nr
; i
++) {
74 struct pipe_sampler_view
*view
= views
? views
[i
] : NULL
;
75 unsigned p
= i
+ start
;
76 pipe_sampler_view_reference(&tex
->textures
[p
], view
);
78 tex
->valid_textures
|= (1 << p
);
80 tex
->valid_textures
&= ~(1 << p
);
83 tex
->num_textures
= util_last_bit(tex
->valid_textures
);
85 for (i
= 0; i
< tex
->num_textures
; i
++) {
86 uint nr_samples
= tex
->textures
[i
]->texture
->nr_samples
;
87 samplers
|= (nr_samples
>> 1) << (i
* 2);
90 tex
->samples
= samplers
;
94 fd_sampler_states_bind(struct pipe_context
*pctx
,
95 enum pipe_shader_type shader
, unsigned start
,
96 unsigned nr
, void **hwcso
)
98 struct fd_context
*ctx
= fd_context(pctx
);
100 bind_sampler_states(&ctx
->tex
[shader
], start
, nr
, hwcso
);
101 ctx
->dirty_shader
[shader
] |= FD_DIRTY_SHADER_TEX
;
102 ctx
->dirty
|= FD_DIRTY_TEX
;
106 fd_set_sampler_views(struct pipe_context
*pctx
, enum pipe_shader_type shader
,
107 unsigned start
, unsigned nr
,
108 struct pipe_sampler_view
**views
)
110 struct fd_context
*ctx
= fd_context(pctx
);
112 set_sampler_views(&ctx
->tex
[shader
], start
, nr
, views
);
113 ctx
->dirty_shader
[shader
] |= FD_DIRTY_SHADER_TEX
;
114 ctx
->dirty
|= FD_DIRTY_TEX
;
118 fd_texture_init(struct pipe_context
*pctx
)
120 pctx
->delete_sampler_state
= fd_sampler_state_delete
;
122 pctx
->sampler_view_destroy
= fd_sampler_view_destroy
;
125 /* helper for setting up border-color buffer for a3xx/a4xx: */
127 fd_setup_border_colors(struct fd_texture_stateobj
*tex
, void *ptr
,
132 for (i
= 0; i
< tex
->num_samplers
; i
++) {
133 struct pipe_sampler_state
*sampler
= tex
->samplers
[i
];
134 uint16_t *bcolor
= (uint16_t *)((uint8_t *)ptr
+
135 (BORDERCOLOR_SIZE
* offset
) +
136 (BORDERCOLOR_SIZE
* i
));
137 uint32_t *bcolor32
= (uint32_t *)&bcolor
[16];
145 * The border colors need to be swizzled in a particular
146 * format-dependent order. Even though samplers don't know about
147 * formats, we can assume that with a GL state tracker, there's a
148 * 1:1 correspondence between sampler and texture. Take advantage
151 if (i
< tex
->num_textures
&& tex
->textures
[i
]) {
152 const struct util_format_description
*desc
=
153 util_format_description(tex
->textures
[i
]->format
);
154 for (j
= 0; j
< 4; j
++) {
155 if (desc
->swizzle
[j
] >= 4)
158 const struct util_format_channel_description
*chan
=
159 &desc
->channel
[desc
->swizzle
[j
]];
160 if (chan
->pure_integer
) {
161 bcolor32
[desc
->swizzle
[j
] + 4] = sampler
->border_color
.i
[j
];
162 bcolor
[desc
->swizzle
[j
] + 8] = sampler
->border_color
.i
[j
];
164 bcolor32
[desc
->swizzle
[j
]] = fui(sampler
->border_color
.f
[j
]);
165 bcolor
[desc
->swizzle
[j
]] =
166 util_float_to_half(sampler
->border_color
.f
[j
]);