1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "main/macros.h"
36 #include "program/prog_instruction.h"
38 #include "st_context.h"
40 #include "st_texture.h"
41 #include "st_format.h"
42 #include "st_cb_texture.h"
43 #include "pipe/p_context.h"
44 #include "util/u_inlines.h"
45 #include "cso_cache/cso_context.h"
47 static boolean
check_sampler_swizzle(struct pipe_sampler_view
*sv
,
50 if ((sv
->swizzle_r
!= GET_SWZ(_swizzle
, 0)) ||
51 (sv
->swizzle_g
!= GET_SWZ(_swizzle
, 1)) ||
52 (sv
->swizzle_b
!= GET_SWZ(_swizzle
, 2)) ||
53 (sv
->swizzle_a
!= GET_SWZ(_swizzle
, 3)))
58 static INLINE
struct pipe_sampler_view
*
59 st_create_texture_sampler_view_from_stobj(struct pipe_context
*pipe
,
60 struct st_texture_object
*stObj
,
61 enum pipe_format format
)
64 struct pipe_sampler_view templ
;
66 u_sampler_view_default_template(&templ
,
70 if (stObj
->base
._Swizzle
!= SWIZZLE_NOOP
) {
71 templ
.swizzle_r
= GET_SWZ(stObj
->base
._Swizzle
, 0);
72 templ
.swizzle_g
= GET_SWZ(stObj
->base
._Swizzle
, 1);
73 templ
.swizzle_b
= GET_SWZ(stObj
->base
._Swizzle
, 2);
74 templ
.swizzle_a
= GET_SWZ(stObj
->base
._Swizzle
, 3);
77 return pipe
->create_sampler_view(pipe
, stObj
->pt
, &templ
);
81 static INLINE
struct pipe_sampler_view
*
82 st_get_texture_sampler_view_from_stobj(struct st_texture_object
*stObj
,
83 struct pipe_context
*pipe
,
84 enum pipe_format format
)
87 if (!stObj
|| !stObj
->pt
) {
91 if (!stObj
->sampler_view
) {
92 stObj
->sampler_view
= st_create_texture_sampler_view_from_stobj(pipe
, stObj
, format
);
95 return stObj
->sampler_view
;
99 update_textures(struct st_context
*st
)
101 struct pipe_context
*pipe
= st
->pipe
;
102 struct gl_vertex_program
*vprog
= st
->ctx
->VertexProgram
._Current
;
103 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
104 const GLbitfield samplersUsed
= (vprog
->Base
.SamplersUsed
|
105 fprog
->Base
.SamplersUsed
);
108 st
->state
.num_textures
= 0;
110 /* loop over sampler units (aka tex image units) */
111 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureImageUnits
; su
++) {
112 struct pipe_sampler_view
*sampler_view
= NULL
;
113 enum pipe_format st_view_format
;
114 if (samplersUsed
& (1 << su
)) {
115 struct gl_texture_object
*texObj
;
116 struct st_texture_object
*stObj
;
120 if (fprog
->Base
.SamplersUsed
& (1 << su
))
121 texUnit
= fprog
->Base
.SamplerUnits
[su
];
123 texUnit
= vprog
->Base
.SamplerUnits
[su
];
125 texObj
= st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
128 texObj
= st_get_default_texture(st
);
130 stObj
= st_texture_object(texObj
);
132 retval
= st_finalize_texture(st
->ctx
, st
->pipe
, texObj
);
138 st_view_format
= stObj
->pt
->format
;
140 struct st_texture_image
*firstImage
;
141 enum pipe_format firstImageFormat
;
142 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
144 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
145 if (firstImageFormat
!= stObj
->pt
->format
)
146 st_view_format
= firstImageFormat
;
149 st
->state
.num_textures
= su
+ 1;
151 /* if sampler view has changed dereference it */
152 if (stObj
->sampler_view
)
153 if (check_sampler_swizzle(stObj
->sampler_view
, stObj
->base
._Swizzle
) || (st_view_format
!= stObj
->sampler_view
->format
))
154 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
156 sampler_view
= st_get_texture_sampler_view_from_stobj(stObj
, pipe
, st_view_format
);
158 pipe_sampler_view_reference(&st
->state
.sampler_views
[su
], sampler_view
);
161 cso_set_fragment_sampler_views(st
->cso_context
,
162 st
->state
.num_textures
,
163 st
->state
.sampler_views
);
164 if (st
->ctx
->Const
.MaxVertexTextureImageUnits
> 0) {
165 cso_set_vertex_sampler_views(st
->cso_context
,
166 MIN2(st
->state
.num_textures
,
167 st
->ctx
->Const
.MaxVertexTextureImageUnits
),
168 st
->state
.sampler_views
);
173 const struct st_tracked_state st_update_texture
= {
174 "st_update_texture", /* name */
176 _NEW_TEXTURE
, /* mesa */
177 ST_NEW_FRAGMENT_PROGRAM
, /* st */
179 update_textures
/* update */
186 finalize_textures(struct st_context
*st
)
188 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
189 const GLboolean prev_missing_textures
= st
->missing_textures
;
192 st
->missing_textures
= GL_FALSE
;
194 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureCoordUnits
; su
++) {
195 if (fprog
->Base
.SamplersUsed
& (1 << su
)) {
196 const GLuint texUnit
= fprog
->Base
.SamplerUnits
[su
];
197 struct gl_texture_object
*texObj
198 = st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
203 retval
= st_finalize_texture(st
->ctx
, st
->pipe
, texObj
);
206 st
->missing_textures
= GL_TRUE
;
213 if (prev_missing_textures
!= st
->missing_textures
)
214 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
219 const struct st_tracked_state st_finalize_textures
= {
220 "st_finalize_textures", /* name */
222 _NEW_TEXTURE
, /* mesa */
225 finalize_textures
/* update */