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"
34 #include "draw/draw_context.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_set_sampler_textures(struct pipe_context
*pipe
,
98 unsigned num
, struct pipe_texture
**texture
)
100 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
103 assert(num
<= PIPE_MAX_SAMPLERS
);
105 /* Check for no-op */
106 if (num
== softpipe
->num_textures
&&
107 !memcmp(softpipe
->texture
, texture
, num
* sizeof(struct pipe_texture
*)))
110 draw_flush(softpipe
->draw
);
112 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
113 struct pipe_texture
*tex
= i
< num
? texture
[i
] : NULL
;
115 pipe_texture_reference(&softpipe
->texture
[i
], tex
);
116 sp_tex_tile_cache_set_texture(softpipe
->tex_cache
[i
], tex
);
119 softpipe
->num_textures
= num
;
121 softpipe
->dirty
|= SP_NEW_TEXTURE
;
126 * Find/create an sp_sampler_varient object for sampling the given texture,
127 * sampler and tex unit.
129 * Note that the tex unit is significant. We can't re-use a sampler
130 * varient for multiple texture units because the sampler varient contains
131 * the texture object pointer. If the texture object pointer were stored
132 * somewhere outside the sampler varient, we could re-use samplers for
133 * multiple texture units.
135 static struct sp_sampler_varient
*
136 get_sampler_varient( unsigned unit
,
137 struct sp_sampler
*sampler
,
138 struct pipe_texture
*texture
,
141 struct softpipe_texture
*sp_texture
= softpipe_texture(texture
);
142 struct sp_sampler_varient
*v
= NULL
;
143 union sp_sampler_key key
;
145 /* if this fails, widen the key.unit field and update this assertion */
146 assert(PIPE_MAX_SAMPLERS
<= 16);
148 key
.bits
.target
= sp_texture
->base
.target
;
149 key
.bits
.is_pot
= sp_texture
->pot
;
150 key
.bits
.processor
= processor
;
151 key
.bits
.unit
= unit
;
154 if (sampler
->current
&&
155 key
.value
== sampler
->current
->key
.value
) {
156 v
= sampler
->current
;
160 for (v
= sampler
->varients
; v
; v
= v
->next
)
161 if (v
->key
.value
== key
.value
)
165 v
= sp_create_sampler_varient( &sampler
->base
, key
);
166 v
->next
= sampler
->varients
;
167 sampler
->varients
= v
;
171 sampler
->current
= v
;
179 softpipe_reset_sampler_varients(struct softpipe_context
*softpipe
)
183 /* It's a bit hard to build these samplers ahead of time -- don't
184 * really know which samplers are going to be used for vertex and
187 for (i
= 0; i
<= softpipe
->vs
->max_sampler
; i
++) {
188 if (softpipe
->sampler
[i
]) {
189 softpipe
->tgsi
.vert_samplers_list
[i
] =
190 get_sampler_varient( i
,
191 sp_sampler(softpipe
->sampler
[i
]),
192 softpipe
->texture
[i
],
193 TGSI_PROCESSOR_VERTEX
);
195 sp_sampler_varient_bind_texture( softpipe
->tgsi
.vert_samplers_list
[i
],
196 softpipe
->tex_cache
[i
],
197 softpipe
->texture
[i
] );
201 for (i
= 0; i
<= softpipe
->fs
->info
.file_max
[TGSI_FILE_SAMPLER
]; i
++) {
202 if (softpipe
->sampler
[i
]) {
203 softpipe
->tgsi
.frag_samplers_list
[i
] =
204 get_sampler_varient( i
,
205 sp_sampler(softpipe
->sampler
[i
]),
206 softpipe
->texture
[i
],
207 TGSI_PROCESSOR_FRAGMENT
);
209 sp_sampler_varient_bind_texture( softpipe
->tgsi
.frag_samplers_list
[i
],
210 softpipe
->tex_cache
[i
],
211 softpipe
->texture
[i
] );
219 softpipe_delete_sampler_state(struct pipe_context
*pipe
,
222 struct sp_sampler
*sp_sampler
= (struct sp_sampler
*)sampler
;
223 struct sp_sampler_varient
*v
, *tmp
;
225 for (v
= sp_sampler
->varients
; v
; v
= tmp
) {
227 sp_sampler_varient_destroy(v
);