2 * Copyright 2012, 2015 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wined3d_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
24 ULONG CDECL
wined3d_sampler_incref(struct wined3d_sampler
*sampler
)
26 unsigned int refcount
= InterlockedIncrement(&sampler
->refcount
);
28 TRACE("%p increasing refcount to %u.\n", sampler
, refcount
);
33 ULONG CDECL
wined3d_sampler_decref(struct wined3d_sampler
*sampler
)
35 unsigned int refcount
= wined3d_atomic_decrement_mutex_lock(&sampler
->refcount
);
37 TRACE("%p decreasing refcount to %u.\n", sampler
, refcount
);
41 sampler
->parent_ops
->wined3d_object_destroyed(sampler
->parent
);
42 sampler
->device
->adapter
->adapter_ops
->adapter_destroy_sampler(sampler
);
43 wined3d_mutex_unlock();
49 void * CDECL
wined3d_sampler_get_parent(const struct wined3d_sampler
*sampler
)
51 TRACE("sampler %p.\n", sampler
);
53 return sampler
->parent
;
56 static void wined3d_sampler_init(struct wined3d_sampler
*sampler
, struct wined3d_device
*device
,
57 const struct wined3d_sampler_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
59 TRACE("sampler %p, device %p, desc %p, parent %p, parent_ops %p.\n",
60 sampler
, device
, desc
, parent
, parent_ops
);
62 TRACE(" Address modes: U %#x, V %#x, W %#x.\n", desc
->address_u
, desc
->address_v
, desc
->address_w
);
63 TRACE(" Border colour: {%.8e, %.8e, %.8e, %.8e}.\n",
64 desc
->border_color
[0], desc
->border_color
[1], desc
->border_color
[2], desc
->border_color
[3]);
65 TRACE(" Filters: mag %#x, min %#x, mip %#x.\n", desc
->mag_filter
, desc
->min_filter
, desc
->mip_filter
);
66 TRACE(" LOD bias: %.8e.\n", desc
->lod_bias
);
67 TRACE(" Minimum LOD: %.8e.\n", desc
->min_lod
);
68 TRACE(" Maximum LOD: %.8e.\n", desc
->max_lod
);
69 TRACE(" Base mip level: %u.\n", desc
->mip_base_level
);
70 TRACE(" Maximum anisotropy: %u.\n", desc
->max_anisotropy
);
71 TRACE(" Comparison: %d.\n", desc
->compare
);
72 TRACE(" Comparison func: %#x.\n", desc
->comparison_func
);
73 TRACE(" SRGB decode: %d.\n", desc
->srgb_decode
);
75 sampler
->refcount
= 1;
76 sampler
->device
= device
;
77 sampler
->parent
= parent
;
78 sampler
->parent_ops
= parent_ops
;
79 sampler
->desc
= *desc
;
82 static void wined3d_sampler_gl_cs_init(void *object
)
84 struct wined3d_sampler_gl
*sampler_gl
= object
;
85 const struct wined3d_sampler_desc
*desc
;
86 const struct wined3d_gl_info
*gl_info
;
87 struct wined3d_context
*context
;
90 TRACE("sampler_gl %p.\n", sampler_gl
);
92 context
= context_acquire(sampler_gl
->s
.device
, NULL
, 0);
93 gl_info
= wined3d_context_gl(context
)->gl_info
;
95 desc
= &sampler_gl
->s
.desc
;
96 GL_EXTCALL(glGenSamplers(1, &name
));
97 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_WRAP_S
,
98 gl_info
->wrap_lookup
[desc
->address_u
- WINED3D_TADDRESS_WRAP
]));
99 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_WRAP_T
,
100 gl_info
->wrap_lookup
[desc
->address_v
- WINED3D_TADDRESS_WRAP
]));
101 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_WRAP_R
,
102 gl_info
->wrap_lookup
[desc
->address_w
- WINED3D_TADDRESS_WRAP
]));
103 GL_EXTCALL(glSamplerParameterfv(name
, GL_TEXTURE_BORDER_COLOR
, &desc
->border_color
[0]));
104 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_MAG_FILTER
,
105 wined3d_gl_mag_filter(desc
->mag_filter
)));
106 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_MIN_FILTER
,
107 wined3d_gl_min_mip_filter(desc
->min_filter
, desc
->mip_filter
)));
108 GL_EXTCALL(glSamplerParameterf(name
, GL_TEXTURE_LOD_BIAS
, desc
->lod_bias
));
109 GL_EXTCALL(glSamplerParameterf(name
, GL_TEXTURE_MIN_LOD
, desc
->min_lod
));
110 GL_EXTCALL(glSamplerParameterf(name
, GL_TEXTURE_MAX_LOD
, desc
->max_lod
));
111 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
112 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_MAX_ANISOTROPY
, desc
->max_anisotropy
));
114 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_COMPARE_MODE
, GL_COMPARE_R_TO_TEXTURE
));
115 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_COMPARE_FUNC
,
116 wined3d_gl_compare_func(desc
->comparison_func
)));
117 if ((context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
118 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
] && !desc
->srgb_decode
)
119 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
));
120 checkGLcall("sampler creation");
122 TRACE("Created sampler %u.\n", name
);
123 sampler_gl
->name
= name
;
125 context_release(context
);
128 void wined3d_sampler_gl_init(struct wined3d_sampler_gl
*sampler_gl
, struct wined3d_device
*device
,
129 const struct wined3d_sampler_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
131 TRACE("sampler_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
132 sampler_gl
, device
, desc
, parent
, parent_ops
);
134 wined3d_sampler_init(&sampler_gl
->s
, device
, desc
, parent
, parent_ops
);
136 if (device
->adapter
->gl_info
.supported
[ARB_SAMPLER_OBJECTS
])
137 wined3d_cs_init_object(device
->cs
, wined3d_sampler_gl_cs_init
, sampler_gl
);
140 static VkFilter
vk_filter_from_wined3d(enum wined3d_texture_filter_type f
)
145 ERR("Invalid filter type %#x.\n", f
);
146 case WINED3D_TEXF_POINT
:
147 return VK_FILTER_NEAREST
;
148 case WINED3D_TEXF_LINEAR
:
149 return VK_FILTER_LINEAR
;
153 static VkSamplerMipmapMode
vk_mipmap_mode_from_wined3d(enum wined3d_texture_filter_type f
)
158 ERR("Invalid filter type %#x.\n", f
);
159 case WINED3D_TEXF_NONE
:
160 case WINED3D_TEXF_POINT
:
161 return VK_SAMPLER_MIPMAP_MODE_NEAREST
;
162 case WINED3D_TEXF_LINEAR
:
163 return VK_SAMPLER_MIPMAP_MODE_LINEAR
;
167 static VkSamplerAddressMode
vk_address_mode_from_wined3d(enum wined3d_texture_address a
)
172 ERR("Invalid address mode %#x.\n", a
);
173 case WINED3D_TADDRESS_WRAP
:
174 return VK_SAMPLER_ADDRESS_MODE_REPEAT
;
175 case WINED3D_TADDRESS_MIRROR
:
176 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
;
177 case WINED3D_TADDRESS_CLAMP
:
178 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
;
179 case WINED3D_TADDRESS_BORDER
:
180 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
;
181 case WINED3D_TADDRESS_MIRROR_ONCE
:
182 return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
;
186 static VkBorderColor
vk_border_colour_from_wined3d(const struct wined3d_color
*colour
)
192 struct wined3d_color wined3d_colour
;
193 VkBorderColor vk_colour
;
197 {{0.0f
, 0.0f
, 0.0f
, 0.0f
}, VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
},
198 {{0.0f
, 0.0f
, 0.0f
, 1.0f
}, VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
},
199 {{1.0f
, 1.0f
, 1.0f
, 1.0f
}, VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
},
202 for (i
= 0; i
< ARRAY_SIZE(colours
); ++i
)
204 if (!memcmp(&colours
[i
], colour
, sizeof(*colour
)))
205 return colours
[i
].vk_colour
;
208 FIXME("Unhandled border colour {%.8e, %.8e, %.8e, %.8e}.\n", colour
->r
, colour
->g
, colour
->b
, colour
->a
);
210 return VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
;
213 static void wined3d_sampler_vk_cs_init(void *object
)
215 struct wined3d_sampler_vk
*sampler_vk
= object
;
216 const struct wined3d_sampler_desc
*desc
;
217 const struct wined3d_d3d_info
*d3d_info
;
218 struct VkSamplerCreateInfo sampler_desc
;
219 const struct wined3d_vk_info
*vk_info
;
220 struct wined3d_context_vk
*context_vk
;
221 struct wined3d_device_vk
*device_vk
;
222 VkSampler vk_sampler
;
225 TRACE("sampler_vk %p.\n", sampler_vk
);
227 context_vk
= wined3d_context_vk(context_acquire(sampler_vk
->s
.device
, NULL
, 0));
228 device_vk
= wined3d_device_vk(context_vk
->c
.device
);
229 d3d_info
= context_vk
->c
.d3d_info
;
230 vk_info
= context_vk
->vk_info
;
232 desc
= &sampler_vk
->s
.desc
;
233 sampler_desc
.sType
= VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
;
234 sampler_desc
.pNext
= NULL
;
235 sampler_desc
.flags
= 0;
236 sampler_desc
.magFilter
= vk_filter_from_wined3d(desc
->mag_filter
);
237 sampler_desc
.minFilter
= vk_filter_from_wined3d(desc
->min_filter
);
238 sampler_desc
.mipmapMode
= vk_mipmap_mode_from_wined3d(desc
->mip_filter
);
239 sampler_desc
.addressModeU
= vk_address_mode_from_wined3d(desc
->address_u
);
240 sampler_desc
.addressModeV
= vk_address_mode_from_wined3d(desc
->address_v
);
241 sampler_desc
.addressModeW
= vk_address_mode_from_wined3d(desc
->address_w
);
242 sampler_desc
.mipLodBias
= desc
->lod_bias
;
243 sampler_desc
.anisotropyEnable
= desc
->max_anisotropy
!= 1;
244 sampler_desc
.maxAnisotropy
= desc
->max_anisotropy
;
245 sampler_desc
.compareEnable
= !!desc
->compare
;
246 sampler_desc
.compareOp
= vk_compare_op_from_wined3d(desc
->comparison_func
);
247 sampler_desc
.minLod
= desc
->min_lod
;
248 sampler_desc
.maxLod
= desc
->max_lod
;
249 sampler_desc
.borderColor
= VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
;
250 sampler_desc
.unnormalizedCoordinates
= VK_FALSE
;
252 if (desc
->address_u
== WINED3D_TADDRESS_BORDER
|| desc
->address_v
== WINED3D_TADDRESS_BORDER
253 || desc
->address_w
== WINED3D_TADDRESS_BORDER
)
254 sampler_desc
.borderColor
= vk_border_colour_from_wined3d((const struct wined3d_color
*)desc
->border_color
);
255 if (desc
->mip_base_level
)
256 FIXME("Unhandled mip_base_level %u.\n", desc
->mip_base_level
);
257 if ((d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
) && !desc
->srgb_decode
)
258 FIXME("Unhandled srgb_decode %#x.\n", desc
->srgb_decode
);
260 vr
= VK_CALL(vkCreateSampler(device_vk
->vk_device
, &sampler_desc
, NULL
, &vk_sampler
));
261 context_release(&context_vk
->c
);
264 ERR("Failed to create Vulkan sampler, vr %s.\n", wined3d_debug_vkresult(vr
));
268 TRACE("Created sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler
));
270 sampler_vk
->vk_image_info
.sampler
= vk_sampler
;
271 sampler_vk
->vk_image_info
.imageView
= VK_NULL_HANDLE
;
272 sampler_vk
->vk_image_info
.imageLayout
= VK_IMAGE_LAYOUT_UNDEFINED
;
275 void wined3d_sampler_vk_init(struct wined3d_sampler_vk
*sampler_vk
, struct wined3d_device
*device
,
276 const struct wined3d_sampler_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
278 TRACE("sampler_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
279 sampler_vk
, device
, desc
, parent
, parent_ops
);
281 wined3d_sampler_init(&sampler_vk
->s
, device
, desc
, parent
, parent_ops
);
282 wined3d_cs_init_object(device
->cs
, wined3d_sampler_vk_cs_init
, sampler_vk
);
285 HRESULT CDECL
wined3d_sampler_create(struct wined3d_device
*device
, const struct wined3d_sampler_desc
*desc
,
286 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_sampler
**sampler
)
288 TRACE("device %p, desc %p, parent %p, parent_ops %p, sampler %p.\n",
289 device
, desc
, parent
, parent_ops
, sampler
);
291 if (desc
->address_u
< WINED3D_TADDRESS_WRAP
|| desc
->address_u
> WINED3D_TADDRESS_MIRROR_ONCE
292 || desc
->address_v
< WINED3D_TADDRESS_WRAP
|| desc
->address_v
> WINED3D_TADDRESS_MIRROR_ONCE
293 || desc
->address_w
< WINED3D_TADDRESS_WRAP
|| desc
->address_w
> WINED3D_TADDRESS_MIRROR_ONCE
)
294 return WINED3DERR_INVALIDCALL
;
296 if (desc
->mag_filter
< WINED3D_TEXF_POINT
|| desc
->mag_filter
> WINED3D_TEXF_LINEAR
297 || desc
->min_filter
< WINED3D_TEXF_POINT
|| desc
->min_filter
> WINED3D_TEXF_LINEAR
298 || desc
->mip_filter
> WINED3D_TEXF_LINEAR
)
299 return WINED3DERR_INVALIDCALL
;
301 return device
->adapter
->adapter_ops
->adapter_create_sampler(device
, desc
, parent
, parent_ops
, sampler
);
304 static void texture_gl_apply_base_level(struct wined3d_texture_gl
*texture_gl
,
305 const struct wined3d_sampler_desc
*desc
, const struct wined3d_gl_info
*gl_info
)
307 struct gl_texture
*gl_tex
;
308 unsigned int base_level
;
310 if (texture_gl
->t
.flags
& WINED3D_TEXTURE_COND_NP2
)
312 else if (desc
->mip_filter
== WINED3D_TEXF_NONE
)
313 base_level
= texture_gl
->t
.lod
;
315 base_level
= min(max(desc
->mip_base_level
, texture_gl
->t
.lod
), texture_gl
->t
.level_count
- 1);
317 gl_tex
= wined3d_texture_gl_get_gl_texture(texture_gl
, texture_gl
->t
.flags
& WINED3D_TEXTURE_IS_SRGB
);
318 if (base_level
!= gl_tex
->base_level
)
320 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
321 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
322 * mipmap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
323 * corresponds to GL_TEXTURE_BASE_LEVEL. */
324 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
, GL_TEXTURE_BASE_LEVEL
, base_level
);
325 gl_tex
->base_level
= base_level
;
329 /* This function relies on the correct texture being bound and loaded. */
330 void wined3d_sampler_gl_bind(struct wined3d_sampler_gl
*sampler_gl
, unsigned int unit
,
331 struct wined3d_texture_gl
*texture_gl
, const struct wined3d_context_gl
*context_gl
)
333 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
335 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
337 GL_EXTCALL(glBindSampler(unit
, sampler_gl
->name
));
338 checkGLcall("bind sampler");
342 wined3d_texture_gl_apply_sampler_desc(texture_gl
, &sampler_gl
->s
.desc
, context_gl
);
346 ERR("Could not apply sampler state.\n");
350 texture_gl_apply_base_level(texture_gl
, &sampler_gl
->s
.desc
, gl_info
);