1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
35 #include "draw/draw_context.h"
37 #include "sp_context.h"
39 #include "sp_texture.h"
40 #include "sp_tex_sample.h"
41 #include "sp_tex_tile_cache.h"
45 struct pipe_sampler_state base
;
46 struct sp_sampler_varient
*varients
;
47 struct sp_sampler_varient
*current
;
50 static struct sp_sampler
*sp_sampler( struct pipe_sampler_state
*sampler
)
52 return (struct sp_sampler
*)sampler
;
57 softpipe_create_sampler_state(struct pipe_context
*pipe
,
58 const struct pipe_sampler_state
*sampler
)
60 struct sp_sampler
*sp_sampler
= CALLOC_STRUCT(sp_sampler
);
62 sp_sampler
->base
= *sampler
;
63 sp_sampler
->varients
= NULL
;
65 return (void *)sp_sampler
;
70 softpipe_bind_sampler_states(struct pipe_context
*pipe
,
71 unsigned num
, void **sampler
)
73 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
76 assert(num
<= PIPE_MAX_SAMPLERS
);
79 if (num
== softpipe
->num_samplers
&&
80 !memcmp(softpipe
->sampler
, sampler
, num
* sizeof(void *)))
83 draw_flush(softpipe
->draw
);
85 for (i
= 0; i
< num
; ++i
)
86 softpipe
->sampler
[i
] = sampler
[i
];
87 for (i
= num
; i
< PIPE_MAX_SAMPLERS
; ++i
)
88 softpipe
->sampler
[i
] = NULL
;
90 softpipe
->num_samplers
= num
;
92 softpipe
->dirty
|= SP_NEW_SAMPLER
;
97 softpipe_bind_vertex_sampler_states(struct pipe_context
*pipe
,
98 unsigned num_samplers
,
101 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
104 assert(num_samplers
<= PIPE_MAX_VERTEX_SAMPLERS
);
106 /* Check for no-op */
107 if (num_samplers
== softpipe
->num_vertex_samplers
&&
108 !memcmp(softpipe
->vertex_samplers
, samplers
, num_samplers
* sizeof(void *)))
111 draw_flush(softpipe
->draw
);
113 for (i
= 0; i
< num_samplers
; ++i
)
114 softpipe
->vertex_samplers
[i
] = samplers
[i
];
115 for (i
= num_samplers
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
116 softpipe
->vertex_samplers
[i
] = NULL
;
118 softpipe
->num_vertex_samplers
= num_samplers
;
120 draw_set_samplers(softpipe
->draw
,
121 softpipe
->vertex_samplers
,
122 softpipe
->num_vertex_samplers
);
124 softpipe
->dirty
|= SP_NEW_SAMPLER
;
128 softpipe_bind_geometry_sampler_states(struct pipe_context
*pipe
,
129 unsigned num_samplers
,
132 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
135 assert(num_samplers
<= PIPE_MAX_GEOMETRY_SAMPLERS
);
137 /* Check for no-op */
138 if (num_samplers
== softpipe
->num_geometry_samplers
&&
139 !memcmp(softpipe
->geometry_samplers
, samplers
, num_samplers
* sizeof(void *)))
142 draw_flush(softpipe
->draw
);
144 for (i
= 0; i
< num_samplers
; ++i
)
145 softpipe
->geometry_samplers
[i
] = samplers
[i
];
146 for (i
= num_samplers
; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; ++i
)
147 softpipe
->geometry_samplers
[i
] = NULL
;
149 softpipe
->num_geometry_samplers
= num_samplers
;
151 softpipe
->dirty
|= SP_NEW_SAMPLER
;
155 static struct pipe_sampler_view
*
156 softpipe_create_sampler_view(struct pipe_context
*pipe
,
157 struct pipe_resource
*resource
,
158 const struct pipe_sampler_view
*templ
)
160 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
164 view
->reference
.count
= 1;
165 view
->texture
= NULL
;
166 pipe_resource_reference(&view
->texture
, resource
);
167 view
->context
= pipe
;
175 softpipe_sampler_view_destroy(struct pipe_context
*pipe
,
176 struct pipe_sampler_view
*view
)
178 pipe_resource_reference(&view
->texture
, NULL
);
184 softpipe_set_sampler_views(struct pipe_context
*pipe
,
186 struct pipe_sampler_view
**views
)
188 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
191 assert(num
<= PIPE_MAX_SAMPLERS
);
193 /* Check for no-op */
194 if (num
== softpipe
->num_sampler_views
&&
195 !memcmp(softpipe
->sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*)))
198 draw_flush(softpipe
->draw
);
200 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
201 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
203 pipe_sampler_view_reference(&softpipe
->sampler_views
[i
], view
);
204 sp_tex_tile_cache_set_sampler_view(softpipe
->tex_cache
[i
], view
);
207 softpipe
->num_sampler_views
= num
;
209 softpipe
->dirty
|= SP_NEW_TEXTURE
;
214 softpipe_set_vertex_sampler_views(struct pipe_context
*pipe
,
216 struct pipe_sampler_view
**views
)
218 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
221 assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
223 /* Check for no-op */
224 if (num
== softpipe
->num_vertex_sampler_views
&&
225 !memcmp(softpipe
->vertex_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
229 draw_flush(softpipe
->draw
);
231 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
232 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
234 pipe_sampler_view_reference(&softpipe
->vertex_sampler_views
[i
], view
);
235 sp_tex_tile_cache_set_sampler_view(softpipe
->vertex_tex_cache
[i
], view
);
238 softpipe
->num_vertex_sampler_views
= num
;
240 draw_set_sampler_views(softpipe
->draw
,
241 softpipe
->vertex_sampler_views
,
242 softpipe
->num_vertex_sampler_views
);
244 softpipe
->dirty
|= SP_NEW_TEXTURE
;
249 softpipe_set_geometry_sampler_views(struct pipe_context
*pipe
,
251 struct pipe_sampler_view
**views
)
253 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
256 assert(num
<= PIPE_MAX_GEOMETRY_SAMPLERS
);
258 /* Check for no-op */
259 if (num
== softpipe
->num_geometry_sampler_views
&&
260 !memcmp(softpipe
->geometry_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
264 draw_flush(softpipe
->draw
);
266 for (i
= 0; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; i
++) {
267 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
269 pipe_sampler_view_reference(&softpipe
->geometry_sampler_views
[i
], view
);
270 sp_tex_tile_cache_set_sampler_view(softpipe
->geometry_tex_cache
[i
], view
);
273 softpipe
->num_geometry_sampler_views
= num
;
275 softpipe
->dirty
|= SP_NEW_TEXTURE
;
280 * Find/create an sp_sampler_varient object for sampling the given texture,
281 * sampler and tex unit.
283 * Note that the tex unit is significant. We can't re-use a sampler
284 * varient for multiple texture units because the sampler varient contains
285 * the texture object pointer. If the texture object pointer were stored
286 * somewhere outside the sampler varient, we could re-use samplers for
287 * multiple texture units.
289 static struct sp_sampler_varient
*
290 get_sampler_varient( unsigned unit
,
291 struct sp_sampler
*sampler
,
292 struct pipe_sampler_view
*view
,
293 struct pipe_resource
*resource
,
296 struct softpipe_resource
*sp_texture
= softpipe_resource(resource
);
297 struct sp_sampler_varient
*v
= NULL
;
298 union sp_sampler_key key
;
300 /* if this fails, widen the key.unit field and update this assertion */
301 assert(PIPE_MAX_SAMPLERS
<= 16);
303 key
.bits
.target
= sp_texture
->base
.target
;
304 key
.bits
.is_pot
= sp_texture
->pot
;
305 key
.bits
.processor
= processor
;
306 key
.bits
.unit
= unit
;
307 key
.bits
.swizzle_r
= view
->swizzle_r
;
308 key
.bits
.swizzle_g
= view
->swizzle_g
;
309 key
.bits
.swizzle_b
= view
->swizzle_b
;
310 key
.bits
.swizzle_a
= view
->swizzle_a
;
313 if (sampler
->current
&&
314 key
.value
== sampler
->current
->key
.value
) {
315 v
= sampler
->current
;
319 for (v
= sampler
->varients
; v
; v
= v
->next
)
320 if (v
->key
.value
== key
.value
)
324 v
= sp_create_sampler_varient( &sampler
->base
, key
);
325 v
->next
= sampler
->varients
;
326 sampler
->varients
= v
;
330 sampler
->current
= v
;
336 softpipe_reset_sampler_varients(struct softpipe_context
*softpipe
)
340 /* It's a bit hard to build these samplers ahead of time -- don't
341 * really know which samplers are going to be used for vertex and
344 for (i
= 0; i
<= softpipe
->vs
->max_sampler
; i
++) {
345 if (softpipe
->vertex_samplers
[i
]) {
346 struct pipe_resource
*texture
= NULL
;
348 if (softpipe
->vertex_sampler_views
[i
]) {
349 texture
= softpipe
->vertex_sampler_views
[i
]->texture
;
352 softpipe
->tgsi
.vert_samplers_list
[i
] =
353 get_sampler_varient( i
,
354 sp_sampler(softpipe
->vertex_samplers
[i
]),
355 softpipe
->vertex_sampler_views
[i
],
357 TGSI_PROCESSOR_VERTEX
);
359 sp_sampler_varient_bind_texture( softpipe
->tgsi
.vert_samplers_list
[i
],
360 softpipe
->vertex_tex_cache
[i
],
366 for (i
= 0; i
<= softpipe
->gs
->max_sampler
; i
++) {
367 if (softpipe
->geometry_samplers
[i
]) {
368 struct pipe_resource
*texture
= NULL
;
370 if (softpipe
->geometry_sampler_views
[i
]) {
371 texture
= softpipe
->geometry_sampler_views
[i
]->texture
;
374 softpipe
->tgsi
.geom_samplers_list
[i
] =
377 sp_sampler(softpipe
->geometry_samplers
[i
]),
378 softpipe
->geometry_sampler_views
[i
],
380 TGSI_PROCESSOR_GEOMETRY
);
382 sp_sampler_varient_bind_texture(
383 softpipe
->tgsi
.geom_samplers_list
[i
],
384 softpipe
->geometry_tex_cache
[i
],
390 for (i
= 0; i
<= softpipe
->fs
->info
.file_max
[TGSI_FILE_SAMPLER
]; i
++) {
391 if (softpipe
->sampler
[i
]) {
392 struct pipe_resource
*texture
= NULL
;
394 if (softpipe
->sampler_views
[i
]) {
395 texture
= softpipe
->sampler_views
[i
]->texture
;
398 softpipe
->tgsi
.frag_samplers_list
[i
] =
399 get_sampler_varient( i
,
400 sp_sampler(softpipe
->sampler
[i
]),
401 softpipe
->sampler_views
[i
],
403 TGSI_PROCESSOR_FRAGMENT
);
405 sp_sampler_varient_bind_texture( softpipe
->tgsi
.frag_samplers_list
[i
],
406 softpipe
->tex_cache
[i
],
413 softpipe_delete_sampler_state(struct pipe_context
*pipe
,
416 struct sp_sampler
*sp_sampler
= (struct sp_sampler
*)sampler
;
417 struct sp_sampler_varient
*v
, *tmp
;
419 for (v
= sp_sampler
->varients
; v
; v
= tmp
) {
421 sp_sampler_varient_destroy(v
);
429 softpipe_init_sampler_funcs(struct pipe_context
*pipe
)
431 pipe
->create_sampler_state
= softpipe_create_sampler_state
;
432 pipe
->bind_fragment_sampler_states
= softpipe_bind_sampler_states
;
433 pipe
->bind_vertex_sampler_states
= softpipe_bind_vertex_sampler_states
;
434 pipe
->bind_geometry_sampler_states
= softpipe_bind_geometry_sampler_states
;
435 pipe
->delete_sampler_state
= softpipe_delete_sampler_state
;
437 pipe
->set_fragment_sampler_views
= softpipe_set_sampler_views
;
438 pipe
->set_vertex_sampler_views
= softpipe_set_vertex_sampler_views
;
439 pipe
->set_geometry_sampler_views
= softpipe_set_geometry_sampler_views
;
441 pipe
->create_sampler_view
= softpipe_create_sampler_view
;
442 pipe
->sampler_view_destroy
= softpipe_sampler_view_destroy
;