xref: /reactos/dll/directx/wine/wined3d/sampler.c (revision 595b846d)
1 /*
2  * Copyright 2012, 2015 Henri Verbeet for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  *
18  */
19 
20 #include "wined3d_private.h"
21 
22 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
23 
24 ULONG CDECL wined3d_sampler_incref(struct wined3d_sampler *sampler)
25 {
26     ULONG refcount = InterlockedIncrement(&sampler->refcount);
27 
28     TRACE("%p increasing refcount to %u.\n", sampler, refcount);
29 
30     return refcount;
31 }
32 
33 static void wined3d_sampler_destroy_object(void *object)
34 {
35     struct wined3d_sampler *sampler = object;
36     const struct wined3d_gl_info *gl_info;
37     struct wined3d_context *context;
38 
39     if (sampler->name)
40     {
41         context = context_acquire(sampler->device, NULL, 0);
42         gl_info = context->gl_info;
43         GL_EXTCALL(glDeleteSamplers(1, &sampler->name));
44         context_release(context);
45     }
46 
47     HeapFree(GetProcessHeap(), 0, sampler);
48 }
49 
50 ULONG CDECL wined3d_sampler_decref(struct wined3d_sampler *sampler)
51 {
52     ULONG refcount = InterlockedDecrement(&sampler->refcount);
53 
54     TRACE("%p decreasing refcount to %u.\n", sampler, refcount);
55 
56     if (!refcount)
57     {
58         sampler->parent_ops->wined3d_object_destroyed(sampler->parent);
59         wined3d_cs_destroy_object(sampler->device->cs, wined3d_sampler_destroy_object, sampler);
60     }
61 
62     return refcount;
63 }
64 
65 void * CDECL wined3d_sampler_get_parent(const struct wined3d_sampler *sampler)
66 {
67     TRACE("sampler %p.\n", sampler);
68 
69     return sampler->parent;
70 }
71 
72 static void wined3d_sampler_cs_init(void *object)
73 {
74     struct wined3d_sampler *sampler = object;
75     const struct wined3d_sampler_desc *desc;
76     const struct wined3d_gl_info *gl_info;
77     struct wined3d_context *context;
78 
79     context = context_acquire(sampler->device, NULL, 0);
80     gl_info = context->gl_info;
81 
82     desc = &sampler->desc;
83     GL_EXTCALL(glGenSamplers(1, &sampler->name));
84     GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_WRAP_S,
85             gl_info->wrap_lookup[desc->address_u - WINED3D_TADDRESS_WRAP]));
86     GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_WRAP_T,
87             gl_info->wrap_lookup[desc->address_v - WINED3D_TADDRESS_WRAP]));
88     GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_WRAP_R,
89             gl_info->wrap_lookup[desc->address_w - WINED3D_TADDRESS_WRAP]));
90     GL_EXTCALL(glSamplerParameterfv(sampler->name, GL_TEXTURE_BORDER_COLOR, &desc->border_color[0]));
91     GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_MAG_FILTER,
92             wined3d_gl_mag_filter(desc->mag_filter)));
93     GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_MIN_FILTER,
94             wined3d_gl_min_mip_filter(desc->min_filter, desc->mip_filter)));
95     GL_EXTCALL(glSamplerParameterf(sampler->name, GL_TEXTURE_LOD_BIAS, desc->lod_bias));
96     GL_EXTCALL(glSamplerParameterf(sampler->name, GL_TEXTURE_MIN_LOD, desc->min_lod));
97     GL_EXTCALL(glSamplerParameterf(sampler->name, GL_TEXTURE_MAX_LOD, desc->max_lod));
98     if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
99         GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_MAX_ANISOTROPY, desc->max_anisotropy));
100     if (desc->compare)
101         GL_EXTCALL(glSamplerParameteri(sampler->name, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE));
102     GL_EXTCALL(glSamplerParameteri(sampler->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(sampler->name, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT));
107     checkGLcall("sampler creation");
108 
109     TRACE("Created sampler %u.\n", sampler->name);
110 
111     context_release(context);
112 }
113 
114 static void wined3d_sampler_init(struct wined3d_sampler *sampler, struct wined3d_device *device,
115         const struct wined3d_sampler_desc *desc, void *parent, const struct wined3d_parent_ops *parent_ops)
116 {
117     sampler->refcount = 1;
118     sampler->device = device;
119     sampler->parent = parent;
120     sampler->parent_ops = parent_ops;
121     sampler->desc = *desc;
122 
123     if (device->adapter->gl_info.supported[ARB_SAMPLER_OBJECTS])
124         wined3d_cs_init_object(device->cs, wined3d_sampler_cs_init, sampler);
125 }
126 
127 HRESULT CDECL wined3d_sampler_create(struct wined3d_device *device, const struct wined3d_sampler_desc *desc,
128         void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_sampler **sampler)
129 {
130     struct wined3d_sampler *object;
131 
132     TRACE("device %p, desc %p, parent %p, sampler %p.\n", device, desc, parent, sampler);
133 
134     if (desc->address_u < WINED3D_TADDRESS_WRAP || desc->address_u > WINED3D_TADDRESS_MIRROR_ONCE
135             || desc->address_v < WINED3D_TADDRESS_WRAP || desc->address_v > WINED3D_TADDRESS_MIRROR_ONCE
136             || desc->address_w < WINED3D_TADDRESS_WRAP || desc->address_w > WINED3D_TADDRESS_MIRROR_ONCE)
137         return WINED3DERR_INVALIDCALL;
138 
139     if (desc->mag_filter < WINED3D_TEXF_POINT || desc->mag_filter > WINED3D_TEXF_LINEAR
140             || desc->min_filter < WINED3D_TEXF_POINT || desc->min_filter > WINED3D_TEXF_LINEAR
141             || desc->mip_filter > WINED3D_TEXF_LINEAR)
142         return WINED3DERR_INVALIDCALL;
143 
144     if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
145         return E_OUTOFMEMORY;
146 
147     wined3d_sampler_init(object, device, desc, parent, parent_ops);
148 
149     TRACE("Created sampler %p.\n", object);
150     *sampler = object;
151 
152     return WINED3D_OK;
153 }
154 
155 static void texture_apply_base_level(struct wined3d_texture *texture,
156         const struct wined3d_sampler_desc *desc, const struct wined3d_gl_info *gl_info)
157 {
158     struct gl_texture *gl_tex;
159     unsigned int base_level;
160 
161     if (texture->flags & WINED3D_TEXTURE_COND_NP2)
162         base_level = 0;
163     else if (desc->mip_filter == WINED3D_TEXF_NONE)
164         base_level = texture->lod;
165     else
166         base_level = min(max(desc->mip_base_level, texture->lod), texture->level_count - 1);
167 
168     gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
169     if (base_level != gl_tex->base_level)
170     {
171         /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
172          * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
173          * mipmap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
174          * corresponds to GL_TEXTURE_BASE_LEVEL. */
175         gl_info->gl_ops.gl.p_glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, base_level);
176         gl_tex->base_level = base_level;
177     }
178 }
179 
180 /* This function relies on the correct texture being bound and loaded. */
181 void wined3d_sampler_bind(struct wined3d_sampler *sampler, unsigned int unit,
182         struct wined3d_texture *texture, const struct wined3d_context *context)
183 {
184     const struct wined3d_gl_info *gl_info = context->gl_info;
185 
186     if (gl_info->supported[ARB_SAMPLER_OBJECTS])
187     {
188         GL_EXTCALL(glBindSampler(unit, sampler->name));
189         checkGLcall("bind sampler");
190     }
191     else if (texture)
192     {
193         wined3d_texture_apply_sampler_desc(texture, &sampler->desc, context);
194     }
195     else
196     {
197         ERR("Could not apply sampler state.\n");
198     }
199 
200     if (texture)
201         texture_apply_base_level(texture, &sampler->desc, gl_info);
202 }
203