source: trunk/CrypPlugins/Libs/OpenCLNet/Sampler.cs @ 2040

Last change on this file since 2040 was 2040, checked in by Sven Rech, 11 years ago
  • added OpenCL.Net project
  • added OpenCL settings in KeySearcher
  • changed rereservation status message behavior
File size: 5.7 KB
Line 
1/*
2 * Copyright (c) 2009 Olav Kalgraf(olav.kalgraf@gmail.com)
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26using System;
27using System.Collections.Generic;
28using System.Linq;
29using System.Text;
30
31namespace OpenCLNet
32{
33
34    /// <summary>
35    /// Wrapper for an OpenCL sampler
36    /// </summary>
37    unsafe public class Sampler : IDisposable, InteropTools.IPropertyContainer
38    {
39        // Track whether Dispose has been called.
40        private bool disposed = false;
41
42        #region Properties
43
44        public IntPtr SamplerID { get; protected set; }
45        public Context Context { get; protected set; }
46
47        public uint ReferenceCount { get { return InteropTools.ReadUInt( this, (uint)SamplerInfo.REFERENCE_COUNT ); } }
48        public AddressingMode AddressingMode { get { return (AddressingMode)InteropTools.ReadUInt( this, (uint)SamplerInfo.ADDRESSING_MODE ); } }
49        public FilterMode FilterMode { get { return (FilterMode)InteropTools.ReadUInt( this, (uint)SamplerInfo.FILTER_MODE ); } }
50        public bool NormalizedCoords { get { return InteropTools.ReadBool( this, (uint)SamplerInfo.NORMALIZED_COORDS ); } }
51
52        #endregion
53
54        #region Construction / Destruction
55
56        internal Sampler( Context context, IntPtr samplerID )
57        {
58            Context = context;
59            SamplerID = samplerID;
60        }
61
62        // Use C# destructor syntax for finalization code.
63        // This destructor will run only if the Dispose method
64        // does not get called.
65        // It gives your base class the opportunity to finalize.
66        // Do not provide destructors in types derived from this class.
67        ~Sampler()
68        {
69            // Do not re-create Dispose clean-up code here.
70            // Calling Dispose(false) is optimal in terms of
71            // readability and maintainability.
72            Dispose( false );
73        }
74
75        #endregion
76
77        #region IDisposable Members
78
79        // Implement IDisposable.
80        // Do not make this method virtual.
81        // A derived class should not be able to override this method.
82        public void Dispose()
83        {
84            Dispose( true );
85            // This object will be cleaned up by the Dispose method.
86            // Therefore, you should call GC.SupressFinalize to
87            // take this object off the finalization queue
88            // and prevent finalization code for this object
89            // from executing a second time.
90            GC.SuppressFinalize( this );
91        }
92
93        // Dispose(bool disposing) executes in two distinct scenarios.
94        // If disposing equals true, the method has been called directly
95        // or indirectly by a user's code. Managed and unmanaged resources
96        // can be disposed.
97        // If disposing equals false, the method has been called by the
98        // runtime from inside the finalizer and you should not reference
99        // other objects. Only unmanaged resources can be disposed.
100        private void Dispose( bool disposing )
101        {
102            // Check to see if Dispose has already been called.
103            if( !this.disposed )
104            {
105                // If disposing equals true, dispose all managed
106                // and unmanaged resources.
107                if( disposing )
108                {
109                    // Dispose managed resources.
110                }
111
112                // Call the appropriate methods to clean up
113                // unmanaged resources here.
114                // If disposing is false,
115                // only the following code is executed.
116                OpenCL.ReleaseSampler( SamplerID );
117                SamplerID = IntPtr.Zero;
118
119                // Note disposing has been done.
120                disposed = true;
121            }
122        }
123
124        #endregion
125
126        #region IPropertyContainer Members
127
128
129        public IntPtr GetPropertySize( uint key )
130        {
131            ErrorCode result;
132            IntPtr size;
133
134            result = (ErrorCode)OpenCL.GetSamplerInfo( SamplerID, key, IntPtr.Zero, null, out size );
135            if( result!=ErrorCode.SUCCESS )
136                throw new OpenCLException( "clGetSamplerInfo failed with error code "+result, result);
137
138            return size;
139        }
140
141        public void ReadProperty( uint key, IntPtr keyLength, void* pBuffer )
142        {
143            ErrorCode result;
144            IntPtr size;
145
146            result = (ErrorCode)OpenCL.GetSamplerInfo( SamplerID, key, keyLength, pBuffer, out size );
147            if( result!=ErrorCode.SUCCESS )
148                throw new OpenCLException( "clGetSamplerInfo failed with error code "+result, result);
149        }
150
151
152        #endregion
153    }
154}
Note: See TracBrowser for help on using the repository browser.