001/* 002 * JavaCL - Java API and utilities for OpenCL 003 * http://javacl.googlecode.com/ 004 * 005 * Copyright (c) 2009-2013, Olivier Chafik (http://ochafik.com/) 006 * All rights reserved. 007 * 008 * Redistribution and use in source and binary forms, with or without 009 * modification, are permitted provided that the following conditions are met: 010 * 011 * * Redistributions of source code must retain the above copyright 012 * notice, this list of conditions and the following disclaimer. 013 * * Redistributions in binary form must reproduce the above copyright 014 * notice, this list of conditions and the following disclaimer in the 015 * documentation and/or other materials provided with the distribution. 016 * * Neither the name of Olivier Chafik nor the 017 * names of its contributors may be used to endorse or promote products 018 * derived from this software without specific prior written permission. 019 * 020 * THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY 021 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 023 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 024 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 027 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 030 */ 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050package com.nativelibs4java.opencl; 051import static com.nativelibs4java.opencl.CLException.error; 052import static com.nativelibs4java.opencl.JavaCL.CL; 053import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_ADDRESS_CLAMP; 054import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_ADDRESS_CLAMP_TO_EDGE; 055import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_ADDRESS_NONE; 056import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_ADDRESS_REPEAT; 057import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_FILTER_LINEAR; 058import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_FILTER_NEAREST; 059import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_SAMPLER_ADDRESSING_MODE; 060import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_SAMPLER_FILTER_MODE; 061import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_SAMPLER_NORMALIZED_COORDS; 062 063import com.nativelibs4java.opencl.library.IOpenCLLibrary.cl_sampler; 064import com.nativelibs4java.util.EnumValue; 065import com.nativelibs4java.util.EnumValues; 066import org.bridj.*; 067import static org.bridj.Pointer.*; 068 069/** 070 * OpenCL sampler object.<br> 071 * A sampler object describes how to sample an image when the image is read in the kernel. <br> 072 * The built-in functions to read from an image in a kernel take a sampler as an argument. <br> 073 * The sampler arguments to the image read function can be sampler objects created using OpenCL functions and passed as argument values to the kernel or can be samplers declared inside a kernel. 074 * <br> 075 * see {@link CLContext#createSampler(boolean, com.nativelibs4java.opencl.CLSampler.AddressingMode, com.nativelibs4java.opencl.CLSampler.FilterMode) } 076 * @author Olivier Chafik 077 */ 078public class CLSampler extends CLAbstractEntity { 079 080 protected static CLInfoGetter infos = new CLInfoGetter() { 081 @Override 082 protected int getInfo(long entity, int infoTypeEnum, long size, Pointer out, Pointer<SizeT> sizeOut) { 083 return CL.clGetSamplerInfo(entity, infoTypeEnum, size, getPeer(out), getPeer(sizeOut)); 084 } 085 }; 086 087 CLSampler(long entity) { 088 super(entity); 089 } 090 091 @Override 092 protected void clear() { 093 error(CL.clReleaseSampler(getEntity())); 094 } 095 096 /** 097 * Values for CL_SAMPLER_ADDRESSING_MODE<br> 098 * How out-of-range image coordinates are handled when reading from an image 099 */ 100 public enum AddressingMode implements com.nativelibs4java.util.ValuedEnum { 101 Repeat(CL_ADDRESS_REPEAT), 102 ClampToEdge(CL_ADDRESS_CLAMP_TO_EDGE), 103 Clamp(CL_ADDRESS_CLAMP), 104 None(CL_ADDRESS_NONE); 105 106 AddressingMode(long value) { this.value = value; } 107 long value; 108 @Override 109 public long value() { return value; } 110 public static AddressingMode getEnum(long v) { return EnumValues.getEnum(v, AddressingMode.class); } 111 } 112 113 /** 114 * Return the value specified by addressing_mode argument to CLContext.createSampler. 115 */ 116 @InfoName("CL_SAMPLER_ADDRESSING_MODE") 117 public AddressingMode getAddressingMode() { 118 return AddressingMode.getEnum(infos.getInt(getEntity(), CL_SAMPLER_ADDRESSING_MODE)); 119 } 120 121 /** 122 * Values for CL_SAMPLER_FILTER_MODE<br> 123 * Type of filter that must be applied when reading an image 124 */ 125 public enum FilterMode implements com.nativelibs4java.util.ValuedEnum { 126 Nearest(CL_FILTER_NEAREST), 127 Linear(CL_FILTER_LINEAR); 128 129 FilterMode(long value) { this.value = value; } 130 long value; 131 @Override 132 public long value() { return value; } 133 public static FilterMode getEnum(int v) { return EnumValues.getEnum(v, FilterMode.class); } 134 } 135 /** 136 * Return the value specified by filter_mode argument to CLContext.createSampler. 137 */ 138 @InfoName("CL_SAMPLER_FILTER_MODE") 139 public FilterMode getFilterMode() { 140 return FilterMode.getEnum(infos.getInt(getEntity(), CL_SAMPLER_FILTER_MODE)); 141 } 142 143 /** 144 * Return the value specified by normalized_coords argument to CLContext.createSampler. 145 */ 146 @InfoName("CL_SAMPLER_NORMALIZED_COORDS") 147 public boolean getNormalizedCoords() { 148 return infos.getBool(getEntity(), CL_SAMPLER_NORMALIZED_COORDS); 149 } 150 151 152 153}