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 struct pipe_sampler_view
*
126 softpipe_create_sampler_view(struct pipe_context
*pipe
,
127 struct pipe_resource
*resource
,
128 const struct pipe_sampler_view
*templ
)
130 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
134 view
->reference
.count
= 1;
135 view
->texture
= NULL
;
136 pipe_resource_reference(&view
->texture
, resource
);
137 view
->context
= pipe
;
145 softpipe_sampler_view_destroy(struct pipe_context
*pipe
,
146 struct pipe_sampler_view
*view
)
148 pipe_resource_reference(&view
->texture
, NULL
);
154 softpipe_set_sampler_views(struct pipe_context
*pipe
,
156 struct pipe_sampler_view
**views
)
158 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
161 assert(num
<= PIPE_MAX_SAMPLERS
);
163 /* Check for no-op */
164 if (num
== softpipe
->num_sampler_views
&&
165 !memcmp(softpipe
->sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*)))
168 draw_flush(softpipe
->draw
);
170 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
171 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
173 pipe_sampler_view_reference(&softpipe
->sampler_views
[i
], view
);
174 sp_tex_tile_cache_set_sampler_view(softpipe
->tex_cache
[i
], view
);
177 softpipe
->num_sampler_views
= num
;
179 softpipe
->dirty
|= SP_NEW_TEXTURE
;
184 softpipe_set_vertex_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_VERTEX_SAMPLERS
);
193 /* Check for no-op */
194 if (num
== softpipe
->num_vertex_sampler_views
&&
195 !memcmp(softpipe
->vertex_sampler_views
, views
, num
* sizeof(struct pipe_sampler_view
*))) {
199 draw_flush(softpipe
->draw
);
201 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
202 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
204 pipe_sampler_view_reference(&softpipe
->vertex_sampler_views
[i
], view
);
205 sp_tex_tile_cache_set_sampler_view(softpipe
->vertex_tex_cache
[i
], view
);
208 softpipe
->num_vertex_sampler_views
= num
;
210 softpipe
->dirty
|= SP_NEW_TEXTURE
;
215 * Find/create an sp_sampler_varient object for sampling the given texture,
216 * sampler and tex unit.
218 * Note that the tex unit is significant. We can't re-use a sampler
219 * varient for multiple texture units because the sampler varient contains
220 * the texture object pointer. If the texture object pointer were stored
221 * somewhere outside the sampler varient, we could re-use samplers for
222 * multiple texture units.
224 static struct sp_sampler_varient
*
225 get_sampler_varient( unsigned unit
,
226 struct sp_sampler
*sampler
,
227 struct pipe_resource
*resource
,
230 struct softpipe_resource
*sp_texture
= softpipe_resource(resource
);
231 struct sp_sampler_varient
*v
= NULL
;
232 union sp_sampler_key key
;
234 /* if this fails, widen the key.unit field and update this assertion */
235 assert(PIPE_MAX_SAMPLERS
<= 16);
237 key
.bits
.target
= sp_texture
->base
.target
;
238 key
.bits
.is_pot
= sp_texture
->pot
;
239 key
.bits
.processor
= processor
;
240 key
.bits
.unit
= unit
;
243 if (sampler
->current
&&
244 key
.value
== sampler
->current
->key
.value
) {
245 v
= sampler
->current
;
249 for (v
= sampler
->varients
; v
; v
= v
->next
)
250 if (v
->key
.value
== key
.value
)
254 v
= sp_create_sampler_varient( &sampler
->base
, key
);
255 v
->next
= sampler
->varients
;
256 sampler
->varients
= v
;
260 sampler
->current
= v
;
268 softpipe_reset_sampler_varients(struct softpipe_context
*softpipe
)
272 /* It's a bit hard to build these samplers ahead of time -- don't
273 * really know which samplers are going to be used for vertex and
276 for (i
= 0; i
<= softpipe
->vs
->max_sampler
; i
++) {
277 if (softpipe
->vertex_samplers
[i
]) {
278 struct pipe_resource
*texture
= NULL
;
280 if (softpipe
->vertex_sampler_views
[i
]) {
281 texture
= softpipe
->vertex_sampler_views
[i
]->texture
;
284 softpipe
->tgsi
.vert_samplers_list
[i
] =
285 get_sampler_varient( i
,
286 sp_sampler(softpipe
->vertex_samplers
[i
]),
288 TGSI_PROCESSOR_VERTEX
);
290 sp_sampler_varient_bind_texture( softpipe
->tgsi
.vert_samplers_list
[i
],
291 softpipe
->vertex_tex_cache
[i
],
296 for (i
= 0; i
<= softpipe
->fs
->info
.file_max
[TGSI_FILE_SAMPLER
]; i
++) {
297 if (softpipe
->sampler
[i
]) {
298 struct pipe_resource
*texture
= NULL
;
300 if (softpipe
->sampler_views
[i
]) {
301 texture
= softpipe
->sampler_views
[i
]->texture
;
304 softpipe
->tgsi
.frag_samplers_list
[i
] =
305 get_sampler_varient( i
,
306 sp_sampler(softpipe
->sampler
[i
]),
308 TGSI_PROCESSOR_FRAGMENT
);
310 sp_sampler_varient_bind_texture( softpipe
->tgsi
.frag_samplers_list
[i
],
311 softpipe
->tex_cache
[i
],
320 softpipe_delete_sampler_state(struct pipe_context
*pipe
,
323 struct sp_sampler
*sp_sampler
= (struct sp_sampler
*)sampler
;
324 struct sp_sampler_varient
*v
, *tmp
;
326 for (v
= sp_sampler
->varients
; v
; v
= tmp
) {
328 sp_sampler_varient_destroy(v
);