1/*
2 * Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include "MTLSamplerManager.h"
27#include "MTLContext.h"
28#include "sun_java2d_SunGraphics2D.h"
29#import "common.h"
30
31@implementation MTLSamplerManager {
32    id<MTLSamplerState> _samplerNearestClamp;
33    id<MTLSamplerState> _samplerLinearClamp;
34    id<MTLSamplerState> _samplerNearestRepeat;
35    id<MTLSamplerState> _samplerLinearRepeat;
36}
37
38- (id _Nonnull)initWithDevice:(id<MTLDevice>) device {
39    self = [super init];
40    if (self) {
41        MTLSamplerDescriptor *samplerDescriptor = [[MTLSamplerDescriptor new] autorelease];
42
43        samplerDescriptor.rAddressMode = MTLSamplerAddressModeClampToEdge;
44        samplerDescriptor.sAddressMode = MTLSamplerAddressModeClampToEdge;
45        samplerDescriptor.tAddressMode = MTLSamplerAddressModeClampToEdge;
46
47        samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
48        samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
49        _samplerNearestClamp = [device newSamplerStateWithDescriptor:samplerDescriptor];
50
51        samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
52        samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
53        _samplerLinearClamp = [device newSamplerStateWithDescriptor:samplerDescriptor];
54
55        samplerDescriptor.rAddressMode = MTLSamplerAddressModeRepeat;
56        samplerDescriptor.sAddressMode = MTLSamplerAddressModeRepeat;
57        samplerDescriptor.tAddressMode = MTLSamplerAddressModeRepeat;
58
59        samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
60        samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
61        _samplerNearestRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];
62
63        samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
64        samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
65        _samplerLinearRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];
66    }
67    return self;
68}
69
70- (void) setSamplerWithEncoder:(id<MTLRenderCommandEncoder>) encoder
71                 interpolation:(int) interpolation
72                        repeat:(bool) repeat {
73    id<MTLSamplerState> sampler;
74    if (repeat) {
75        sampler = interpolation == INTERPOLATION_BILINEAR ? _samplerLinearRepeat : _samplerNearestRepeat;
76    } else {
77        sampler = interpolation == INTERPOLATION_BILINEAR ? _samplerLinearClamp : _samplerNearestClamp;
78    }
79    [encoder setFragmentSamplerState:sampler atIndex:0];
80}
81
82- (void)dealloc {
83    [_samplerNearestClamp release];
84    [_samplerLinearClamp release];
85    [_samplerNearestRepeat release];
86    [_samplerLinearRepeat release];
87    [super dealloc];
88}
89
90@end
91