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 start
, unsigned nr
, void **hwcso
)
57 for (i
= 0; i
< nr
; i
++) {
58 unsigned p
= i
+ start
;
59 tex
->samplers
[p
] = hwcso
[i
];
61 tex
->valid_samplers
|= (1 << p
);
63 tex
->valid_samplers
&= ~(1 << p
);
66 tex
->num_samplers
= util_last_bit(tex
->valid_samplers
);
69 static void set_sampler_views(struct fd_texture_stateobj
*tex
,
70 unsigned start
, unsigned nr
, struct pipe_sampler_view
**views
)
73 unsigned samplers
= 0;
75 for (i
= 0; i
< nr
; i
++) {
76 struct pipe_sampler_view
*view
= views
? views
[i
] : NULL
;
77 unsigned p
= i
+ start
;
78 pipe_sampler_view_reference(&tex
->textures
[p
], view
);
80 tex
->valid_textures
|= (1 << p
);
82 tex
->valid_textures
&= ~(1 << p
);
85 tex
->num_textures
= util_last_bit(tex
->valid_textures
);
87 for (i
= 0; i
< tex
->num_textures
; i
++) {
88 uint nr_samples
= tex
->textures
[i
]->texture
->nr_samples
;
89 samplers
|= (nr_samples
>> 1) << (i
* 2);
92 tex
->samples
= samplers
;
96 fd_sampler_states_bind(struct pipe_context
*pctx
,
97 enum pipe_shader_type shader
, unsigned start
,
98 unsigned nr
, void **hwcso
)
100 struct fd_context
*ctx
= fd_context(pctx
);
102 bind_sampler_states(&ctx
->tex
[shader
], start
, nr
, hwcso
);
103 ctx
->dirty_shader
[shader
] |= FD_DIRTY_SHADER_TEX
;
104 ctx
->dirty
|= FD_DIRTY_TEX
;
108 fd_set_sampler_views(struct pipe_context
*pctx
, enum pipe_shader_type shader
,
109 unsigned start
, unsigned nr
,
110 struct pipe_sampler_view
**views
)
112 struct fd_context
*ctx
= fd_context(pctx
);
114 set_sampler_views(&ctx
->tex
[shader
], start
, nr
, views
);
115 ctx
->dirty_shader
[shader
] |= FD_DIRTY_SHADER_TEX
;
116 ctx
->dirty
|= FD_DIRTY_TEX
;
120 fd_texture_init(struct pipe_context
*pctx
)
122 pctx
->delete_sampler_state
= fd_sampler_state_delete
;
124 pctx
->sampler_view_destroy
= fd_sampler_view_destroy
;
127 /* helper for setting up border-color buffer for a3xx/a4xx: */
129 fd_setup_border_colors(struct fd_texture_stateobj
*tex
, void *ptr
,
134 for (i
= 0; i
< tex
->num_samplers
; i
++) {
135 struct pipe_sampler_state
*sampler
= tex
->samplers
[i
];
136 uint16_t *bcolor
= (uint16_t *)((uint8_t *)ptr
+
137 (BORDERCOLOR_SIZE
* offset
) +
138 (BORDERCOLOR_SIZE
* i
));
139 uint32_t *bcolor32
= (uint32_t *)&bcolor
[16];
147 * The border colors need to be swizzled in a particular
148 * format-dependent order. Even though samplers don't know about
149 * formats, we can assume that with a GL state tracker, there's a
150 * 1:1 correspondence between sampler and texture. Take advantage
153 if (i
< tex
->num_textures
&& tex
->textures
[i
]) {
154 const struct util_format_description
*desc
=
155 util_format_description(tex
->textures
[i
]->format
);
156 for (j
= 0; j
< 4; j
++) {
157 if (desc
->swizzle
[j
] >= 4)
160 const struct util_format_channel_description
*chan
=
161 &desc
->channel
[desc
->swizzle
[j
]];
162 if (chan
->pure_integer
) {
163 bcolor32
[desc
->swizzle
[j
] + 4] = sampler
->border_color
.i
[j
];
164 bcolor
[desc
->swizzle
[j
] + 8] = sampler
->border_color
.i
[j
];
166 bcolor32
[desc
->swizzle
[j
]] = fui(sampler
->border_color
.f
[j
]);
167 bcolor
[desc
->swizzle
[j
]] =
168 util_float_to_half(sampler
->border_color
.f
[j
]);