e51241892e1c0739da545f6ad24f2ab736588510
[mesa.git] / src / gallium / drivers / softpipe / sp_state_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 draw_flush(softpipe->draw);
61
62 /* set the new samplers */
63 for (i = 0; i < num; i++) {
64 softpipe->samplers[shader][start + i] = samplers[i];
65 }
66
67 /* find highest non-null samplers[] entry */
68 {
69 unsigned j = MAX2(softpipe->num_samplers[shader], start + num);
70 while (j > 0 && softpipe->samplers[shader][j - 1] == NULL)
71 j--;
72 softpipe->num_samplers[shader] = j;
73 }
74
75 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
76 draw_set_samplers(softpipe->draw,
77 shader,
78 softpipe->samplers[shader],
79 softpipe->num_samplers[shader]);
80 }
81
82 softpipe->dirty |= SP_NEW_SAMPLER;
83 }
84
85
86 static void
87 softpipe_sampler_view_destroy(struct pipe_context *pipe,
88 struct pipe_sampler_view *view)
89 {
90 pipe_resource_reference(&view->texture, NULL);
91 FREE(view);
92 }
93
94
95 void
96 softpipe_set_sampler_views(struct pipe_context *pipe,
97 unsigned shader,
98 unsigned start,
99 unsigned num,
100 struct pipe_sampler_view **views)
101 {
102 struct softpipe_context *softpipe = softpipe_context(pipe);
103 uint i;
104
105 assert(shader < PIPE_SHADER_TYPES);
106 assert(start + num <= Elements(softpipe->sampler_views[shader]));
107
108 draw_flush(softpipe->draw);
109
110 /* set the new sampler views */
111 for (i = 0; i < num; i++) {
112 struct sp_sampler_view *sp_sviewsrc;
113 struct sp_sampler_view *sp_sviewdst =
114 &softpipe->tgsi.sampler[shader]->sp_sview[start + i];
115 struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
116 pipe_sampler_view_reference(pview, views[i]);
117 sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
118 views[i]);
119 /*
120 * We don't really have variants, however some bits are different per shader,
121 * so just copy?
122 */
123 sp_sviewsrc = (struct sp_sampler_view *)*pview;
124 if (sp_sviewsrc) {
125 memcpy(sp_sviewdst, sp_sviewsrc, sizeof(*sp_sviewsrc));
126 sp_sviewdst->compute_lambda = softpipe_get_lambda_func(&sp_sviewdst->base, shader);
127 sp_sviewdst->cache = softpipe->tex_cache[shader][start + i];
128 }
129 else {
130 memset(sp_sviewdst, 0, sizeof(*sp_sviewsrc));
131 }
132 }
133
134
135 /* find highest non-null sampler_views[] entry */
136 {
137 unsigned j = MAX2(softpipe->num_sampler_views[shader], start + num);
138 while (j > 0 && softpipe->sampler_views[shader][j - 1] == NULL)
139 j--;
140 softpipe->num_sampler_views[shader] = j;
141 }
142
143 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
144 draw_set_sampler_views(softpipe->draw,
145 shader,
146 softpipe->sampler_views[shader],
147 softpipe->num_sampler_views[shader]);
148 }
149
150 softpipe->dirty |= SP_NEW_TEXTURE;
151 }
152
153
154 static void
155 softpipe_delete_sampler_state(struct pipe_context *pipe,
156 void *sampler)
157 {
158 FREE( sampler );
159 }
160
161
162 void
163 softpipe_init_sampler_funcs(struct pipe_context *pipe)
164 {
165 pipe->create_sampler_state = softpipe_create_sampler_state;
166 pipe->bind_sampler_states = softpipe_bind_sampler_states;
167 pipe->delete_sampler_state = softpipe_delete_sampler_state;
168
169 pipe->create_sampler_view = softpipe_create_sampler_view;
170 pipe->set_sampler_views = softpipe_set_sampler_views;
171 pipe->sampler_view_destroy = softpipe_sampler_view_destroy;
172 }
173