i965/vec4: Make with_writemask() non-static.
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sampler_state.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "intel_batchbuffer.h"
28 #include "intel_mipmap_tree.h"
29
30 #include "main/macros.h"
31 #include "main/samplerobj.h"
32
33 /**
34 * Sets the sampler state for a single unit.
35 */
36 static void
37 gen7_update_sampler_state(struct brw_context *brw, int unit, int ss_index,
38 struct gen7_sampler_state *sampler,
39 uint32_t *sdc_offset)
40 {
41 struct gl_context *ctx = &brw->ctx;
42 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
43 struct gl_texture_object *texObj = texUnit->_Current;
44 struct intel_texture_image *intel_image =
45 intel_texture_image(texObj->Image[0][texObj->BaseLevel]);
46 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
47 bool using_nearest = false;
48
49 /* These don't use samplers at all. */
50 if (texObj->Target == GL_TEXTURE_BUFFER)
51 return;
52
53 switch (gl_sampler->MinFilter) {
54 case GL_NEAREST:
55 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
56 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
57 using_nearest = true;
58 break;
59 case GL_LINEAR:
60 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
61 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
62 break;
63 case GL_NEAREST_MIPMAP_NEAREST:
64 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
65 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
66 break;
67 case GL_LINEAR_MIPMAP_NEAREST:
68 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
69 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
70 break;
71 case GL_NEAREST_MIPMAP_LINEAR:
72 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
73 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
74 break;
75 case GL_LINEAR_MIPMAP_LINEAR:
76 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
77 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
78 break;
79 default:
80 break;
81 }
82
83 /* Set Anisotropy: */
84 if (gl_sampler->MaxAnisotropy > 1.0) {
85 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
86 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
87
88 if (gl_sampler->MaxAnisotropy > 2.0) {
89 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
90 BRW_ANISORATIO_16);
91 }
92 }
93 else {
94 switch (gl_sampler->MagFilter) {
95 case GL_NEAREST:
96 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
97 using_nearest = true;
98 break;
99 case GL_LINEAR:
100 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
101 break;
102 default:
103 break;
104 }
105 }
106
107 sampler->ss3.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
108 using_nearest);
109 sampler->ss3.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
110 using_nearest);
111 sampler->ss3.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
112 using_nearest);
113
114 /* Cube-maps on 965 and later must use the same wrap mode for all 3
115 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
116 */
117 if (texObj->Target == GL_TEXTURE_CUBE_MAP ||
118 texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
119 if (ctx->Texture.CubeMapSeamless &&
120 (gl_sampler->MinFilter != GL_NEAREST ||
121 gl_sampler->MagFilter != GL_NEAREST)) {
122 sampler->ss3.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
123 sampler->ss3.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
124 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
125 } else {
126 sampler->ss3.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
127 sampler->ss3.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
128 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
129 }
130 } else if (texObj->Target == GL_TEXTURE_1D) {
131 /* There's a bug in 1D texture sampling - it actually pays
132 * attention to the wrap_t value, though it should not.
133 * Override the wrap_t value here to GL_REPEAT to keep
134 * any nonexistent border pixels from floating in.
135 */
136 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
137 }
138
139 /* Set shadow function: */
140 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
141 /* Shadowing is "enabled" by emitting a particular sampler
142 * message (sample_c). So need to recompile WM program when
143 * shadow comparison is enabled on each/any texture unit.
144 */
145 sampler->ss1.shadow_function =
146 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
147 }
148
149 /* Set LOD bias: */
150 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
151 gl_sampler->LodBias, -16, 15), 8);
152
153 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
154 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
155
156 int baselevel = texObj->BaseLevel - intel_image->mt->first_level;
157 sampler->ss0.base_level = U_FIXED(baselevel, 1);
158
159 sampler->ss1.max_lod = U_FIXED(CLAMP(baselevel +
160 gl_sampler->MaxLod, 0, 13), 8);
161 sampler->ss1.min_lod = U_FIXED(CLAMP(baselevel +
162 gl_sampler->MinLod, 0, 13), 8);
163
164 /* The sampler can handle non-normalized texture rectangle coordinates
165 * natively
166 */
167 if (texObj->Target == GL_TEXTURE_RECTANGLE) {
168 sampler->ss3.non_normalized_coord = 1;
169 }
170
171 upload_default_color(brw, gl_sampler, unit, sdc_offset);
172
173 sampler->ss2.default_color_pointer = *sdc_offset >> 5;
174
175 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
176 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
177 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
178 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
179 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
180 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
181 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
182 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
183 }
184
185
186 static void
187 gen7_upload_sampler_state_table(struct brw_context *brw,
188 struct gl_program *prog,
189 uint32_t sampler_count,
190 uint32_t *sst_offset,
191 uint32_t *sdc_offset)
192 {
193 struct gl_context *ctx = &brw->ctx;
194 struct gen7_sampler_state *samplers;
195
196 GLbitfield SamplersUsed = prog->SamplersUsed;
197
198 if (sampler_count == 0)
199 return;
200
201 samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
202 sampler_count * sizeof(*samplers),
203 32, sst_offset);
204 memset(samplers, 0, sampler_count * sizeof(*samplers));
205
206 for (unsigned s = 0; s < sampler_count; s++) {
207 if (SamplersUsed & (1 << s)) {
208 const unsigned unit = prog->SamplerUnits[s];
209 if (ctx->Texture.Unit[unit]._ReallyEnabled)
210 gen7_update_sampler_state(brw, unit, s, &samplers[s],
211 &sdc_offset[s]);
212 }
213 }
214
215 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
216 }
217
218 void
219 gen7_init_vtable_sampler_functions(struct brw_context *brw)
220 {
221 brw->vtbl.upload_sampler_state_table = gen7_upload_sampler_state_table;
222 }