include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / wined3d / sampler.c
blobafd26a527f9790f7dd8185c23b36db5f28944a93
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 "wined3d_private.h"
21 #include "wined3d_gl.h"
22 #include "wined3d_vk.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
26 ULONG CDECL wined3d_sampler_incref(struct wined3d_sampler *sampler)
28 unsigned int refcount = InterlockedIncrement(&sampler->refcount);
30 TRACE("%p increasing refcount to %u.\n", sampler, refcount);
32 return refcount;
35 ULONG CDECL wined3d_sampler_decref(struct wined3d_sampler *sampler)
37 unsigned int refcount = wined3d_atomic_decrement_mutex_lock(&sampler->refcount);
39 TRACE("%p decreasing refcount to %u.\n", sampler, refcount);
41 if (!refcount)
43 sampler->parent_ops->wined3d_object_destroyed(sampler->parent);
44 sampler->device->adapter->adapter_ops->adapter_destroy_sampler(sampler);
45 wined3d_mutex_unlock();
48 return refcount;
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 TRACE(" Address modes: U %#x, V %#x, W %#x.\n", desc->address_u, desc->address_v, desc->address_w);
65 TRACE(" Border colour: {%.8e, %.8e, %.8e, %.8e}.\n",
66 desc->border_color[0], desc->border_color[1], desc->border_color[2], desc->border_color[3]);
67 TRACE(" Filters: mag %#x, min %#x, mip %#x.\n", desc->mag_filter, desc->min_filter, desc->mip_filter);
68 TRACE(" LOD bias: %.8e.\n", desc->lod_bias);
69 TRACE(" Minimum LOD: %.8e.\n", desc->min_lod);
70 TRACE(" Maximum LOD: %.8e.\n", desc->max_lod);
71 TRACE(" Base mip level: %u.\n", desc->mip_base_level);
72 TRACE(" Maximum anisotropy: %u.\n", desc->max_anisotropy);
73 TRACE(" Comparison: %d.\n", desc->compare);
74 TRACE(" Comparison func: %#x.\n", desc->comparison_func);
75 TRACE(" SRGB decode: %d.\n", desc->srgb_decode);
77 sampler->refcount = 1;
78 sampler->device = device;
79 sampler->parent = parent;
80 sampler->parent_ops = parent_ops;
81 sampler->desc = *desc;
84 static void wined3d_sampler_gl_cs_init(void *object)
86 struct wined3d_sampler_gl *sampler_gl = object;
87 const struct wined3d_sampler_desc *desc;
88 const struct wined3d_gl_info *gl_info;
89 struct wined3d_context *context;
90 GLuint name;
92 TRACE("sampler_gl %p.\n", sampler_gl);
94 context = context_acquire(sampler_gl->s.device, NULL, 0);
95 gl_info = wined3d_context_gl(context)->gl_info;
97 desc = &sampler_gl->s.desc;
98 GL_EXTCALL(glGenSamplers(1, &name));
99 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_S,
100 gl_info->wrap_lookup[desc->address_u - WINED3D_TADDRESS_WRAP]));
101 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_T,
102 gl_info->wrap_lookup[desc->address_v - WINED3D_TADDRESS_WRAP]));
103 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_R,
104 gl_info->wrap_lookup[desc->address_w - WINED3D_TADDRESS_WRAP]));
105 GL_EXTCALL(glSamplerParameterfv(name, GL_TEXTURE_BORDER_COLOR, &desc->border_color[0]));
106 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MAG_FILTER,
107 wined3d_gl_mag_filter(desc->mag_filter)));
108 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MIN_FILTER,
109 wined3d_gl_min_mip_filter(desc->min_filter, desc->mip_filter)));
110 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_LOD_BIAS, desc->lod_bias));
111 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_MIN_LOD, desc->min_lod));
112 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_MAX_LOD, desc->max_lod));
113 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
114 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MAX_ANISOTROPY, desc->max_anisotropy));
115 if (desc->compare)
116 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE));
117 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_COMPARE_FUNC,
118 wined3d_gl_compare_func(desc->comparison_func)));
119 if ((context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
120 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && !desc->srgb_decode)
121 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT));
122 checkGLcall("sampler creation");
124 TRACE("Created sampler %u.\n", name);
125 sampler_gl->name = name;
127 context_release(context);
130 void wined3d_sampler_gl_init(struct wined3d_sampler_gl *sampler_gl, struct wined3d_device *device,
131 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
133 TRACE("sampler_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
134 sampler_gl, device, desc, parent, parent_ops);
136 wined3d_sampler_init(&sampler_gl->s, device, desc, parent, parent_ops);
138 if (wined3d_adapter_gl(device->adapter)->gl_info.supported[ARB_SAMPLER_OBJECTS])
139 wined3d_cs_init_object(device->cs, wined3d_sampler_gl_cs_init, sampler_gl);
142 static VkFilter vk_filter_from_wined3d(enum wined3d_texture_filter_type f)
144 switch (f)
146 default:
147 ERR("Invalid filter type %#x.\n", f);
148 case WINED3D_TEXF_POINT:
149 return VK_FILTER_NEAREST;
150 case WINED3D_TEXF_LINEAR:
151 return VK_FILTER_LINEAR;
155 static VkSamplerMipmapMode vk_mipmap_mode_from_wined3d(enum wined3d_texture_filter_type f)
157 switch (f)
159 default:
160 ERR("Invalid filter type %#x.\n", f);
161 case WINED3D_TEXF_NONE:
162 case WINED3D_TEXF_POINT:
163 return VK_SAMPLER_MIPMAP_MODE_NEAREST;
164 case WINED3D_TEXF_LINEAR:
165 return VK_SAMPLER_MIPMAP_MODE_LINEAR;
169 static VkSamplerAddressMode vk_address_mode_from_wined3d(enum wined3d_texture_address a)
171 switch (a)
173 default:
174 ERR("Invalid address mode %#x.\n", a);
175 case WINED3D_TADDRESS_WRAP:
176 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
177 case WINED3D_TADDRESS_MIRROR:
178 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
179 case WINED3D_TADDRESS_CLAMP:
180 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
181 case WINED3D_TADDRESS_BORDER:
182 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
183 case WINED3D_TADDRESS_MIRROR_ONCE:
184 return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE;
188 static VkBorderColor vk_border_colour_from_wined3d(const struct wined3d_color *colour)
190 unsigned int i;
192 static const struct
194 struct wined3d_color wined3d_colour;
195 VkBorderColor vk_colour;
197 colours[] =
199 {{0.0f, 0.0f, 0.0f, 0.0f}, VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK},
200 {{0.0f, 0.0f, 0.0f, 1.0f}, VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK},
201 {{1.0f, 1.0f, 1.0f, 1.0f}, VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE},
204 for (i = 0; i < ARRAY_SIZE(colours); ++i)
206 if (!memcmp(&colours[i], colour, sizeof(*colour)))
207 return colours[i].vk_colour;
210 FIXME("Unhandled border colour {%.8e, %.8e, %.8e, %.8e}.\n", colour->r, colour->g, colour->b, colour->a);
212 return VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
215 static void wined3d_sampler_vk_cs_init(void *object)
217 struct wined3d_sampler_vk *sampler_vk = object;
218 const struct wined3d_sampler_desc *desc;
219 const struct wined3d_d3d_info *d3d_info;
220 struct VkSamplerCreateInfo sampler_desc;
221 const struct wined3d_vk_info *vk_info;
222 struct wined3d_context_vk *context_vk;
223 struct wined3d_device_vk *device_vk;
224 VkSampler vk_sampler;
225 VkResult vr;
227 TRACE("sampler_vk %p.\n", sampler_vk);
229 context_vk = wined3d_context_vk(context_acquire(sampler_vk->s.device, NULL, 0));
230 device_vk = wined3d_device_vk(context_vk->c.device);
231 d3d_info = context_vk->c.d3d_info;
232 vk_info = context_vk->vk_info;
234 desc = &sampler_vk->s.desc;
235 sampler_desc.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
236 sampler_desc.pNext = NULL;
237 sampler_desc.flags = 0;
238 sampler_desc.magFilter = vk_filter_from_wined3d(desc->mag_filter);
239 sampler_desc.minFilter = vk_filter_from_wined3d(desc->min_filter);
240 sampler_desc.mipmapMode = vk_mipmap_mode_from_wined3d(desc->mip_filter);
241 sampler_desc.addressModeU = vk_address_mode_from_wined3d(desc->address_u);
242 sampler_desc.addressModeV = vk_address_mode_from_wined3d(desc->address_v);
243 sampler_desc.addressModeW = vk_address_mode_from_wined3d(desc->address_w);
244 sampler_desc.mipLodBias = desc->lod_bias;
245 sampler_desc.anisotropyEnable = desc->max_anisotropy != 1;
246 sampler_desc.maxAnisotropy = desc->max_anisotropy;
247 sampler_desc.compareEnable = !!desc->compare;
248 sampler_desc.compareOp = vk_compare_op_from_wined3d(desc->comparison_func);
249 sampler_desc.minLod = desc->min_lod;
250 sampler_desc.maxLod = desc->max_lod;
251 sampler_desc.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
252 sampler_desc.unnormalizedCoordinates = VK_FALSE;
254 if (desc->address_u == WINED3D_TADDRESS_BORDER || desc->address_v == WINED3D_TADDRESS_BORDER
255 || desc->address_w == WINED3D_TADDRESS_BORDER)
256 sampler_desc.borderColor = vk_border_colour_from_wined3d((const struct wined3d_color *)desc->border_color);
257 if (desc->mip_base_level)
258 FIXME("Unhandled mip_base_level %u.\n", desc->mip_base_level);
259 if ((d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL) && !desc->srgb_decode)
260 FIXME("Unhandled srgb_decode %#x.\n", desc->srgb_decode);
262 vr = VK_CALL(vkCreateSampler(device_vk->vk_device, &sampler_desc, NULL, &vk_sampler));
263 context_release(&context_vk->c);
264 if (vr < 0)
266 ERR("Failed to create Vulkan sampler, vr %s.\n", wined3d_debug_vkresult(vr));
267 return;
270 TRACE("Created sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
272 sampler_vk->vk_image_info.sampler = vk_sampler;
273 sampler_vk->vk_image_info.imageView = VK_NULL_HANDLE;
274 sampler_vk->vk_image_info.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
277 void wined3d_sampler_vk_init(struct wined3d_sampler_vk *sampler_vk, struct wined3d_device *device,
278 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
280 TRACE("sampler_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
281 sampler_vk, device, desc, parent, parent_ops);
283 wined3d_sampler_init(&sampler_vk->s, device, desc, parent, parent_ops);
284 wined3d_cs_init_object(device->cs, wined3d_sampler_vk_cs_init, sampler_vk);
287 HRESULT CDECL wined3d_sampler_create(struct wined3d_device *device, const struct wined3d_sampler_desc *desc,
288 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_sampler **sampler)
290 TRACE("device %p, desc %p, parent %p, parent_ops %p, sampler %p.\n",
291 device, desc, parent, parent_ops, sampler);
293 if (desc->address_u < WINED3D_TADDRESS_WRAP || desc->address_u > WINED3D_TADDRESS_MIRROR_ONCE
294 || desc->address_v < WINED3D_TADDRESS_WRAP || desc->address_v > WINED3D_TADDRESS_MIRROR_ONCE
295 || desc->address_w < WINED3D_TADDRESS_WRAP || desc->address_w > WINED3D_TADDRESS_MIRROR_ONCE)
296 return WINED3DERR_INVALIDCALL;
298 if (desc->mag_filter < WINED3D_TEXF_POINT || desc->mag_filter > WINED3D_TEXF_LINEAR
299 || desc->min_filter < WINED3D_TEXF_POINT || desc->min_filter > WINED3D_TEXF_LINEAR
300 || desc->mip_filter > WINED3D_TEXF_LINEAR)
301 return WINED3DERR_INVALIDCALL;
303 return device->adapter->adapter_ops->adapter_create_sampler(device, desc, parent, parent_ops, sampler);
306 static void texture_gl_apply_base_level(struct wined3d_texture_gl *texture_gl,
307 const struct wined3d_sampler_desc *desc, const struct wined3d_gl_info *gl_info)
309 struct gl_texture *gl_tex;
311 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
312 if (desc->mip_base_level != gl_tex->sampler_desc.mip_base_level)
314 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_BASE_LEVEL, desc->mip_base_level);
315 gl_tex->sampler_desc.mip_base_level = desc->mip_base_level;
319 /* This function relies on the correct texture being bound and loaded. */
320 void wined3d_sampler_gl_bind(struct wined3d_sampler_gl *sampler_gl, unsigned int unit,
321 struct wined3d_texture_gl *texture_gl, const struct wined3d_context_gl *context_gl)
323 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
325 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
327 GL_EXTCALL(glBindSampler(unit, sampler_gl->name));
328 checkGLcall("bind sampler");
330 else if (texture_gl)
332 wined3d_texture_gl_apply_sampler_desc(texture_gl, &sampler_gl->s.desc, context_gl);
334 else
336 ERR("Could not apply sampler state.\n");
339 if (texture_gl)
340 texture_gl_apply_base_level(texture_gl, &sampler_gl->s.desc, gl_info);