memory.cpp 8.37 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ================================================================================================
// 
// This file is part of the KissCL, an OpenCL C++ wrapper following the KISS principle.
// 
// Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge (software@cszb.net)
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the 
// GNU Lesser General Public License version 3 as published by the Free Software Foundation.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 
// the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License in the file 
// "LICENSE.txt" along with this library. If not, see <http://www.gnu.org/licenses/>.
// 
// ================================================================================================

20
21
22
23
#include "memory.h"

#include "tgt/assert.h"
#include "tgt/logmanager.h"
schultezub's avatar
schultezub committed
24
#include "tgt/texture.h"
schultezub's avatar
schultezub committed
25
#include "kisscl/context.h"
26

schultezub's avatar
schultezub committed
27
namespace kisscl {
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    
    MemoryObject::MemoryObject(const Context* context)
        : CLWrapper<cl_mem>(0)
        , _context(context)
    {
    }

    MemoryObject::~MemoryObject() {

    }

    const Context* MemoryObject::getContext() const {
        return _context;
    }

// ================================================================================================

    Buffer::Buffer(const Context* context, cl_mem_flags flags, size_t size, const void* hostPtr /*= 0*/)
        : MemoryObject(context)
        , _size(size)
    {
        tgtAssert(size > 0, "Buffer size must be greater than 0.");

        cl_int err;
        _id = clCreateBuffer(context->getId(), flags, size, const_cast<void*>(hostPtr), &err);
        LCL_ERROR(err);
    }


    Buffer::~Buffer() {

    }

    size_t Buffer::getSize() const {
        return _size;
    }

schultezub's avatar
schultezub committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// ================================================================================================

    Image::Image(const Context* context, cl_mem_flags flags, const tgt::Texture* texture)
        : MemoryObject(context)
    {
        tgtAssert(texture != 0, "Texture must not be 0");
        if (!texture->getPixelData()) {
            LERRORC("kisscl.Image", "Invalid pixel data in given texture.");
            return;
        }

        cl_int err;
        const tgt::ivec3& dims = texture->getDimensions();
        GLenum texDataType = texture->getDataType();
        GLint  texFormat = texture->getFormat();

        cl_image_format imageFormat;
        switch(texDataType) {
            case GL_BYTE:
                imageFormat.image_channel_data_type = CL_SNORM_INT8;
                break;
            case GL_UNSIGNED_BYTE:
                imageFormat.image_channel_data_type = CL_UNORM_INT8;
                break;
            case GL_SHORT:
                imageFormat.image_channel_data_type = CL_SNORM_INT16;
                break;
            case GL_UNSIGNED_SHORT:
                imageFormat.image_channel_data_type = CL_UNORM_INT16;
                break;
            case GL_INT:
                imageFormat.image_channel_data_type = CL_SIGNED_INT32;
                break;
            case GL_UNSIGNED_INT:
                imageFormat.image_channel_data_type = CL_UNSIGNED_INT32;
                break;
            case GL_FLOAT:
                imageFormat.image_channel_data_type = CL_FLOAT;
                break;
        }

        switch(texFormat) {
            case GL_ALPHA:
            case GL_LUMINANCE:
                imageFormat.image_channel_order = CL_A;
                break;
            case GL_LUMINANCE_ALPHA:
                imageFormat.image_channel_order = CL_RA;
                break;
            case GL_RGBA:
                imageFormat.image_channel_order = CL_RGBA;
                break;
        }

#if defined(CL_VERSION_1_2)
        cl_image_desc desc;
        if (dims.y == 1 && dims.z == 1)
            desc.image_type = CL_MEM_OBJECT_IMAGE1D;
        else if (dims.z == 1)
            desc.image_type = CL_MEM_OBJECT_IMAGE2D;
        else
            desc.image_type = CL_MEM_OBJECT_IMAGE3D;

        desc.image_width = dims.x;
        desc.image_height = dims.y;
        desc.image_depth = dims.z
        desc.image_row_pitch = 0;
        desc.image_slice_pitch = 0;
        desc.num_mip_levels = 0;
        desc.num_samples = 0;
        desc.buffer = 0;

        object_ = clCreateImage(context->getId(), flags, &imageFormat, &desc, texture->getPixelData(), &err);
#else // defined(CL_VERSION_1_2)
        if (dims.z == 1) {
            // 1D or 2D image
            _id = clCreateImage2D(context->getId(), flags, &imageFormat, dims.x, dims.y, 0, const_cast<GLubyte*>(texture->getPixelData()), &err);
        }
        else {
            // 3D image
            _id = clCreateImage3D(context->getId(), flags, &imageFormat, dims.x, dims.y, dims.z, 0, 0, const_cast<GLubyte*>(texture->getPixelData()), &err);
        }
#endif // defined(CL_VERSION_1_2)

        LCL_ERROR(err);
    }

    Image::Image(const Context* context, cl_mem_flags flags, const tgt::svec3& dimensions, cl_channel_order channelOrder, cl_channel_type channelType, void* hostPtr)
        : MemoryObject(context)
    {
        tgtAssert(hostPtr != 0, "Texture must not be 0");

        cl_int err;
        cl_image_format imageFormat;
        imageFormat.image_channel_data_type = channelType;
        imageFormat.image_channel_order = channelOrder;

#if defined(CL_VERSION_1_2)
        cl_image_desc desc;
        if (dimensions.y == 1 && dimensions.z == 1)
            desc.image_type = CL_MEM_OBJECT_IMAGE1D;
        else if (dimensions.z == 1)
            desc.image_type = CL_MEM_OBJECT_IMAGE2D;
        else
            desc.image_type = CL_MEM_OBJECT_IMAGE3D;

        desc.image_width = dimensions.x;
        desc.image_height = dimensions.y;
        desc.image_depth = dimensions.z
            desc.image_row_pitch = 0;
        desc.image_slice_pitch = 0;
        desc.num_mip_levels = 0;
        desc.num_samples = 0;
        desc.buffer = 0;

        object_ = clCreateImage(context->getId(), flags, &imageFormat, &desc, texture->getPixelData(), &err);
#else // defined(CL_VERSION_1_2)
        if (dimensions.z == 1) {
            // 1D or 2D image
            _id = clCreateImage2D(context->getId(), flags, &imageFormat, dimensions.x, dimensions.y, 0, hostPtr, &err);
        }
        else {
            // 3D image
            _id = clCreateImage3D(context->getId(), flags, &imageFormat, dimensions.x, dimensions.y, dimensions.z, 0, 0, hostPtr, &err);
        }
#endif // defined(CL_VERSION_1_2)

        LCL_ERROR(err);
    }

    Image::~Image() {

    }


    SharedTexture::SharedTexture(const Context* context, cl_mem_flags flags, const tgt::Texture* texture)
        : MemoryObject(context)
        , _texture(texture)
    {
        tgtAssert(_texture != 0, "Texture must not be 0.");
        tgtAssert(glIsTexture(_texture->getId()) == GL_TRUE, "Texture must be a valid OpenGL texture.");

        cl_int err;
#if defined(CL_VERSION_1_2)
        _id = clCreateFromGLTexture(context->getId(), flags, _texture->getType(), 0, _texture->getId(), &err);
#else // defined(CL_VERSION_1_2)
        switch(_texture->getType()) {
            case GL_TEXTURE_3D:
                _id = clCreateFromGLTexture3D(context->getId(), flags, _texture->getType(), 0, _texture->getId(), &err);
                LCL_ERROR(err);
                break;
            case GL_TEXTURE_2D:
            case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
            case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
            case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
            case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
            case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
            case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
            case GL_TEXTURE_RECTANGLE:
                _id = clCreateFromGLTexture2D(context->getId(), flags, _texture->getType(), 0, _texture->getId(), &err);
                LCL_ERROR(err);
                break;
            default:
                LERRORC("kisscl.SharedTexture", "Unknown texture type!");
#endif // defined(CL_VERSION_1_2)

        }
    }

    SharedTexture::~SharedTexture() {

    }

238
}