winemac.drv: Remove workarounds for Mac OS X 10.6 and earlier.
[wine.git] / dlls / wined3d / sampler.c
blobbba3165316ebc57e2cea397f1636b52529da9db1
1 /*
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 "config.h"
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);
33 return 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);
42 if (!refcount)
44 wined3d_mutex_lock();
45 sampler->parent_ops->wined3d_object_destroyed(sampler->parent);
46 sampler->device->adapter->adapter_ops->adapter_destroy_sampler(sampler);
47 wined3d_mutex_unlock();
50 return refcount;
53 void * CDECL wined3d_sampler_get_parent(const struct wined3d_sampler *sampler)
55 TRACE("sampler %p.\n", sampler);
57 return sampler->parent;
60 static void wined3d_sampler_init(struct wined3d_sampler *sampler, struct wined3d_device *device,
61 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
63 TRACE("sampler %p, device %p, desc %p, parent %p, parent_ops %p.\n",
64 sampler, device, desc, parent, parent_ops);
66 sampler->refcount = 1;
67 sampler->device = device;
68 sampler->parent = parent;
69 sampler->parent_ops = parent_ops;
70 sampler->desc = *desc;
73 static void wined3d_sampler_gl_cs_init(void *object)
75 struct wined3d_sampler_gl *sampler_gl = object;
76 const struct wined3d_sampler_desc *desc;
77 const struct wined3d_gl_info *gl_info;
78 struct wined3d_context *context;
79 GLuint name;
81 TRACE("sampler_gl %p.\n", sampler_gl);
83 context = context_acquire(sampler_gl->s.device, NULL, 0);
84 gl_info = wined3d_context_gl(context)->gl_info;
86 desc = &sampler_gl->s.desc;
87 GL_EXTCALL(glGenSamplers(1, &name));
88 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_S,
89 gl_info->wrap_lookup[desc->address_u - WINED3D_TADDRESS_WRAP]));
90 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_T,
91 gl_info->wrap_lookup[desc->address_v - WINED3D_TADDRESS_WRAP]));
92 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_R,
93 gl_info->wrap_lookup[desc->address_w - WINED3D_TADDRESS_WRAP]));
94 GL_EXTCALL(glSamplerParameterfv(name, GL_TEXTURE_BORDER_COLOR, &desc->border_color[0]));
95 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MAG_FILTER,
96 wined3d_gl_mag_filter(desc->mag_filter)));
97 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MIN_FILTER,
98 wined3d_gl_min_mip_filter(desc->min_filter, desc->mip_filter)));
99 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_LOD_BIAS, desc->lod_bias));
100 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_MIN_LOD, desc->min_lod));
101 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_MAX_LOD, desc->max_lod));
102 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
103 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MAX_ANISOTROPY, desc->max_anisotropy));
104 if (desc->compare)
105 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE));
106 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_COMPARE_FUNC,
107 wined3d_gl_compare_func(desc->comparison_func)));
108 if ((context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
109 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && !desc->srgb_decode)
110 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT));
111 checkGLcall("sampler creation");
113 TRACE("Created sampler %u.\n", name);
114 sampler_gl->name = name;
116 context_release(context);
119 void wined3d_sampler_gl_init(struct wined3d_sampler_gl *sampler_gl, struct wined3d_device *device,
120 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
122 TRACE("sampler_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
123 sampler_gl, device, desc, parent, parent_ops);
125 wined3d_sampler_init(&sampler_gl->s, device, desc, parent, parent_ops);
127 if (device->adapter->gl_info.supported[ARB_SAMPLER_OBJECTS])
128 wined3d_cs_init_object(device->cs, wined3d_sampler_gl_cs_init, sampler_gl);
131 static VkFilter vk_filter_from_wined3d(enum wined3d_texture_filter_type f)
133 switch (f)
135 default:
136 ERR("Invalid filter type %#x.\n", f);
137 case WINED3D_TEXF_POINT:
138 return VK_FILTER_NEAREST;
139 case WINED3D_TEXF_LINEAR:
140 return VK_FILTER_LINEAR;
144 static VkSamplerMipmapMode vk_mipmap_mode_from_wined3d(enum wined3d_texture_filter_type f)
146 switch (f)
148 default:
149 ERR("Invalid filter type %#x.\n", f);
150 case WINED3D_TEXF_NONE:
151 case WINED3D_TEXF_POINT:
152 return VK_SAMPLER_MIPMAP_MODE_NEAREST;
153 case WINED3D_TEXF_LINEAR:
154 return VK_SAMPLER_MIPMAP_MODE_LINEAR;
158 static VkSamplerAddressMode vk_address_mode_from_wined3d(enum wined3d_texture_address a)
160 switch (a)
162 default:
163 ERR("Invalid address mode %#x.\n", a);
164 case WINED3D_TADDRESS_WRAP:
165 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
166 case WINED3D_TADDRESS_MIRROR:
167 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
168 case WINED3D_TADDRESS_CLAMP:
169 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
170 case WINED3D_TADDRESS_BORDER:
171 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
172 case WINED3D_TADDRESS_MIRROR_ONCE:
173 return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE;
177 static VkBorderColor vk_border_colour_from_wined3d(const struct wined3d_color *colour)
179 unsigned int i;
181 static const struct
183 struct wined3d_color wined3d_colour;
184 VkBorderColor vk_colour;
186 colours[] =
188 {{0.0f, 0.0f, 0.0f, 0.0f}, VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK},
189 {{0.0f, 0.0f, 0.0f, 1.0f}, VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK},
190 {{1.0f, 1.0f, 1.0f, 1.0f}, VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE},
193 for (i = 0; i < ARRAY_SIZE(colours); ++i)
195 if (!memcmp(&colours[i], colour, sizeof(*colour)))
196 return colours[i].vk_colour;
199 FIXME("Unhandled border colour {%.8e, %.8e, %.8e, %.8e}.\n", colour->r, colour->g, colour->b, colour->a);
201 return VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
204 static void wined3d_sampler_vk_cs_init(void *object)
206 struct wined3d_sampler_vk *sampler_vk = object;
207 const struct wined3d_sampler_desc *desc;
208 const struct wined3d_d3d_info *d3d_info;
209 struct VkSamplerCreateInfo sampler_desc;
210 const struct wined3d_vk_info *vk_info;
211 struct wined3d_context_vk *context_vk;
212 struct wined3d_device_vk *device_vk;
213 VkSampler vk_sampler;
214 VkResult vr;
216 TRACE("sampler_vk %p.\n", sampler_vk);
218 context_vk = wined3d_context_vk(context_acquire(sampler_vk->s.device, NULL, 0));
219 device_vk = wined3d_device_vk(context_vk->c.device);
220 d3d_info = context_vk->c.d3d_info;
221 vk_info = context_vk->vk_info;
223 desc = &sampler_vk->s.desc;
224 sampler_desc.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
225 sampler_desc.pNext = NULL;
226 sampler_desc.flags = 0;
227 sampler_desc.magFilter = vk_filter_from_wined3d(desc->mag_filter);
228 sampler_desc.minFilter = vk_filter_from_wined3d(desc->min_filter);
229 sampler_desc.mipmapMode = vk_mipmap_mode_from_wined3d(desc->mip_filter);
230 sampler_desc.addressModeU = vk_address_mode_from_wined3d(desc->address_u);
231 sampler_desc.addressModeV = vk_address_mode_from_wined3d(desc->address_v);
232 sampler_desc.addressModeW = vk_address_mode_from_wined3d(desc->address_w);
233 sampler_desc.mipLodBias = desc->lod_bias;
234 sampler_desc.anisotropyEnable = desc->max_anisotropy != 1;
235 sampler_desc.maxAnisotropy = desc->max_anisotropy;
236 sampler_desc.compareEnable = !!desc->compare;
237 sampler_desc.compareOp = vk_compare_op_from_wined3d(desc->comparison_func);
238 sampler_desc.minLod = desc->min_lod;
239 sampler_desc.maxLod = desc->max_lod;
240 sampler_desc.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
241 sampler_desc.unnormalizedCoordinates = VK_FALSE;
243 if (desc->address_u == WINED3D_TADDRESS_BORDER || desc->address_v == WINED3D_TADDRESS_BORDER
244 || desc->address_w == WINED3D_TADDRESS_BORDER)
245 sampler_desc.borderColor = vk_border_colour_from_wined3d((const struct wined3d_color *)desc->border_color);
246 if (desc->mip_base_level)
247 FIXME("Unhandled mip_base_level %u.\n", desc->mip_base_level);
248 if ((d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL) && !desc->srgb_decode)
249 FIXME("Unhandled srgb_decode %#x.\n", desc->srgb_decode);
251 vr = VK_CALL(vkCreateSampler(device_vk->vk_device, &sampler_desc, NULL, &vk_sampler));
252 context_release(&context_vk->c);
253 if (vr < 0)
255 ERR("Failed to create Vulkan sampler, vr %s.\n", wined3d_debug_vkresult(vr));
256 return;
259 TRACE("Created sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
261 sampler_vk->vk_image_info.sampler = vk_sampler;
262 sampler_vk->vk_image_info.imageView = VK_NULL_HANDLE;
263 sampler_vk->vk_image_info.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
266 void wined3d_sampler_vk_init(struct wined3d_sampler_vk *sampler_vk, struct wined3d_device *device,
267 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
269 TRACE("sampler_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
270 sampler_vk, device, desc, parent, parent_ops);
272 wined3d_sampler_init(&sampler_vk->s, device, desc, parent, parent_ops);
273 wined3d_cs_init_object(device->cs, wined3d_sampler_vk_cs_init, sampler_vk);
276 HRESULT CDECL wined3d_sampler_create(struct wined3d_device *device, const struct wined3d_sampler_desc *desc,
277 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_sampler **sampler)
279 TRACE("device %p, desc %p, parent %p, parent_ops %p, sampler %p.\n",
280 device, desc, parent, parent_ops, sampler);
282 if (desc->address_u < WINED3D_TADDRESS_WRAP || desc->address_u > WINED3D_TADDRESS_MIRROR_ONCE
283 || desc->address_v < WINED3D_TADDRESS_WRAP || desc->address_v > WINED3D_TADDRESS_MIRROR_ONCE
284 || desc->address_w < WINED3D_TADDRESS_WRAP || desc->address_w > WINED3D_TADDRESS_MIRROR_ONCE)
285 return WINED3DERR_INVALIDCALL;
287 if (desc->mag_filter < WINED3D_TEXF_POINT || desc->mag_filter > WINED3D_TEXF_LINEAR
288 || desc->min_filter < WINED3D_TEXF_POINT || desc->min_filter > WINED3D_TEXF_LINEAR
289 || desc->mip_filter > WINED3D_TEXF_LINEAR)
290 return WINED3DERR_INVALIDCALL;
292 return device->adapter->adapter_ops->adapter_create_sampler(device, desc, parent, parent_ops, sampler);
295 static void texture_gl_apply_base_level(struct wined3d_texture_gl *texture_gl,
296 const struct wined3d_sampler_desc *desc, const struct wined3d_gl_info *gl_info)
298 struct gl_texture *gl_tex;
299 unsigned int base_level;
301 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
302 base_level = 0;
303 else if (desc->mip_filter == WINED3D_TEXF_NONE)
304 base_level = texture_gl->t.lod;
305 else
306 base_level = min(max(desc->mip_base_level, texture_gl->t.lod), texture_gl->t.level_count - 1);
308 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
309 if (base_level != gl_tex->base_level)
311 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
312 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
313 * mipmap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
314 * corresponds to GL_TEXTURE_BASE_LEVEL. */
315 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_BASE_LEVEL, base_level);
316 gl_tex->base_level = base_level;
320 /* This function relies on the correct texture being bound and loaded. */
321 void wined3d_sampler_gl_bind(struct wined3d_sampler_gl *sampler_gl, unsigned int unit,
322 struct wined3d_texture_gl *texture_gl, const struct wined3d_context_gl *context_gl)
324 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
326 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
328 GL_EXTCALL(glBindSampler(unit, sampler_gl->name));
329 checkGLcall("bind sampler");
331 else if (texture_gl)
333 wined3d_texture_gl_apply_sampler_desc(texture_gl, &sampler_gl->s.desc, context_gl);
335 else
337 ERR("Could not apply sampler state.\n");
340 if (texture_gl)
341 texture_gl_apply_base_level(texture_gl, &sampler_gl->s.desc, gl_info);