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 softpipe
->dirty
|= SP_NEW_SAMPLER
;
125 softpipe_bind_geometry_sampler_states(struct pipe_context
*pipe
,
126 unsigned num_samplers
,
129 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
132 assert(num_samplers
<= PIPE_MAX_GEOMETRY_SAMPLERS
);
134 /* Check for no-op */
135 if (num_samplers
== softpipe
->num_geometry_samplers
&&
136 !memcmp(softpipe
->geometry_samplers
, samplers
, num_samplers
* sizeof(void *)))
139 draw_flush(softpipe
->draw
);
141 for (i
= 0; i
< num_samplers
; ++i
)
142 softpipe
->geometry_samplers
[i
] = samplers
[i
];
143 for (i
= num_samplers
; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; ++i
)
144 softpipe
->geometry_samplers
[i
] = NULL
;
146 softpipe
->num_geometry_samplers
= num_samplers
;
148 softpipe
->dirty
|= SP_NEW_SAMPLER
;
152 struct pipe_sampler_view
*
153 softpipe_create_sampler_view(struct pipe_context
*pipe
,
154 struct pipe_resource
*resource
,
155 const struct pipe_sampler_view
*templ
)
157 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
161 view
->reference
.count
= 1;
162 view
->texture
= NULL
;
163 pipe_resource_reference(&view
->texture
, resource
);
164 view
->context
= pipe
;
172 softpipe_sampler_view_destroy(struct pipe_context
*pipe
,
173 struct pipe_sampler_view
*view
)
175 pipe_resource_reference(&view
->texture
, NULL
);
181 softpipe_set_sampler_views(struct pipe_context
*pipe
,
183 struct pipe_sampler_view
**views
)
185 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
188 assert(num
<= PIPE_MAX_SAMPLERS
);
190 /* Check for no-op */
191 if (num
== softpipe
->num_sampler_views
&&
192 !memcmp(softpipe
->sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*)))
195 draw_flush(softpipe
->draw
);
197 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
198 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
200 pipe_sampler_view_reference(&softpipe
->sampler_views
[i
], view
);
201 sp_tex_tile_cache_set_sampler_view(softpipe
->tex_cache
[i
], view
);
204 softpipe
->num_sampler_views
= num
;
206 softpipe
->dirty
|= SP_NEW_TEXTURE
;
211 softpipe_set_vertex_sampler_views(struct pipe_context
*pipe
,
213 struct pipe_sampler_view
**views
)
215 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
218 assert(num
<= PIPE_MAX_VERTEX_SAMPLERS
);
220 /* Check for no-op */
221 if (num
== softpipe
->num_vertex_sampler_views
&&
222 !memcmp(softpipe
->vertex_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
226 draw_flush(softpipe
->draw
);
228 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
229 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
231 pipe_sampler_view_reference(&softpipe
->vertex_sampler_views
[i
], view
);
232 sp_tex_tile_cache_set_sampler_view(softpipe
->vertex_tex_cache
[i
], view
);
235 softpipe
->num_vertex_sampler_views
= num
;
237 softpipe
->dirty
|= SP_NEW_TEXTURE
;
241 softpipe_set_geometry_sampler_views(struct pipe_context
*pipe
,
243 struct pipe_sampler_view
**views
)
245 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
248 assert(num
<= PIPE_MAX_GEOMETRY_SAMPLERS
);
250 /* Check for no-op */
251 if (num
== softpipe
->num_geometry_sampler_views
&&
252 !memcmp(softpipe
->geometry_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
256 draw_flush(softpipe
->draw
);
258 for (i
= 0; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; i
++) {
259 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
261 pipe_sampler_view_reference(&softpipe
->geometry_sampler_views
[i
], view
);
262 sp_tex_tile_cache_set_sampler_view(softpipe
->geometry_tex_cache
[i
], view
);
265 softpipe
->num_geometry_sampler_views
= num
;
267 softpipe
->dirty
|= SP_NEW_TEXTURE
;
272 * Find/create an sp_sampler_varient object for sampling the given texture,
273 * sampler and tex unit.
275 * Note that the tex unit is significant. We can't re-use a sampler
276 * varient for multiple texture units because the sampler varient contains
277 * the texture object pointer. If the texture object pointer were stored
278 * somewhere outside the sampler varient, we could re-use samplers for
279 * multiple texture units.
281 static struct sp_sampler_varient
*
282 get_sampler_varient( unsigned unit
,
283 struct sp_sampler
*sampler
,
284 struct pipe_resource
*resource
,
287 struct softpipe_resource
*sp_texture
= softpipe_resource(resource
);
288 struct sp_sampler_varient
*v
= NULL
;
289 union sp_sampler_key key
;
291 /* if this fails, widen the key.unit field and update this assertion */
292 assert(PIPE_MAX_SAMPLERS
<= 16);
294 key
.bits
.target
= sp_texture
->base
.target
;
295 key
.bits
.is_pot
= sp_texture
->pot
;
296 key
.bits
.processor
= processor
;
297 key
.bits
.unit
= unit
;
300 if (sampler
->current
&&
301 key
.value
== sampler
->current
->key
.value
) {
302 v
= sampler
->current
;
306 for (v
= sampler
->varients
; v
; v
= v
->next
)
307 if (v
->key
.value
== key
.value
)
311 v
= sp_create_sampler_varient( &sampler
->base
, key
);
312 v
->next
= sampler
->varients
;
313 sampler
->varients
= v
;
317 sampler
->current
= v
;
325 softpipe_reset_sampler_varients(struct softpipe_context
*softpipe
)
329 /* It's a bit hard to build these samplers ahead of time -- don't
330 * really know which samplers are going to be used for vertex and
333 for (i
= 0; i
<= softpipe
->vs
->max_sampler
; i
++) {
334 if (softpipe
->vertex_samplers
[i
]) {
335 struct pipe_resource
*texture
= NULL
;
337 if (softpipe
->vertex_sampler_views
[i
]) {
338 texture
= softpipe
->vertex_sampler_views
[i
]->texture
;
341 softpipe
->tgsi
.vert_samplers_list
[i
] =
342 get_sampler_varient( i
,
343 sp_sampler(softpipe
->vertex_samplers
[i
]),
345 TGSI_PROCESSOR_VERTEX
);
347 sp_sampler_varient_bind_texture( softpipe
->tgsi
.vert_samplers_list
[i
],
348 softpipe
->vertex_tex_cache
[i
],
354 for (i
= 0; i
<= softpipe
->gs
->max_sampler
; i
++) {
355 if (softpipe
->geometry_samplers
[i
]) {
356 struct pipe_resource
*texture
= NULL
;
358 if (softpipe
->geometry_sampler_views
[i
]) {
359 texture
= softpipe
->geometry_sampler_views
[i
]->texture
;
362 softpipe
->tgsi
.geom_samplers_list
[i
] =
365 sp_sampler(softpipe
->geometry_samplers
[i
]),
367 TGSI_PROCESSOR_GEOMETRY
);
369 sp_sampler_varient_bind_texture(
370 softpipe
->tgsi
.geom_samplers_list
[i
],
371 softpipe
->geometry_tex_cache
[i
],
377 for (i
= 0; i
<= softpipe
->fs
->info
.file_max
[TGSI_FILE_SAMPLER
]; i
++) {
378 if (softpipe
->sampler
[i
]) {
379 struct pipe_resource
*texture
= NULL
;
381 if (softpipe
->sampler_views
[i
]) {
382 texture
= softpipe
->sampler_views
[i
]->texture
;
385 softpipe
->tgsi
.frag_samplers_list
[i
] =
386 get_sampler_varient( i
,
387 sp_sampler(softpipe
->sampler
[i
]),
389 TGSI_PROCESSOR_FRAGMENT
);
391 sp_sampler_varient_bind_texture( softpipe
->tgsi
.frag_samplers_list
[i
],
392 softpipe
->tex_cache
[i
],
401 softpipe_delete_sampler_state(struct pipe_context
*pipe
,
404 struct sp_sampler
*sp_sampler
= (struct sp_sampler
*)sampler
;
405 struct sp_sampler_varient
*v
, *tmp
;
407 for (v
= sp_sampler
->varients
; v
; v
= tmp
) {
409 sp_sampler_varient_destroy(v
);