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
21 #include "wine/port.h"
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
27 ULONG CDECL
wined3d_sampler_incref(struct wined3d_sampler
*sampler
)
29 ULONG refcount
= InterlockedIncrement(&sampler
->refcount
);
31 TRACE("%p increasing refcount to %u.\n", sampler
, refcount
);
36 ULONG CDECL
wined3d_sampler_decref(struct wined3d_sampler
*sampler
)
38 ULONG refcount
= InterlockedDecrement(&sampler
->refcount
);
40 TRACE("%p decreasing refcount to %u.\n", sampler
, refcount
);
44 sampler
->parent_ops
->wined3d_object_destroyed(sampler
->parent
);
45 sampler
->device
->adapter
->adapter_ops
->adapter_destroy_sampler(sampler
);
51 void * CDECL
wined3d_sampler_get_parent(const struct wined3d_sampler
*sampler
)
53 TRACE("sampler %p.\n", sampler
);
55 return sampler
->parent
;
58 static void wined3d_sampler_init(struct wined3d_sampler
*sampler
, struct wined3d_device
*device
,
59 const struct wined3d_sampler_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
61 TRACE("sampler %p, device %p, desc %p, parent %p, parent_ops %p.\n",
62 sampler
, device
, desc
, parent
, parent_ops
);
64 sampler
->refcount
= 1;
65 sampler
->device
= device
;
66 sampler
->parent
= parent
;
67 sampler
->parent_ops
= parent_ops
;
68 sampler
->desc
= *desc
;
71 static void wined3d_sampler_gl_cs_init(void *object
)
73 struct wined3d_sampler_gl
*sampler_gl
= object
;
74 const struct wined3d_sampler_desc
*desc
;
75 const struct wined3d_gl_info
*gl_info
;
76 struct wined3d_context
*context
;
79 TRACE("sampler_gl %p.\n", sampler_gl
);
81 context
= context_acquire(sampler_gl
->s
.device
, NULL
, 0);
82 gl_info
= wined3d_context_gl(context
)->gl_info
;
84 desc
= &sampler_gl
->s
.desc
;
85 GL_EXTCALL(glGenSamplers(1, &name
));
86 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_WRAP_S
,
87 gl_info
->wrap_lookup
[desc
->address_u
- WINED3D_TADDRESS_WRAP
]));
88 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_WRAP_T
,
89 gl_info
->wrap_lookup
[desc
->address_v
- WINED3D_TADDRESS_WRAP
]));
90 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_WRAP_R
,
91 gl_info
->wrap_lookup
[desc
->address_w
- WINED3D_TADDRESS_WRAP
]));
92 GL_EXTCALL(glSamplerParameterfv(name
, GL_TEXTURE_BORDER_COLOR
, &desc
->border_color
[0]));
93 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_MAG_FILTER
,
94 wined3d_gl_mag_filter(desc
->mag_filter
)));
95 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_MIN_FILTER
,
96 wined3d_gl_min_mip_filter(desc
->min_filter
, desc
->mip_filter
)));
97 GL_EXTCALL(glSamplerParameterf(name
, GL_TEXTURE_LOD_BIAS
, desc
->lod_bias
));
98 GL_EXTCALL(glSamplerParameterf(name
, GL_TEXTURE_MIN_LOD
, desc
->min_lod
));
99 GL_EXTCALL(glSamplerParameterf(name
, GL_TEXTURE_MAX_LOD
, desc
->max_lod
));
100 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
101 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_MAX_ANISOTROPY
, desc
->max_anisotropy
));
103 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_COMPARE_MODE
, GL_COMPARE_R_TO_TEXTURE
));
104 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_COMPARE_FUNC
,
105 wined3d_gl_compare_func(desc
->comparison_func
)));
106 if ((context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
107 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
] && !desc
->srgb_decode
)
108 GL_EXTCALL(glSamplerParameteri(name
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
));
109 checkGLcall("sampler creation");
111 TRACE("Created sampler %u.\n", name
);
112 sampler_gl
->name
= name
;
114 context_release(context
);
117 void wined3d_sampler_gl_init(struct wined3d_sampler_gl
*sampler_gl
, struct wined3d_device
*device
,
118 const struct wined3d_sampler_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
120 TRACE("sampler_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
121 sampler_gl
, device
, desc
, parent
, parent_ops
);
123 wined3d_sampler_init(&sampler_gl
->s
, device
, desc
, parent
, parent_ops
);
125 if (device
->adapter
->gl_info
.supported
[ARB_SAMPLER_OBJECTS
])
126 wined3d_cs_init_object(device
->cs
, wined3d_sampler_gl_cs_init
, sampler_gl
);
129 static VkFilter
vk_filter_from_wined3d(enum wined3d_texture_filter_type f
)
134 ERR("Invalid filter type %#x.\n", f
);
135 case WINED3D_TEXF_POINT
:
136 return VK_FILTER_NEAREST
;
137 case WINED3D_TEXF_LINEAR
:
138 return VK_FILTER_LINEAR
;
142 static VkSamplerMipmapMode
vk_mipmap_mode_from_wined3d(enum wined3d_texture_filter_type f
)
147 ERR("Invalid filter type %#x.\n", f
);
148 case WINED3D_TEXF_NONE
:
149 case WINED3D_TEXF_POINT
:
150 return VK_SAMPLER_MIPMAP_MODE_NEAREST
;
151 case WINED3D_TEXF_LINEAR
:
152 return VK_SAMPLER_MIPMAP_MODE_LINEAR
;
156 static VkSamplerAddressMode
vk_address_mode_from_wined3d(enum wined3d_texture_address a
)
161 ERR("Invalid address mode %#x.\n", a
);
162 case WINED3D_TADDRESS_WRAP
:
163 return VK_SAMPLER_ADDRESS_MODE_REPEAT
;
164 case WINED3D_TADDRESS_MIRROR
:
165 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
;
166 case WINED3D_TADDRESS_CLAMP
:
167 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
;
168 case WINED3D_TADDRESS_BORDER
:
169 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
;
170 case WINED3D_TADDRESS_MIRROR_ONCE
:
171 return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
;
175 static VkBorderColor
vk_border_colour_from_wined3d(const struct wined3d_color
*colour
)
181 struct wined3d_color wined3d_colour
;
182 VkBorderColor vk_colour
;
186 {{0.0f
, 0.0f
, 0.0f
, 0.0f
}, VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
},
187 {{0.0f
, 0.0f
, 0.0f
, 1.0f
}, VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
},
188 {{1.0f
, 1.0f
, 1.0f
, 1.0f
}, VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
},
191 for (i
= 0; i
< ARRAY_SIZE(colours
); ++i
)
193 if (!memcmp(&colours
[i
], colour
, sizeof(*colour
)))
194 return colours
[i
].vk_colour
;
197 FIXME("Unhandled border colour {%.8e, %.8e, %.8e, %.8e}.\n", colour
->r
, colour
->g
, colour
->b
, colour
->a
);
199 return VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
;
202 static void wined3d_sampler_vk_cs_init(void *object
)
204 struct wined3d_sampler_vk
*sampler_vk
= object
;
205 const struct wined3d_sampler_desc
*desc
;
206 const struct wined3d_d3d_info
*d3d_info
;
207 struct VkSamplerCreateInfo sampler_desc
;
208 const struct wined3d_vk_info
*vk_info
;
209 struct wined3d_context_vk
*context_vk
;
210 struct wined3d_device_vk
*device_vk
;
211 VkSampler vk_sampler
;
214 TRACE("sampler_vk %p.\n", sampler_vk
);
216 context_vk
= wined3d_context_vk(context_acquire(sampler_vk
->s
.device
, NULL
, 0));
217 device_vk
= wined3d_device_vk(context_vk
->c
.device
);
218 d3d_info
= context_vk
->c
.d3d_info
;
219 vk_info
= context_vk
->vk_info
;
221 desc
= &sampler_vk
->s
.desc
;
222 sampler_desc
.sType
= VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
;
223 sampler_desc
.pNext
= NULL
;
224 sampler_desc
.flags
= 0;
225 sampler_desc
.magFilter
= vk_filter_from_wined3d(desc
->mag_filter
);
226 sampler_desc
.minFilter
= vk_filter_from_wined3d(desc
->min_filter
);
227 sampler_desc
.mipmapMode
= vk_mipmap_mode_from_wined3d(desc
->mip_filter
);
228 sampler_desc
.addressModeU
= vk_address_mode_from_wined3d(desc
->address_u
);
229 sampler_desc
.addressModeV
= vk_address_mode_from_wined3d(desc
->address_v
);
230 sampler_desc
.addressModeW
= vk_address_mode_from_wined3d(desc
->address_w
);
231 sampler_desc
.mipLodBias
= desc
->lod_bias
;
232 sampler_desc
.anisotropyEnable
= desc
->max_anisotropy
!= 1;
233 sampler_desc
.maxAnisotropy
= desc
->max_anisotropy
;
234 sampler_desc
.compareEnable
= !!desc
->compare
;
235 sampler_desc
.compareOp
= vk_compare_op_from_wined3d(desc
->comparison_func
);
236 sampler_desc
.minLod
= desc
->min_lod
;
237 sampler_desc
.maxLod
= desc
->max_lod
;
238 sampler_desc
.borderColor
= VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
;
239 sampler_desc
.unnormalizedCoordinates
= VK_FALSE
;
241 if (desc
->address_u
== WINED3D_TADDRESS_BORDER
|| desc
->address_v
== WINED3D_TADDRESS_BORDER
242 || desc
->address_w
== WINED3D_TADDRESS_BORDER
)
243 sampler_desc
.borderColor
= vk_border_colour_from_wined3d((const struct wined3d_color
*)desc
->border_color
);
244 if (desc
->mip_base_level
)
245 FIXME("Unhandled mip_base_level %u.\n", desc
->mip_base_level
);
246 if ((d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
) && !desc
->srgb_decode
)
247 FIXME("Unhandled srgb_decode %#x.\n", desc
->srgb_decode
);
249 vr
= VK_CALL(vkCreateSampler(device_vk
->vk_device
, &sampler_desc
, NULL
, &vk_sampler
));
250 context_release(&context_vk
->c
);
253 ERR("Failed to create Vulkan sampler, vr %s.\n", wined3d_debug_vkresult(vr
));
257 TRACE("Created sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler
));
259 sampler_vk
->vk_image_info
.sampler
= vk_sampler
;
260 sampler_vk
->vk_image_info
.imageView
= VK_NULL_HANDLE
;
261 sampler_vk
->vk_image_info
.imageLayout
= VK_IMAGE_LAYOUT_UNDEFINED
;
264 void wined3d_sampler_vk_init(struct wined3d_sampler_vk
*sampler_vk
, struct wined3d_device
*device
,
265 const struct wined3d_sampler_desc
*desc
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
267 TRACE("sampler_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
268 sampler_vk
, device
, desc
, parent
, parent_ops
);
270 wined3d_sampler_init(&sampler_vk
->s
, device
, desc
, parent
, parent_ops
);
271 wined3d_cs_init_object(device
->cs
, wined3d_sampler_vk_cs_init
, sampler_vk
);
274 HRESULT CDECL
wined3d_sampler_create(struct wined3d_device
*device
, const struct wined3d_sampler_desc
*desc
,
275 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_sampler
**sampler
)
277 TRACE("device %p, desc %p, parent %p, parent_ops %p, sampler %p.\n",
278 device
, desc
, parent
, parent_ops
, sampler
);
280 if (desc
->address_u
< WINED3D_TADDRESS_WRAP
|| desc
->address_u
> WINED3D_TADDRESS_MIRROR_ONCE
281 || desc
->address_v
< WINED3D_TADDRESS_WRAP
|| desc
->address_v
> WINED3D_TADDRESS_MIRROR_ONCE
282 || desc
->address_w
< WINED3D_TADDRESS_WRAP
|| desc
->address_w
> WINED3D_TADDRESS_MIRROR_ONCE
)
283 return WINED3DERR_INVALIDCALL
;
285 if (desc
->mag_filter
< WINED3D_TEXF_POINT
|| desc
->mag_filter
> WINED3D_TEXF_LINEAR
286 || desc
->min_filter
< WINED3D_TEXF_POINT
|| desc
->min_filter
> WINED3D_TEXF_LINEAR
287 || desc
->mip_filter
> WINED3D_TEXF_LINEAR
)
288 return WINED3DERR_INVALIDCALL
;
290 return device
->adapter
->adapter_ops
->adapter_create_sampler(device
, desc
, parent
, parent_ops
, sampler
);
293 static void texture_gl_apply_base_level(struct wined3d_texture_gl
*texture_gl
,
294 const struct wined3d_sampler_desc
*desc
, const struct wined3d_gl_info
*gl_info
)
296 struct gl_texture
*gl_tex
;
297 unsigned int base_level
;
299 if (texture_gl
->t
.flags
& WINED3D_TEXTURE_COND_NP2
)
301 else if (desc
->mip_filter
== WINED3D_TEXF_NONE
)
302 base_level
= texture_gl
->t
.lod
;
304 base_level
= min(max(desc
->mip_base_level
, texture_gl
->t
.lod
), texture_gl
->t
.level_count
- 1);
306 gl_tex
= wined3d_texture_gl_get_gl_texture(texture_gl
, texture_gl
->t
.flags
& WINED3D_TEXTURE_IS_SRGB
);
307 if (base_level
!= gl_tex
->base_level
)
309 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
310 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
311 * mipmap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
312 * corresponds to GL_TEXTURE_BASE_LEVEL. */
313 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
, GL_TEXTURE_BASE_LEVEL
, base_level
);
314 gl_tex
->base_level
= base_level
;
318 /* This function relies on the correct texture being bound and loaded. */
319 void wined3d_sampler_gl_bind(struct wined3d_sampler_gl
*sampler_gl
, unsigned int unit
,
320 struct wined3d_texture_gl
*texture_gl
, const struct wined3d_context_gl
*context_gl
)
322 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
324 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
326 GL_EXTCALL(glBindSampler(unit
, sampler_gl
->name
));
327 checkGLcall("bind sampler");
331 wined3d_texture_gl_apply_sampler_desc(texture_gl
, &sampler_gl
->s
.desc
, context_gl
);
335 ERR("Could not apply sampler state.\n");
339 texture_gl_apply_base_level(texture_gl
, &sampler_gl
->s
.desc
, gl_info
);