d2d1: Partially implement d2d_device_context_DrawImage().
[wine.git] / dlls / wined3d / sampler.c
blob8d909627a18c788fca166dd4981bcad5c9b5f611
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 sampler->parent_ops->wined3d_object_destroyed(sampler->parent);
45 sampler->device->adapter->adapter_ops->adapter_destroy_sampler(sampler);
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 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;
77 GLuint name;
79 context = context_acquire(sampler_gl->s.device, NULL, 0);
80 gl_info = wined3d_context_gl(context)->gl_info;
82 desc = &sampler_gl->s.desc;
83 GL_EXTCALL(glGenSamplers(1, &name));
84 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_S,
85 gl_info->wrap_lookup[desc->address_u - WINED3D_TADDRESS_WRAP]));
86 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_T,
87 gl_info->wrap_lookup[desc->address_v - WINED3D_TADDRESS_WRAP]));
88 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_WRAP_R,
89 gl_info->wrap_lookup[desc->address_w - WINED3D_TADDRESS_WRAP]));
90 GL_EXTCALL(glSamplerParameterfv(name, GL_TEXTURE_BORDER_COLOR, &desc->border_color[0]));
91 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MAG_FILTER,
92 wined3d_gl_mag_filter(desc->mag_filter)));
93 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MIN_FILTER,
94 wined3d_gl_min_mip_filter(desc->min_filter, desc->mip_filter)));
95 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_LOD_BIAS, desc->lod_bias));
96 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_MIN_LOD, desc->min_lod));
97 GL_EXTCALL(glSamplerParameterf(name, GL_TEXTURE_MAX_LOD, desc->max_lod));
98 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
99 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_MAX_ANISOTROPY, desc->max_anisotropy));
100 if (desc->compare)
101 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE));
102 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_COMPARE_FUNC,
103 wined3d_gl_compare_func(desc->comparison_func)));
104 if ((context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
105 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && !desc->srgb_decode)
106 GL_EXTCALL(glSamplerParameteri(name, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT));
107 checkGLcall("sampler creation");
109 TRACE("Created sampler %u.\n", name);
110 sampler_gl->name = name;
112 context_release(context);
115 void wined3d_sampler_gl_init(struct wined3d_sampler_gl *sampler_gl, struct wined3d_device *device,
116 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
118 TRACE("sampler_gl %p, device %p, desc %p, parent %p, parent_ops %p.\n",
119 sampler_gl, device, desc, parent, parent_ops);
121 wined3d_sampler_init(&sampler_gl->s, device, desc, parent, parent_ops);
123 if (device->adapter->gl_info.supported[ARB_SAMPLER_OBJECTS])
124 wined3d_cs_init_object(device->cs, wined3d_sampler_gl_cs_init, sampler_gl);
127 static VkFilter vk_filter_from_wined3d(enum wined3d_texture_filter_type f)
129 switch (f)
131 default:
132 ERR("Invalid filter type %#x.\n", f);
133 case WINED3D_TEXF_POINT:
134 return VK_FILTER_NEAREST;
135 case WINED3D_TEXF_LINEAR:
136 return VK_FILTER_LINEAR;
140 static VkSamplerMipmapMode vk_mipmap_mode_from_wined3d(enum wined3d_texture_filter_type f)
142 switch (f)
144 default:
145 ERR("Invalid filter type %#x.\n", f);
146 case WINED3D_TEXF_NONE:
147 case WINED3D_TEXF_POINT:
148 return VK_SAMPLER_MIPMAP_MODE_NEAREST;
149 case WINED3D_TEXF_LINEAR:
150 return VK_SAMPLER_MIPMAP_MODE_LINEAR;
154 static VkSamplerAddressMode vk_address_mode_from_wined3d(enum wined3d_texture_address a)
156 switch (a)
158 default:
159 ERR("Invalid address mode %#x.\n", a);
160 case WINED3D_TADDRESS_WRAP:
161 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
162 case WINED3D_TADDRESS_MIRROR:
163 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
164 case WINED3D_TADDRESS_CLAMP:
165 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
166 case WINED3D_TADDRESS_BORDER:
167 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
168 case WINED3D_TADDRESS_MIRROR_ONCE:
169 return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE;
173 static void wined3d_sampler_vk_cs_init(void *object)
175 struct wined3d_sampler_vk *sampler_vk = object;
176 const struct wined3d_sampler_desc *desc;
177 const struct wined3d_d3d_info *d3d_info;
178 struct VkSamplerCreateInfo sampler_desc;
179 const struct wined3d_vk_info *vk_info;
180 struct wined3d_context_vk *context_vk;
181 struct wined3d_device_vk *device_vk;
182 VkSampler vk_sampler;
183 VkResult vr;
185 context_vk = wined3d_context_vk(context_acquire(sampler_vk->s.device, NULL, 0));
186 device_vk = wined3d_device_vk(context_vk->c.device);
187 d3d_info = context_vk->c.d3d_info;
188 vk_info = context_vk->vk_info;
190 desc = &sampler_vk->s.desc;
191 sampler_desc.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
192 sampler_desc.pNext = NULL;
193 sampler_desc.flags = 0;
194 sampler_desc.magFilter = vk_filter_from_wined3d(desc->mag_filter);
195 sampler_desc.minFilter = vk_filter_from_wined3d(desc->min_filter);
196 sampler_desc.mipmapMode = vk_mipmap_mode_from_wined3d(desc->mip_filter);
197 sampler_desc.addressModeU = vk_address_mode_from_wined3d(desc->address_u);
198 sampler_desc.addressModeV = vk_address_mode_from_wined3d(desc->address_v);
199 sampler_desc.addressModeW = vk_address_mode_from_wined3d(desc->address_w);
200 sampler_desc.mipLodBias = desc->lod_bias;
201 sampler_desc.anisotropyEnable = desc->max_anisotropy != 1;
202 sampler_desc.maxAnisotropy = desc->max_anisotropy;
203 sampler_desc.compareEnable = desc->compare;
204 sampler_desc.compareOp = vk_compare_op_from_wined3d(desc->comparison_func);
205 sampler_desc.minLod = desc->min_lod;
206 sampler_desc.maxLod = desc->max_lod;
207 sampler_desc.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
208 sampler_desc.unnormalizedCoordinates = VK_FALSE;
210 if ((desc->address_u == WINED3D_TADDRESS_BORDER || desc->address_v == WINED3D_TADDRESS_BORDER
211 || desc->address_w == WINED3D_TADDRESS_BORDER)
212 && (desc->border_color[0] != 0.0f || desc->border_color[1] != 0.0f
213 || desc->border_color[2] != 0.0f || desc->border_color[3] != 0.0f))
214 FIXME("Unhandled border colour {%.8e, %.8e, %.8e, %.8e}.\n",
215 desc->border_color[0], desc->border_color[1],
216 desc->border_color[2], desc->border_color[3]);
217 if (desc->mip_base_level)
218 FIXME("Unhandled mip_base_level %u.\n", desc->mip_base_level);
219 if ((d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL) && !desc->srgb_decode)
220 FIXME("Unhandled srgb_decode %#x.\n", desc->srgb_decode);
222 vr = VK_CALL(vkCreateSampler(device_vk->vk_device, &sampler_desc, NULL, &vk_sampler));
223 context_release(&context_vk->c);
224 if (vr < 0)
226 ERR("Failed to create Vulkan sampler, vr %s.\n", wined3d_debug_vkresult(vr));
227 return;
230 TRACE("Created sampler 0x%s.\n", wine_dbgstr_longlong(vk_sampler));
232 sampler_vk->vk_image_info.sampler = vk_sampler;
233 sampler_vk->vk_image_info.imageView = VK_NULL_HANDLE;
234 sampler_vk->vk_image_info.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
237 void wined3d_sampler_vk_init(struct wined3d_sampler_vk *sampler_vk, struct wined3d_device *device,
238 const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
240 TRACE("sampler_vk %p, device %p, desc %p, parent %p, parent_ops %p.\n",
241 sampler_vk, device, desc, parent, parent_ops);
243 wined3d_sampler_init(&sampler_vk->s, device, desc, parent, parent_ops);
244 wined3d_cs_init_object(device->cs, wined3d_sampler_vk_cs_init, sampler_vk);
247 HRESULT CDECL wined3d_sampler_create(struct wined3d_device *device, const struct wined3d_sampler_desc *desc,
248 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_sampler **sampler)
250 TRACE("device %p, desc %p, parent %p, parent_ops %p, sampler %p.\n",
251 device, desc, parent, parent_ops, sampler);
253 if (desc->address_u < WINED3D_TADDRESS_WRAP || desc->address_u > WINED3D_TADDRESS_MIRROR_ONCE
254 || desc->address_v < WINED3D_TADDRESS_WRAP || desc->address_v > WINED3D_TADDRESS_MIRROR_ONCE
255 || desc->address_w < WINED3D_TADDRESS_WRAP || desc->address_w > WINED3D_TADDRESS_MIRROR_ONCE)
256 return WINED3DERR_INVALIDCALL;
258 if (desc->mag_filter < WINED3D_TEXF_POINT || desc->mag_filter > WINED3D_TEXF_LINEAR
259 || desc->min_filter < WINED3D_TEXF_POINT || desc->min_filter > WINED3D_TEXF_LINEAR
260 || desc->mip_filter > WINED3D_TEXF_LINEAR)
261 return WINED3DERR_INVALIDCALL;
263 return device->adapter->adapter_ops->adapter_create_sampler(device, desc, parent, parent_ops, sampler);
266 static void texture_gl_apply_base_level(struct wined3d_texture_gl *texture_gl,
267 const struct wined3d_sampler_desc *desc, const struct wined3d_gl_info *gl_info)
269 struct gl_texture *gl_tex;
270 unsigned int base_level;
272 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
273 base_level = 0;
274 else if (desc->mip_filter == WINED3D_TEXF_NONE)
275 base_level = texture_gl->t.lod;
276 else
277 base_level = min(max(desc->mip_base_level, texture_gl->t.lod), texture_gl->t.level_count - 1);
279 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
280 if (base_level != gl_tex->base_level)
282 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
283 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
284 * mipmap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
285 * corresponds to GL_TEXTURE_BASE_LEVEL. */
286 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_BASE_LEVEL, base_level);
287 gl_tex->base_level = base_level;
291 /* This function relies on the correct texture being bound and loaded. */
292 void wined3d_sampler_gl_bind(struct wined3d_sampler_gl *sampler_gl, unsigned int unit,
293 struct wined3d_texture_gl *texture_gl, const struct wined3d_context_gl *context_gl)
295 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
297 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
299 GL_EXTCALL(glBindSampler(unit, sampler_gl->name));
300 checkGLcall("bind sampler");
302 else if (texture_gl)
304 wined3d_texture_gl_apply_sampler_desc(texture_gl, &sampler_gl->s.desc, context_gl);
306 else
308 ERR("Could not apply sampler state.\n");
311 if (texture_gl)
312 texture_gl_apply_base_level(texture_gl, &sampler_gl->s.desc, gl_info);