r600g/compute Improve debugging output
[mesa.git] / src / gallium / drivers / softpipe / sp_state_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /* Authors:
29 * Brian Paul
30 */
31
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34
35 #include "draw/draw_context.h"
36
37 #include "sp_context.h"
38 #include "sp_state.h"
39 #include "sp_texture.h"
40 #include "sp_tex_sample.h"
41 #include "sp_tex_tile_cache.h"
42
43
44 /**
45 * Bind a range [start, start+num-1] of samplers for a shader stage.
46 */
47 static void
48 softpipe_bind_sampler_states(struct pipe_context *pipe,
49 unsigned shader,
50 unsigned start,
51 unsigned num,
52 void **samplers)
53 {
54 struct softpipe_context *softpipe = softpipe_context(pipe);
55 unsigned i;
56
57 assert(shader < PIPE_SHADER_TYPES);
58 assert(start + num <= Elements(softpipe->samplers[shader]));
59
60 /* Check for no-op */
61 if (start + num <= softpipe->num_samplers[shader] &&
62 !memcmp(softpipe->samplers[shader] + start, samplers,
63 num * sizeof(void *))) {
64 return;
65 }
66
67 draw_flush(softpipe->draw);
68
69 /* set the new samplers */
70 for (i = 0; i < num; i++) {
71 softpipe->samplers[shader][start + i] = samplers[i];
72 }
73
74 /* find highest non-null samplers[] entry */
75 {
76 unsigned j = MAX2(softpipe->num_samplers[shader], start + num);
77 while (j > 0 && softpipe->samplers[shader][j - 1] == NULL)
78 j--;
79 softpipe->num_samplers[shader] = j;
80 }
81
82 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
83 draw_set_samplers(softpipe->draw,
84 shader,
85 softpipe->samplers[shader],
86 softpipe->num_samplers[shader]);
87 }
88
89 softpipe->dirty |= SP_NEW_SAMPLER;
90 }
91
92
93 static void
94 softpipe_sampler_view_destroy(struct pipe_context *pipe,
95 struct pipe_sampler_view *view)
96 {
97 pipe_resource_reference(&view->texture, NULL);
98 FREE(view);
99 }
100
101
102 void
103 softpipe_set_sampler_views(struct pipe_context *pipe,
104 unsigned shader,
105 unsigned start,
106 unsigned num,
107 struct pipe_sampler_view **views)
108 {
109 struct softpipe_context *softpipe = softpipe_context(pipe);
110 uint i;
111
112 assert(shader < PIPE_SHADER_TYPES);
113 assert(start + num <= Elements(softpipe->sampler_views[shader]));
114
115 /* Check for no-op */
116 if (start + num <= softpipe->num_sampler_views[shader] &&
117 !memcmp(softpipe->sampler_views[shader] + start, views,
118 num * sizeof(struct pipe_sampler_view *))) {
119 return;
120 }
121
122 draw_flush(softpipe->draw);
123
124 /* set the new sampler views */
125 for (i = 0; i < num; i++) {
126 struct sp_sampler_view *sp_sviewsrc;
127 struct sp_sampler_view *sp_sviewdst =
128 &softpipe->tgsi.sampler[shader]->sp_sview[start + i];
129 struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
130 pipe_sampler_view_reference(pview, views[i]);
131 sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
132 views[i]);
133 /*
134 * We don't really have variants, however some bits are different per shader,
135 * so just copy?
136 */
137 sp_sviewsrc = (struct sp_sampler_view *)*pview;
138 if (sp_sviewsrc) {
139 memcpy(sp_sviewdst, sp_sviewsrc, sizeof(*sp_sviewsrc));
140 sp_sviewdst->compute_lambda = softpipe_get_lambda_func(&sp_sviewdst->base, shader);
141 sp_sviewdst->cache = softpipe->tex_cache[shader][start + i];
142 }
143 else {
144 memset(sp_sviewdst, 0, sizeof(*sp_sviewsrc));
145 }
146 }
147
148
149 /* find highest non-null sampler_views[] entry */
150 {
151 unsigned j = MAX2(softpipe->num_sampler_views[shader], start + num);
152 while (j > 0 && softpipe->sampler_views[shader][j - 1] == NULL)
153 j--;
154 softpipe->num_sampler_views[shader] = j;
155 }
156
157 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
158 draw_set_sampler_views(softpipe->draw,
159 shader,
160 softpipe->sampler_views[shader],
161 softpipe->num_sampler_views[shader]);
162 }
163
164 softpipe->dirty |= SP_NEW_TEXTURE;
165 }
166
167
168 static void
169 softpipe_set_fragment_sampler_views(struct pipe_context *pipe,
170 unsigned num,
171 struct pipe_sampler_view **views)
172 {
173 softpipe_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num, views);
174 }
175
176
177 static void
178 softpipe_set_vertex_sampler_views(struct pipe_context *pipe,
179 unsigned num,
180 struct pipe_sampler_view **views)
181 {
182 softpipe_set_sampler_views(pipe, PIPE_SHADER_VERTEX, 0, num, views);
183 }
184
185
186 static void
187 softpipe_set_geometry_sampler_views(struct pipe_context *pipe,
188 unsigned num,
189 struct pipe_sampler_view **views)
190 {
191 softpipe_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY, 0, num, views);
192 }
193
194
195 static void
196 softpipe_delete_sampler_state(struct pipe_context *pipe,
197 void *sampler)
198 {
199 FREE( sampler );
200 }
201
202
203 void
204 softpipe_init_sampler_funcs(struct pipe_context *pipe)
205 {
206 pipe->create_sampler_state = softpipe_create_sampler_state;
207 pipe->bind_sampler_states = softpipe_bind_sampler_states;
208 pipe->delete_sampler_state = softpipe_delete_sampler_state;
209
210 pipe->set_fragment_sampler_views = softpipe_set_fragment_sampler_views;
211 pipe->set_vertex_sampler_views = softpipe_set_vertex_sampler_views;
212 pipe->set_geometry_sampler_views = softpipe_set_geometry_sampler_views;
213
214 pipe->create_sampler_view = softpipe_create_sampler_view;
215 pipe->sampler_view_destroy = softpipe_sampler_view_destroy;
216 }
217