wined3d: Create GL sampler objects for wined3d sampler objects.
[wine/multimedia.git] / dlls / wined3d / sampler.c
blobffa1155ed0f8c243c74b928416f943a896b45cc5
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);
39 const struct wined3d_gl_info *gl_info;
40 struct wined3d_context *context;
42 TRACE("%p decreasing refcount to %u.\n", sampler, refcount);
44 if (!refcount)
46 context = context_acquire(sampler->device, NULL);
47 gl_info = context->gl_info;
48 GL_EXTCALL(glDeleteSamplers(1, &sampler->name));
49 context_release(context);
51 HeapFree(GetProcessHeap(), 0, sampler);
54 return refcount;
57 void * CDECL wined3d_sampler_get_parent(const struct wined3d_sampler *sampler)
59 TRACE("sampler %p.\n", sampler);
61 return sampler->parent;
64 static void wined3d_sampler_init(struct wined3d_sampler *sampler, struct wined3d_device *device,
65 const struct wined3d_sampler_desc *desc, void *parent)
67 const struct wined3d_gl_info *gl_info;
68 struct wined3d_context *context;
70 sampler->refcount = 1;
71 sampler->device = device;
72 sampler->parent = parent;
73 sampler->desc = *desc;
75 context = context_acquire(device, NULL);
76 gl_info = context->gl_info;
78 GL_EXTCALL(glGenSamplers(1, &sampler->name));
79 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_WRAP_S,
80 gl_info->wrap_lookup[desc->address_u - WINED3D_TADDRESS_WRAP]));
81 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_WRAP_T,
82 gl_info->wrap_lookup[desc->address_v - WINED3D_TADDRESS_WRAP]));
83 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_WRAP_R,
84 gl_info->wrap_lookup[desc->address_w - WINED3D_TADDRESS_WRAP]));
85 GL_EXTCALL(glSamplerParameterfv(sampler->name, GL_TEXTURE_BORDER_COLOR, &desc->border_color[0]));
86 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_MAG_FILTER,
87 wined3d_gl_mag_filter(desc->mag_filter)));
88 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_MIN_FILTER,
89 wined3d_gl_min_mip_filter(desc->min_filter, desc->mip_filter)));
90 GL_EXTCALL(glSamplerParameterf(sampler->name, GL_TEXTURE_LOD_BIAS, desc->lod_bias));
91 GL_EXTCALL(glSamplerParameterf(sampler->name, GL_TEXTURE_MIN_LOD, desc->min_lod));
92 GL_EXTCALL(glSamplerParameterf(sampler->name, GL_TEXTURE_MAX_LOD, desc->max_lod));
93 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
94 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_MAX_ANISOTROPY_EXT, desc->max_anisotropy));
95 if (desc->compare)
96 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE));
97 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_COMPARE_FUNC,
98 wined3d_gl_compare_func(desc->comparison_func)));
99 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && !desc->srgb_decode)
100 GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT));
101 checkGLcall("sampler creation");
103 TRACE("Created sampler %u.\n", sampler->name);
105 context_release(context);
108 HRESULT CDECL wined3d_sampler_create(struct wined3d_device *device, const struct wined3d_sampler_desc *desc,
109 void *parent, struct wined3d_sampler **sampler)
111 struct wined3d_sampler *object;
113 TRACE("device %p, desc %p, parent %p, sampler %p.\n", device, desc, parent, sampler);
115 if (!device->adapter->gl_info.supported[ARB_SAMPLER_OBJECTS])
116 return WINED3DERR_INVALIDCALL;
118 if (desc->address_u < WINED3D_TADDRESS_WRAP || desc->address_u > WINED3D_TADDRESS_MIRROR_ONCE
119 || desc->address_v < WINED3D_TADDRESS_WRAP || desc->address_v > WINED3D_TADDRESS_MIRROR_ONCE
120 || desc->address_w < WINED3D_TADDRESS_WRAP || desc->address_w > WINED3D_TADDRESS_MIRROR_ONCE)
121 return WINED3DERR_INVALIDCALL;
123 if (desc->mag_filter < WINED3D_TEXF_POINT || desc->mag_filter > WINED3D_TEXF_LINEAR
124 || desc->min_filter < WINED3D_TEXF_POINT || desc->min_filter > WINED3D_TEXF_LINEAR
125 || desc->mip_filter > WINED3D_TEXF_LINEAR)
126 return WINED3DERR_INVALIDCALL;
128 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
129 return E_OUTOFMEMORY;
131 wined3d_sampler_init(object, device, desc, parent);
133 TRACE("Created sampler %p.\n", object);
134 *sampler = object;
136 return WINED3D_OK;