1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "pipe/p_defines.h"
27bf215546Sopenharmony_ci#include "util/u_bitmask.h"
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci#include "util/u_inlines.h"
30bf215546Sopenharmony_ci#include "util/u_math.h"
31bf215546Sopenharmony_ci#include "util/u_memory.h"
32bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "svga_context.h"
35bf215546Sopenharmony_ci#include "svga_cmd.h"
36bf215546Sopenharmony_ci#include "svga_debug.h"
37bf215546Sopenharmony_ci#include "svga_resource_texture.h"
38bf215546Sopenharmony_ci#include "svga_surface.h"
39bf215546Sopenharmony_ci#include "svga_sampler_view.h"
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistatic inline unsigned
43bf215546Sopenharmony_citranslate_wrap_mode(unsigned wrap)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   switch (wrap) {
46bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_REPEAT:
47bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_WRAP;
48bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP:
49bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_CLAMP;
50bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
51bf215546Sopenharmony_ci      /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
52bf215546Sopenharmony_ci       * hardware.
53bf215546Sopenharmony_ci       */
54bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_CLAMP;
55bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
56bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_BORDER;
57bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_REPEAT:
58bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_MIRROR;
59bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP:
60bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
61bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
62bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_MIRRORONCE;
63bf215546Sopenharmony_ci   default:
64bf215546Sopenharmony_ci      assert(0);
65bf215546Sopenharmony_ci      return SVGA3D_TEX_ADDRESS_WRAP;
66bf215546Sopenharmony_ci   }
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistatic inline unsigned
71bf215546Sopenharmony_citranslate_img_filter(unsigned filter)
72bf215546Sopenharmony_ci{
73bf215546Sopenharmony_ci   switch (filter) {
74bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_NEAREST:
75bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_NEAREST;
76bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_LINEAR:
77bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_LINEAR;
78bf215546Sopenharmony_ci   default:
79bf215546Sopenharmony_ci      assert(0);
80bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_NEAREST;
81bf215546Sopenharmony_ci   }
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistatic inline unsigned
86bf215546Sopenharmony_citranslate_mip_filter(unsigned filter)
87bf215546Sopenharmony_ci{
88bf215546Sopenharmony_ci   switch (filter) {
89bf215546Sopenharmony_ci   case PIPE_TEX_MIPFILTER_NONE:
90bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_NONE;
91bf215546Sopenharmony_ci   case PIPE_TEX_MIPFILTER_NEAREST:
92bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_NEAREST;
93bf215546Sopenharmony_ci   case PIPE_TEX_MIPFILTER_LINEAR:
94bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_LINEAR;
95bf215546Sopenharmony_ci   default:
96bf215546Sopenharmony_ci      assert(0);
97bf215546Sopenharmony_ci      return SVGA3D_TEX_FILTER_NONE;
98bf215546Sopenharmony_ci   }
99bf215546Sopenharmony_ci}
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_cistatic uint8
103bf215546Sopenharmony_citranslate_comparison_func(unsigned func)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   switch (func) {
106bf215546Sopenharmony_ci   case PIPE_FUNC_NEVER:
107bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_NEVER;
108bf215546Sopenharmony_ci   case PIPE_FUNC_LESS:
109bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_LESS;
110bf215546Sopenharmony_ci   case PIPE_FUNC_EQUAL:
111bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_EQUAL;
112bf215546Sopenharmony_ci   case PIPE_FUNC_LEQUAL:
113bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_LESS_EQUAL;
114bf215546Sopenharmony_ci   case PIPE_FUNC_GREATER:
115bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_GREATER;
116bf215546Sopenharmony_ci   case PIPE_FUNC_NOTEQUAL:
117bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_NOT_EQUAL;
118bf215546Sopenharmony_ci   case PIPE_FUNC_GEQUAL:
119bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_GREATER_EQUAL;
120bf215546Sopenharmony_ci   case PIPE_FUNC_ALWAYS:
121bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_ALWAYS;
122bf215546Sopenharmony_ci   default:
123bf215546Sopenharmony_ci      assert(!"Invalid comparison function");
124bf215546Sopenharmony_ci      return SVGA3D_COMPARISON_ALWAYS;
125bf215546Sopenharmony_ci   }
126bf215546Sopenharmony_ci}
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci/**
130bf215546Sopenharmony_ci * Translate filtering state to vgpu10 format.
131bf215546Sopenharmony_ci */
132bf215546Sopenharmony_cistatic SVGA3dFilter
133bf215546Sopenharmony_citranslate_filter_mode(unsigned img_filter,
134bf215546Sopenharmony_ci                      unsigned min_filter,
135bf215546Sopenharmony_ci                      unsigned mag_filter,
136bf215546Sopenharmony_ci                      boolean anisotropic,
137bf215546Sopenharmony_ci                      boolean compare)
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci   SVGA3dFilter mode = 0;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   if (img_filter == PIPE_TEX_FILTER_LINEAR)
142bf215546Sopenharmony_ci      mode |= SVGA3D_FILTER_MIP_LINEAR;
143bf215546Sopenharmony_ci   if (min_filter == PIPE_TEX_FILTER_LINEAR)
144bf215546Sopenharmony_ci      mode |= SVGA3D_FILTER_MIN_LINEAR;
145bf215546Sopenharmony_ci   if (mag_filter == PIPE_TEX_FILTER_LINEAR)
146bf215546Sopenharmony_ci      mode |= SVGA3D_FILTER_MAG_LINEAR;
147bf215546Sopenharmony_ci   if (anisotropic)
148bf215546Sopenharmony_ci      mode |= SVGA3D_FILTER_ANISOTROPIC;
149bf215546Sopenharmony_ci   if (compare)
150bf215546Sopenharmony_ci      mode |= SVGA3D_FILTER_COMPARE;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   return mode;
153bf215546Sopenharmony_ci}
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci/**
157bf215546Sopenharmony_ci * Define a vgpu10 sampler state.
158bf215546Sopenharmony_ci */
159bf215546Sopenharmony_cistatic void
160bf215546Sopenharmony_cidefine_sampler_state_object(struct svga_context *svga,
161bf215546Sopenharmony_ci                            struct svga_sampler_state *ss,
162bf215546Sopenharmony_ci                            const struct pipe_sampler_state *ps)
163bf215546Sopenharmony_ci{
164bf215546Sopenharmony_ci   uint8_t max_aniso = (uint8_t) 255; /* XXX fix me */
165bf215546Sopenharmony_ci   boolean anisotropic;
166bf215546Sopenharmony_ci   uint8 compare_func;
167bf215546Sopenharmony_ci   SVGA3dFilter filter;
168bf215546Sopenharmony_ci   SVGA3dRGBAFloat bcolor;
169bf215546Sopenharmony_ci   float min_lod, max_lod;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   assert(svga_have_vgpu10(svga));
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   anisotropic = ss->aniso_level > 1.0f;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   filter = translate_filter_mode(ps->min_mip_filter,
176bf215546Sopenharmony_ci                                  ps->min_img_filter,
177bf215546Sopenharmony_ci                                  ps->mag_img_filter,
178bf215546Sopenharmony_ci                                  anisotropic,
179bf215546Sopenharmony_ci                                  ss->compare_mode);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   compare_func = translate_comparison_func(ss->compare_func);
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   COPY_4V(bcolor.value, ps->border_color.f);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   assert(ps->min_lod <= ps->max_lod);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   if (ps->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
188bf215546Sopenharmony_ci      /* just use the base level image */
189bf215546Sopenharmony_ci      min_lod = max_lod = 0.0f;
190bf215546Sopenharmony_ci   }
191bf215546Sopenharmony_ci   else {
192bf215546Sopenharmony_ci      min_lod = ps->min_lod;
193bf215546Sopenharmony_ci      max_lod = ps->max_lod;
194bf215546Sopenharmony_ci   }
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   /* If shadow comparisons are enabled, create two sampler states: one
197bf215546Sopenharmony_ci    * with the given shadow compare mode, another with shadow comparison off.
198bf215546Sopenharmony_ci    * We need the later because in some cases, we have to do the shadow
199bf215546Sopenharmony_ci    * compare in the shader.  So, we don't want to do it twice.
200bf215546Sopenharmony_ci    */
201bf215546Sopenharmony_ci   STATIC_ASSERT(PIPE_TEX_COMPARE_NONE == 0);
202bf215546Sopenharmony_ci   STATIC_ASSERT(PIPE_TEX_COMPARE_R_TO_TEXTURE == 1);
203bf215546Sopenharmony_ci   ss->id[1] = SVGA3D_INVALID_ID;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   unsigned i;
206bf215546Sopenharmony_ci   for (i = 0; i <= ss->compare_mode; i++) {
207bf215546Sopenharmony_ci      ss->id[i] = util_bitmask_add(svga->sampler_object_id_bm);
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci      SVGA_RETRY(svga, SVGA3D_vgpu10_DefineSamplerState
210bf215546Sopenharmony_ci                 (svga->swc,
211bf215546Sopenharmony_ci                  ss->id[i],
212bf215546Sopenharmony_ci                  filter,
213bf215546Sopenharmony_ci                  ss->addressu,
214bf215546Sopenharmony_ci                  ss->addressv,
215bf215546Sopenharmony_ci                  ss->addressw,
216bf215546Sopenharmony_ci                  ss->lod_bias, /* float */
217bf215546Sopenharmony_ci                  max_aniso,
218bf215546Sopenharmony_ci                  compare_func,
219bf215546Sopenharmony_ci                  bcolor,
220bf215546Sopenharmony_ci                  min_lod,       /* float */
221bf215546Sopenharmony_ci                  max_lod));      /* float */
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci      /* turn off the shadow compare option for second iteration */
224bf215546Sopenharmony_ci      filter &= ~SVGA3D_FILTER_COMPARE;
225bf215546Sopenharmony_ci   }
226bf215546Sopenharmony_ci}
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_cistatic void *
230bf215546Sopenharmony_cisvga_create_sampler_state(struct pipe_context *pipe,
231bf215546Sopenharmony_ci                          const struct pipe_sampler_state *sampler)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
234bf215546Sopenharmony_ci   struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci   if (!cso)
237bf215546Sopenharmony_ci      return NULL;
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);
240bf215546Sopenharmony_ci   cso->magfilter = translate_img_filter( sampler->mag_img_filter );
241bf215546Sopenharmony_ci   cso->minfilter = translate_img_filter( sampler->min_img_filter );
242bf215546Sopenharmony_ci   cso->aniso_level = MAX2( sampler->max_anisotropy, 1 );
243bf215546Sopenharmony_ci   if (sampler->max_anisotropy)
244bf215546Sopenharmony_ci      cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;
245bf215546Sopenharmony_ci   cso->lod_bias = sampler->lod_bias;
246bf215546Sopenharmony_ci   cso->addressu = translate_wrap_mode(sampler->wrap_s);
247bf215546Sopenharmony_ci   cso->addressv = translate_wrap_mode(sampler->wrap_t);
248bf215546Sopenharmony_ci   cso->addressw = translate_wrap_mode(sampler->wrap_r);
249bf215546Sopenharmony_ci   cso->normalized_coords = sampler->normalized_coords;
250bf215546Sopenharmony_ci   cso->compare_mode = sampler->compare_mode;
251bf215546Sopenharmony_ci   cso->compare_func = sampler->compare_func;
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci   {
254bf215546Sopenharmony_ci      uint32 r = float_to_ubyte(sampler->border_color.f[0]);
255bf215546Sopenharmony_ci      uint32 g = float_to_ubyte(sampler->border_color.f[1]);
256bf215546Sopenharmony_ci      uint32 b = float_to_ubyte(sampler->border_color.f[2]);
257bf215546Sopenharmony_ci      uint32 a = float_to_ubyte(sampler->border_color.f[3]);
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci      cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b;
260bf215546Sopenharmony_ci   }
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   /* No SVGA3D support for:
263bf215546Sopenharmony_ci    *    - min/max LOD clamping
264bf215546Sopenharmony_ci    */
265bf215546Sopenharmony_ci   cso->min_lod = 0;
266bf215546Sopenharmony_ci   cso->view_min_lod = MAX2((int) (sampler->min_lod + 0.5), 0);
267bf215546Sopenharmony_ci   cso->view_max_lod = MAX2((int) (sampler->max_lod + 0.5), 0);
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   /* Use min_mipmap */
270bf215546Sopenharmony_ci   if (svga->debug.use_min_mipmap) {
271bf215546Sopenharmony_ci      if (cso->view_min_lod == cso->view_max_lod) {
272bf215546Sopenharmony_ci         cso->min_lod = cso->view_min_lod;
273bf215546Sopenharmony_ci         cso->view_min_lod = 0;
274bf215546Sopenharmony_ci         cso->view_max_lod = 1000; /* Just a high number */
275bf215546Sopenharmony_ci         cso->mipfilter = SVGA3D_TEX_FILTER_NONE;
276bf215546Sopenharmony_ci      }
277bf215546Sopenharmony_ci   }
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   if (svga_have_vgpu10(svga)) {
280bf215546Sopenharmony_ci      define_sampler_state_object(svga, cso, sampler);
281bf215546Sopenharmony_ci   }
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   SVGA_DBG(DEBUG_SAMPLERS,
284bf215546Sopenharmony_ci            "New sampler: min %u, view(min %u, max %u) lod, mipfilter %s\n",
285bf215546Sopenharmony_ci            cso->min_lod, cso->view_min_lod, cso->view_max_lod,
286bf215546Sopenharmony_ci            cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci   svga->hud.num_sampler_objects++;
289bf215546Sopenharmony_ci   SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
290bf215546Sopenharmony_ci                        SVGA_STATS_COUNT_SAMPLER);
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   return cso;
293bf215546Sopenharmony_ci}
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_cistatic void
297bf215546Sopenharmony_cisvga_bind_sampler_states(struct pipe_context *pipe,
298bf215546Sopenharmony_ci                         enum pipe_shader_type shader,
299bf215546Sopenharmony_ci                         unsigned start,
300bf215546Sopenharmony_ci                         unsigned num,
301bf215546Sopenharmony_ci                         void **samplers)
302bf215546Sopenharmony_ci{
303bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
304bf215546Sopenharmony_ci   unsigned i;
305bf215546Sopenharmony_ci   boolean any_change = FALSE;
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci   assert(shader < PIPE_SHADER_TYPES);
308bf215546Sopenharmony_ci   assert(start + num <= PIPE_MAX_SAMPLERS);
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci   /* Pre-VGPU10 only supports FS textures */
311bf215546Sopenharmony_ci   if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)
312bf215546Sopenharmony_ci      return;
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   for (i = 0; i < num; i++) {
315bf215546Sopenharmony_ci      if (svga->curr.sampler[shader][start + i] != samplers[i])
316bf215546Sopenharmony_ci         any_change = TRUE;
317bf215546Sopenharmony_ci      svga->curr.sampler[shader][start + i] = samplers[i];
318bf215546Sopenharmony_ci   }
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   if (!any_change) {
321bf215546Sopenharmony_ci      return;
322bf215546Sopenharmony_ci   }
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   /* find highest non-null sampler[] entry */
325bf215546Sopenharmony_ci   {
326bf215546Sopenharmony_ci      unsigned j = MAX2(svga->curr.num_samplers[shader], start + num);
327bf215546Sopenharmony_ci      while (j > 0 && svga->curr.sampler[shader][j - 1] == NULL)
328bf215546Sopenharmony_ci         j--;
329bf215546Sopenharmony_ci      svga->curr.num_samplers[shader] = j;
330bf215546Sopenharmony_ci   }
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_SAMPLER;
333bf215546Sopenharmony_ci}
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_cistatic void
337bf215546Sopenharmony_cisvga_delete_sampler_state(struct pipe_context *pipe, void *sampler)
338bf215546Sopenharmony_ci{
339bf215546Sopenharmony_ci   struct svga_sampler_state *ss = (struct svga_sampler_state *) sampler;
340bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   if (svga_have_vgpu10(svga)) {
343bf215546Sopenharmony_ci      unsigned i;
344bf215546Sopenharmony_ci      for (i = 0; i < ARRAY_SIZE(ss->id); i++) {
345bf215546Sopenharmony_ci         if (ss->id[i] != SVGA3D_INVALID_ID) {
346bf215546Sopenharmony_ci            svga_hwtnl_flush_retry(svga);
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_ci            SVGA_RETRY(svga, SVGA3D_vgpu10_DestroySamplerState(svga->swc,
349bf215546Sopenharmony_ci                                                               ss->id[i]));
350bf215546Sopenharmony_ci            util_bitmask_clear(svga->sampler_object_id_bm, ss->id[i]);
351bf215546Sopenharmony_ci         }
352bf215546Sopenharmony_ci      }
353bf215546Sopenharmony_ci   }
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   FREE(sampler);
356bf215546Sopenharmony_ci   svga->hud.num_sampler_objects--;
357bf215546Sopenharmony_ci}
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_cistatic struct pipe_sampler_view *
361bf215546Sopenharmony_cisvga_create_sampler_view(struct pipe_context *pipe,
362bf215546Sopenharmony_ci                         struct pipe_resource *texture,
363bf215546Sopenharmony_ci                         const struct pipe_sampler_view *templ)
364bf215546Sopenharmony_ci{
365bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
366bf215546Sopenharmony_ci   struct svga_pipe_sampler_view *sv = CALLOC_STRUCT(svga_pipe_sampler_view);
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci   if (!sv) {
369bf215546Sopenharmony_ci      return NULL;
370bf215546Sopenharmony_ci   }
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci   sv->base = *templ;
373bf215546Sopenharmony_ci   sv->base.reference.count = 1;
374bf215546Sopenharmony_ci   sv->base.texture = NULL;
375bf215546Sopenharmony_ci   pipe_resource_reference(&sv->base.texture, texture);
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci   sv->base.context = pipe;
378bf215546Sopenharmony_ci   sv->id = SVGA3D_INVALID_ID;
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   svga->hud.num_samplerview_objects++;
381bf215546Sopenharmony_ci   SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
382bf215546Sopenharmony_ci                        SVGA_STATS_COUNT_SAMPLERVIEW);
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci   return &sv->base;
385bf215546Sopenharmony_ci}
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_cistatic void
389bf215546Sopenharmony_cisvga_sampler_view_destroy(struct pipe_context *pipe,
390bf215546Sopenharmony_ci                          struct pipe_sampler_view *view)
391bf215546Sopenharmony_ci{
392bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
393bf215546Sopenharmony_ci   struct svga_pipe_sampler_view *sv = svga_pipe_sampler_view(view);
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci   if (svga_have_vgpu10(svga) && sv->id != SVGA3D_INVALID_ID) {
396bf215546Sopenharmony_ci      assert(view->context == pipe);
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci      svga_hwtnl_flush_retry(svga);
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci      SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc,
401bf215546Sopenharmony_ci                                                               sv->id));
402bf215546Sopenharmony_ci      util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
403bf215546Sopenharmony_ci   }
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci   pipe_resource_reference(&sv->base.texture, NULL);
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci   FREE(sv);
408bf215546Sopenharmony_ci   svga->hud.num_samplerview_objects--;
409bf215546Sopenharmony_ci}
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_cistatic void
413bf215546Sopenharmony_cisvga_set_sampler_views(struct pipe_context *pipe,
414bf215546Sopenharmony_ci                       enum pipe_shader_type shader,
415bf215546Sopenharmony_ci                       unsigned start,
416bf215546Sopenharmony_ci                       unsigned num,
417bf215546Sopenharmony_ci                       unsigned unbind_num_trailing_slots,
418bf215546Sopenharmony_ci                       bool take_ownership,
419bf215546Sopenharmony_ci                       struct pipe_sampler_view **views)
420bf215546Sopenharmony_ci{
421bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
422bf215546Sopenharmony_ci   unsigned flag_1d = 0;
423bf215546Sopenharmony_ci   unsigned flag_srgb = 0;
424bf215546Sopenharmony_ci   uint i;
425bf215546Sopenharmony_ci   boolean any_change = FALSE;
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci   assert(shader < PIPE_SHADER_TYPES);
428bf215546Sopenharmony_ci   assert(start + num <= ARRAY_SIZE(svga->curr.sampler_views[shader]));
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci   /* Pre-VGPU10 only supports FS textures */
431bf215546Sopenharmony_ci   if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT) {
432bf215546Sopenharmony_ci      for (unsigned i = 0; i < num; i++) {
433bf215546Sopenharmony_ci         struct pipe_sampler_view *view = views[i];
434bf215546Sopenharmony_ci         pipe_sampler_view_reference(&view, NULL);
435bf215546Sopenharmony_ci      }
436bf215546Sopenharmony_ci      return;
437bf215546Sopenharmony_ci   }
438bf215546Sopenharmony_ci
439bf215546Sopenharmony_ci   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SETSAMPLERVIEWS);
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci   /* This bit of code works around a quirk in the CSO module.
442bf215546Sopenharmony_ci    * If start=num=0 it means all sampler views should be released.
443bf215546Sopenharmony_ci    * Note that the CSO module treats sampler views for fragment shaders
444bf215546Sopenharmony_ci    * differently than other shader types.
445bf215546Sopenharmony_ci    */
446bf215546Sopenharmony_ci   if (start == 0 && num == 0 && svga->curr.num_sampler_views[shader] > 0) {
447bf215546Sopenharmony_ci      for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
448bf215546Sopenharmony_ci         pipe_sampler_view_reference(&svga->curr.sampler_views[shader][i],
449bf215546Sopenharmony_ci                                     NULL);
450bf215546Sopenharmony_ci      }
451bf215546Sopenharmony_ci      any_change = TRUE;
452bf215546Sopenharmony_ci   }
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci   for (i = 0; i < num; i++) {
455bf215546Sopenharmony_ci      enum pipe_texture_target target;
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_ci      any_change |= svga->curr.sampler_views[shader][start + i] != views[i];
458bf215546Sopenharmony_ci
459bf215546Sopenharmony_ci      if (take_ownership) {
460bf215546Sopenharmony_ci         pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
461bf215546Sopenharmony_ci               NULL);
462bf215546Sopenharmony_ci         svga->curr.sampler_views[shader][start + i] = views[i];
463bf215546Sopenharmony_ci      } else if (svga->curr.sampler_views[shader][start + i] != views[i]) {
464bf215546Sopenharmony_ci         pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
465bf215546Sopenharmony_ci                                     views[i]);
466bf215546Sopenharmony_ci      }
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci      if (!views[i])
469bf215546Sopenharmony_ci         continue;
470bf215546Sopenharmony_ci
471bf215546Sopenharmony_ci      if (util_format_is_srgb(views[i]->format))
472bf215546Sopenharmony_ci         flag_srgb |= 1 << (start + i);
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci      target = views[i]->target;
475bf215546Sopenharmony_ci      if (target == PIPE_TEXTURE_1D) {
476bf215546Sopenharmony_ci         flag_1d |= 1 << (start + i);
477bf215546Sopenharmony_ci      } else if (target == PIPE_TEXTURE_RECT) {
478bf215546Sopenharmony_ci         /* If the size of the bound texture changes, we need to emit new
479bf215546Sopenharmony_ci          * const buffer values.
480bf215546Sopenharmony_ci          */
481bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;
482bf215546Sopenharmony_ci      } else if (target == PIPE_BUFFER) {
483bf215546Sopenharmony_ci         /* If the size of the bound buffer changes, we need to emit new
484bf215546Sopenharmony_ci          * const buffer values.
485bf215546Sopenharmony_ci          */
486bf215546Sopenharmony_ci         svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;
487bf215546Sopenharmony_ci      }
488bf215546Sopenharmony_ci   }
489bf215546Sopenharmony_ci
490bf215546Sopenharmony_ci   for (; i < num + unbind_num_trailing_slots; i++) {
491bf215546Sopenharmony_ci      if (svga->curr.sampler_views[shader][start + i]) {
492bf215546Sopenharmony_ci         pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
493bf215546Sopenharmony_ci                                     NULL);
494bf215546Sopenharmony_ci         any_change = TRUE;
495bf215546Sopenharmony_ci      }
496bf215546Sopenharmony_ci   }
497bf215546Sopenharmony_ci
498bf215546Sopenharmony_ci   if (!any_change) {
499bf215546Sopenharmony_ci      goto done;
500bf215546Sopenharmony_ci   }
501bf215546Sopenharmony_ci
502bf215546Sopenharmony_ci   /* find highest non-null sampler_views[] entry */
503bf215546Sopenharmony_ci   {
504bf215546Sopenharmony_ci      unsigned j = MAX2(svga->curr.num_sampler_views[shader], start + num);
505bf215546Sopenharmony_ci      while (j > 0 && svga->curr.sampler_views[shader][j - 1] == NULL)
506bf215546Sopenharmony_ci         j--;
507bf215546Sopenharmony_ci      svga->curr.num_sampler_views[shader] = j;
508bf215546Sopenharmony_ci   }
509bf215546Sopenharmony_ci
510bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
513bf215546Sopenharmony_ci       flag_1d != svga->curr.tex_flags.flag_1d) {
514bf215546Sopenharmony_ci      svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
515bf215546Sopenharmony_ci      svga->curr.tex_flags.flag_1d = flag_1d;
516bf215546Sopenharmony_ci      svga->curr.tex_flags.flag_srgb = flag_srgb;
517bf215546Sopenharmony_ci   }
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   /* Check if any of the sampler view resources collide with the framebuffer
520bf215546Sopenharmony_ci    * color buffers or depth stencil resource. If so, set the NEW_FRAME_BUFFER
521bf215546Sopenharmony_ci    * dirty bit so that emit_framebuffer can be invoked to create backed view
522bf215546Sopenharmony_ci    * for the conflicted surface view.
523bf215546Sopenharmony_ci    */
524bf215546Sopenharmony_ci   if (svga_check_sampler_framebuffer_resource_collision(svga, shader)) {
525bf215546Sopenharmony_ci      svga->dirty |= SVGA_NEW_FRAME_BUFFER;
526bf215546Sopenharmony_ci   }
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_cidone:
529bf215546Sopenharmony_ci   SVGA_STATS_TIME_POP(svga_sws(svga));
530bf215546Sopenharmony_ci}
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci/**
533bf215546Sopenharmony_ci * Clean up sampler, sampler view state at context destruction time
534bf215546Sopenharmony_ci */
535bf215546Sopenharmony_civoid
536bf215546Sopenharmony_cisvga_cleanup_sampler_state(struct svga_context *svga)
537bf215546Sopenharmony_ci{
538bf215546Sopenharmony_ci   enum pipe_shader_type shader;
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci   for (shader = 0; shader <= PIPE_SHADER_COMPUTE; shader++) {
541bf215546Sopenharmony_ci      unsigned i;
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci      for (i = 0; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {
544bf215546Sopenharmony_ci         pipe_sampler_view_reference(&svga->state.hw_draw.sampler_views[shader][i],
545bf215546Sopenharmony_ci                                     NULL);
546bf215546Sopenharmony_ci      }
547bf215546Sopenharmony_ci   }
548bf215546Sopenharmony_ci
549bf215546Sopenharmony_ci   /* free polygon stipple state */
550bf215546Sopenharmony_ci   if (svga->polygon_stipple.sampler) {
551bf215546Sopenharmony_ci      svga->pipe.delete_sampler_state(&svga->pipe, svga->polygon_stipple.sampler);
552bf215546Sopenharmony_ci   }
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci   if (svga->polygon_stipple.sampler_view) {
555bf215546Sopenharmony_ci      svga->pipe.sampler_view_destroy(&svga->pipe,
556bf215546Sopenharmony_ci                                      &svga->polygon_stipple.sampler_view->base);
557bf215546Sopenharmony_ci   }
558bf215546Sopenharmony_ci   pipe_resource_reference(&svga->polygon_stipple.texture, NULL);
559bf215546Sopenharmony_ci}
560bf215546Sopenharmony_ci
561bf215546Sopenharmony_civoid
562bf215546Sopenharmony_cisvga_init_sampler_functions( struct svga_context *svga )
563bf215546Sopenharmony_ci{
564bf215546Sopenharmony_ci   svga->pipe.create_sampler_state = svga_create_sampler_state;
565bf215546Sopenharmony_ci   svga->pipe.bind_sampler_states = svga_bind_sampler_states;
566bf215546Sopenharmony_ci   svga->pipe.delete_sampler_state = svga_delete_sampler_state;
567bf215546Sopenharmony_ci   svga->pipe.set_sampler_views = svga_set_sampler_views;
568bf215546Sopenharmony_ci   svga->pipe.create_sampler_view = svga_create_sampler_view;
569bf215546Sopenharmony_ci   svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;
570bf215546Sopenharmony_ci}
571