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"
36 #include "draw/draw_context.h"
38 #include "sp_context.h"
40 #include "sp_texture.h"
41 #include "sp_tex_sample.h"
42 #include "sp_tex_tile_cache.h"
46 struct pipe_sampler_state base
;
47 struct sp_sampler_varient
*varients
;
48 struct sp_sampler_varient
*current
;
51 static struct sp_sampler
*sp_sampler( struct pipe_sampler_state
*sampler
)
53 return (struct sp_sampler
*)sampler
;
58 softpipe_create_sampler_state(struct pipe_context
*pipe
,
59 const struct pipe_sampler_state
*sampler
)
61 struct sp_sampler
*sp_sampler
= CALLOC_STRUCT(sp_sampler
);
63 sp_sampler
->base
= *sampler
;
64 sp_sampler
->varients
= NULL
;
66 return (void *)sp_sampler
;
71 softpipe_bind_sampler_states(struct pipe_context
*pipe
,
72 unsigned num
, void **sampler
)
74 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
77 assert(num
<= PIPE_MAX_SAMPLERS
);
80 if (num
== softpipe
->num_samplers
&&
81 !memcmp(softpipe
->sampler
, sampler
, num
* sizeof(void *)))
84 draw_flush(softpipe
->draw
);
86 for (i
= 0; i
< num
; ++i
)
87 softpipe
->sampler
[i
] = sampler
[i
];
88 for (i
= num
; i
< PIPE_MAX_SAMPLERS
; ++i
)
89 softpipe
->sampler
[i
] = NULL
;
91 softpipe
->num_samplers
= num
;
93 softpipe
->dirty
|= SP_NEW_SAMPLER
;
98 softpipe_bind_vertex_sampler_states(struct pipe_context
*pipe
,
99 unsigned num_samplers
,
102 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
105 assert(num_samplers
<= PIPE_MAX_VERTEX_SAMPLERS
);
107 /* Check for no-op */
108 if (num_samplers
== softpipe
->num_vertex_samplers
&&
109 !memcmp(softpipe
->vertex_samplers
, samplers
, num_samplers
* sizeof(void *)))
112 draw_flush(softpipe
->draw
);
114 for (i
= 0; i
< num_samplers
; ++i
)
115 softpipe
->vertex_samplers
[i
] = samplers
[i
];
116 for (i
= num_samplers
; i
< PIPE_MAX_VERTEX_SAMPLERS
; ++i
)
117 softpipe
->vertex_samplers
[i
] = NULL
;
119 softpipe
->num_vertex_samplers
= num_samplers
;
121 draw_set_samplers(softpipe
->draw
,
122 softpipe
->vertex_samplers
,
123 softpipe
->num_vertex_samplers
);
125 softpipe
->dirty
|= SP_NEW_SAMPLER
;
129 softpipe_bind_geometry_sampler_states(struct pipe_context
*pipe
,
130 unsigned num_samplers
,
133 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
136 assert(num_samplers
<= PIPE_MAX_GEOMETRY_SAMPLERS
);
138 /* Check for no-op */
139 if (num_samplers
== softpipe
->num_geometry_samplers
&&
140 !memcmp(softpipe
->geometry_samplers
, samplers
, num_samplers
* sizeof(void *)))
143 draw_flush(softpipe
->draw
);
145 for (i
= 0; i
< num_samplers
; ++i
)
146 softpipe
->geometry_samplers
[i
] = samplers
[i
];
147 for (i
= num_samplers
; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; ++i
)
148 softpipe
->geometry_samplers
[i
] = NULL
;
150 softpipe
->num_geometry_samplers
= num_samplers
;
152 softpipe
->dirty
|= SP_NEW_SAMPLER
;
156 static struct pipe_sampler_view
*
157 softpipe_create_sampler_view(struct pipe_context
*pipe
,
158 struct pipe_resource
*resource
,
159 const struct pipe_sampler_view
*templ
)
161 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
165 view
->reference
.count
= 1;
166 view
->texture
= NULL
;
167 pipe_resource_reference(&view
->texture
, resource
);
168 view
->context
= pipe
;
176 softpipe_sampler_view_destroy(struct pipe_context
*pipe
,
177 struct pipe_sampler_view
*view
)
179 pipe_resource_reference(&view
->texture
, NULL
);
185 softpipe_set_sampler_views(struct pipe_context
*pipe
,
187 struct pipe_sampler_view
**views
)
189 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
192 assert(num
<= PIPE_MAX_SAMPLERS
);
194 /* Check for no-op */
195 if (num
== softpipe
->num_sampler_views
&&
196 !memcmp(softpipe
->sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*)))
199 draw_flush(softpipe
->draw
);
201 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
202 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
204 pipe_sampler_view_reference(&softpipe
->sampler_views
[i
], view
);
205 sp_tex_tile_cache_set_sampler_view(softpipe
->tex_cache
[i
], view
);
208 softpipe
->num_sampler_views
= num
;
210 softpipe
->dirty
|= SP_NEW_TEXTURE
;
215 softpipe_set_vertex_sampler_views(struct pipe_context
*pipe
,
217 struct pipe_sampler_view
**views
)
219 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
222 assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
224 /* Check for no-op */
225 if (num
== softpipe
->num_vertex_sampler_views
&&
226 !memcmp(softpipe
->vertex_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
230 draw_flush(softpipe
->draw
);
232 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
233 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
235 pipe_sampler_view_reference(&softpipe
->vertex_sampler_views
[i
], view
);
236 sp_tex_tile_cache_set_sampler_view(softpipe
->vertex_tex_cache
[i
], view
);
239 softpipe
->num_vertex_sampler_views
= num
;
241 draw_set_sampler_views(softpipe
->draw
,
242 softpipe
->vertex_sampler_views
,
243 softpipe
->num_vertex_sampler_views
);
245 softpipe
->dirty
|= SP_NEW_TEXTURE
;
250 softpipe_set_geometry_sampler_views(struct pipe_context
*pipe
,
252 struct pipe_sampler_view
**views
)
254 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
257 assert(num
<= PIPE_MAX_GEOMETRY_SAMPLERS
);
259 /* Check for no-op */
260 if (num
== softpipe
->num_geometry_sampler_views
&&
261 !memcmp(softpipe
->geometry_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
265 draw_flush(softpipe
->draw
);
267 for (i
= 0; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; i
++) {
268 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
270 pipe_sampler_view_reference(&softpipe
->geometry_sampler_views
[i
], view
);
271 sp_tex_tile_cache_set_sampler_view(softpipe
->geometry_tex_cache
[i
], view
);
274 softpipe
->num_geometry_sampler_views
= num
;
276 softpipe
->dirty
|= SP_NEW_TEXTURE
;
281 * Find/create an sp_sampler_varient object for sampling the given texture,
282 * sampler and tex unit.
284 * Note that the tex unit is significant. We can't re-use a sampler
285 * varient for multiple texture units because the sampler varient contains
286 * the texture object pointer. If the texture object pointer were stored
287 * somewhere outside the sampler varient, we could re-use samplers for
288 * multiple texture units.
290 static struct sp_sampler_varient
*
291 get_sampler_varient( unsigned unit
,
292 struct sp_sampler
*sampler
,
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
;
309 if (sampler
->current
&&
310 key
.value
== sampler
->current
->key
.value
) {
311 v
= sampler
->current
;
315 for (v
= sampler
->varients
; v
; v
= v
->next
)
316 if (v
->key
.value
== key
.value
)
320 v
= sp_create_sampler_varient( &sampler
->base
, key
);
321 v
->next
= sampler
->varients
;
322 sampler
->varients
= v
;
326 sampler
->current
= v
;
332 softpipe_reset_sampler_varients(struct softpipe_context
*softpipe
)
336 /* It's a bit hard to build these samplers ahead of time -- don't
337 * really know which samplers are going to be used for vertex and
340 for (i
= 0; i
<= softpipe
->vs
->max_sampler
; i
++) {
341 if (softpipe
->vertex_samplers
[i
]) {
342 struct pipe_resource
*texture
= NULL
;
344 if (softpipe
->vertex_sampler_views
[i
]) {
345 texture
= softpipe
->vertex_sampler_views
[i
]->texture
;
348 softpipe
->tgsi
.vert_samplers_list
[i
] =
349 get_sampler_varient( i
,
350 sp_sampler(softpipe
->vertex_samplers
[i
]),
352 TGSI_PROCESSOR_VERTEX
);
354 sp_sampler_varient_bind_texture( softpipe
->tgsi
.vert_samplers_list
[i
],
355 softpipe
->vertex_tex_cache
[i
],
361 for (i
= 0; i
<= softpipe
->gs
->max_sampler
; i
++) {
362 if (softpipe
->geometry_samplers
[i
]) {
363 struct pipe_resource
*texture
= NULL
;
365 if (softpipe
->geometry_sampler_views
[i
]) {
366 texture
= softpipe
->geometry_sampler_views
[i
]->texture
;
369 softpipe
->tgsi
.geom_samplers_list
[i
] =
372 sp_sampler(softpipe
->geometry_samplers
[i
]),
374 TGSI_PROCESSOR_GEOMETRY
);
376 sp_sampler_varient_bind_texture(
377 softpipe
->tgsi
.geom_samplers_list
[i
],
378 softpipe
->geometry_tex_cache
[i
],
384 for (i
= 0; i
<= softpipe
->fs
->info
.file_max
[TGSI_FILE_SAMPLER
]; i
++) {
385 if (softpipe
->sampler
[i
]) {
386 struct pipe_resource
*texture
= NULL
;
388 if (softpipe
->sampler_views
[i
]) {
389 texture
= softpipe
->sampler_views
[i
]->texture
;
392 softpipe
->tgsi
.frag_samplers_list
[i
] =
393 get_sampler_varient( i
,
394 sp_sampler(softpipe
->sampler
[i
]),
396 TGSI_PROCESSOR_FRAGMENT
);
398 sp_sampler_varient_bind_texture( softpipe
->tgsi
.frag_samplers_list
[i
],
399 softpipe
->tex_cache
[i
],
406 softpipe_delete_sampler_state(struct pipe_context
*pipe
,
409 struct sp_sampler
*sp_sampler
= (struct sp_sampler
*)sampler
;
410 struct sp_sampler_varient
*v
, *tmp
;
412 for (v
= sp_sampler
->varients
; v
; v
= tmp
) {
414 sp_sampler_varient_destroy(v
);
422 softpipe_init_sampler_funcs(struct pipe_context
*pipe
)
424 pipe
->create_sampler_state
= softpipe_create_sampler_state
;
425 pipe
->bind_fragment_sampler_states
= softpipe_bind_sampler_states
;
426 pipe
->bind_vertex_sampler_states
= softpipe_bind_vertex_sampler_states
;
427 pipe
->bind_geometry_sampler_states
= softpipe_bind_geometry_sampler_states
;
428 pipe
->delete_sampler_state
= softpipe_delete_sampler_state
;
430 pipe
->set_fragment_sampler_views
= softpipe_set_sampler_views
;
431 pipe
->set_vertex_sampler_views
= softpipe_set_vertex_sampler_views
;
432 pipe
->set_geometry_sampler_views
= softpipe_set_geometry_sampler_views
;
434 pipe
->create_sampler_view
= softpipe_create_sampler_view
;
435 pipe
->sampler_view_destroy
= softpipe_sampler_view_destroy
;