summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_wm_sampler_state.c
blob: d43968c85ac898ce90c6e9a18cad0e15fd3921ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
 develop this 3D driver.
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice (including the
 next paragraph) shall be included in all copies or substantial
 portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 **********************************************************************/
 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  */
                   
#include "util/u_math.h"

#include "brw_context.h"
#include "brw_state.h"
#include "brw_defines.h"
#include "brw_screen.h"


/* Samplers aren't strictly wm state from the hardware's perspective,
 * but that is the only situation in which we use them in this driver.
 */



static struct brw_winsys_buffer *
upload_default_color( struct brw_context *brw,
		      const GLfloat *color )
{
   struct brw_sampler_default_color sdc;

   COPY_4V(sdc.color, color); 
   
   return brw_cache_data( &brw->cache, BRW_SAMPLER_DEFAULT_COLOR, &sdc,
			  NULL, 0 );
}


struct wm_sampler_key {
   int sampler_count;
   struct brw_sampler_state sampler[BRW_MAX_TEX_UNIT];
};


/** Sets up the cache key for sampler state for all texture units */
static void
brw_wm_sampler_populate_key(struct brw_context *brw,
			    struct wm_sampler_key *key)
{
   int i;

   memset(key, 0, sizeof(*key));

   key->sampler_count = MIN2(brw->curr.num_textures,
			    brw->curr.num_samplers);

   for (i = 0; i < key->sampler_count; i++) {
      const struct brw_texture *tex = brw->curr.texture[i];
      const struct brw_sampler *sampler = brw->curr.sampler[i];
      struct brw_sampler_state *entry = &key->sampler[i];

      entry->ss0 = sampler->ss0;
      entry->ss1 = sampler->ss1;
      entry->ss2.default_color_pointer = brw->wm.sdc_bo[i]->offset[0] >> 5; /* reloc */
      entry->ss3 = sampler->ss3;

      /* Cube-maps on 965 and later must use the same wrap mode for all 3
       * coordinate dimensions.  Futher, only CUBE and CLAMP are valid.
       */
      if (tex->base.target == PIPE_TEXTURE_CUBE) {
	 if (FALSE &&
	     (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST || 
	      sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)) {
	    entry->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
	    entry->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
	    entry->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
	 } else {
	    entry->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
	    entry->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
	    entry->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
	 }
      } else if (tex->base.target == PIPE_TEXTURE_1D) {
	 /* There's a bug in 1D texture sampling - it actually pays
	  * attention to the wrap_t value, though it should not.
	  * Override the wrap_t value here to GL_REPEAT to keep
	  * any nonexistent border pixels from floating in.
	  */
	 entry->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
      }
   }
}


static void
brw_wm_sampler_update_default_colors(struct brw_context *brw)
{
   int nr = MIN2(brw->curr.num_textures,
		 brw->curr.num_samplers);
   int i;

   for (i = 0; i < nr; i++) {
      const struct brw_texture *tex = brw->curr.texture[i];
      const struct brw_sampler *sampler = brw->curr.sampler[i];

      brw->sws->bo_unreference(brw->wm.sdc_bo[i]);

      if (pf_is_depth_or_stencil(tex->base.format)) {
	 float bordercolor[4] = {
	    sampler->border_color[0],
	    sampler->border_color[0],
	    sampler->border_color[0],
	    sampler->border_color[0]
	 };
	 /* GL specs that border color for depth textures is taken from the
	  * R channel, while the hardware uses A.  Spam R into all the
	  * channels for safety.
	  */
	 brw->wm.sdc_bo[i] = upload_default_color(brw, bordercolor);
      } else {
	 brw->wm.sdc_bo[i] = upload_default_color(brw, sampler->border_color);
      }
   }
}



/* All samplers must be uploaded in a single contiguous array.  
 */
static int upload_wm_samplers( struct brw_context *brw )
{
   struct wm_sampler_key key;
   int i;

   brw_wm_sampler_update_default_colors(brw);
   brw_wm_sampler_populate_key(brw, &key);

   if (brw->wm.sampler_count != key.sampler_count) {
      brw->wm.sampler_count = key.sampler_count;
      brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
   }

   brw->sws->bo_unreference(brw->wm.sampler_bo);
   brw->wm.sampler_bo = NULL;
   if (brw->wm.sampler_count == 0)
      return 0;

   brw->wm.sampler_bo = brw_search_cache(&brw->cache, BRW_SAMPLER,
					 &key, sizeof(key),
					 brw->wm.sdc_bo, key.sampler_count,
					 NULL);

   /* If we didnt find it in the cache, compute the state and put it in the
    * cache.
    */
   if (brw->wm.sampler_bo == NULL) {
      brw->wm.sampler_bo = brw_upload_cache(&brw->cache, BRW_SAMPLER,
					    &key, sizeof(key),
					    brw->wm.sdc_bo, key.sampler_count,
					    &key.sampler, sizeof(key.sampler),
					    NULL, NULL);

      /* Emit SDC relocations */
      for (i = 0; i < key.sampler_count; i++) {
	 brw->sws->bo_emit_reloc(brw->wm.sampler_bo,
				 I915_GEM_DOMAIN_SAMPLER, 0,
				 0,
				 i * sizeof(struct brw_sampler_state) +
				 offsetof(struct brw_sampler_state, ss2),
				 brw->wm.sdc_bo[i]);
      }
   }

   return 0;
}

const struct brw_tracked_state brw_wm_samplers = {
   .dirty = {
      .mesa = PIPE_NEW_BOUND_TEXTURES | PIPE_NEW_SAMPLERS,
      .brw = 0,
      .cache = 0
   },
   .prepare = upload_wm_samplers,
};