2

我正在尝试使用 OpenCL 绘制从 OpenGL 纹理获得的 cl_image,然后渲染该纹理。问题是当我在 CL_DEVICE_TYPE_CPU 上运行我的代码时,它工作正常,但是当我在 CL_DEVICE_TYPE_GPU 上运行时,纹理似乎是一些随机像素。我是 OpenCL 的新手,不确定发生了什么,所以我将在下面发布代码,也在 OSX 上使用 OpenCL。

主机代码:

#import "GLView.h"
#import <GLKit/GLKit.h>
#import <OpenCL/OpenCL.h>
#import "kernel.cl.h"

#define WIDTH 500
#define HEIGHT 500

static GLfloat squareVertexData[] =
{
    -0.5f, -0.5f, 0.0f,     0.0f, 0.0f,
    0.5f, -0.5f, 0.0f,      1.0f, 0.0f,
    0.5f, 0.5f, 0.0f,       1.0f, 1.0f,
    -0.5f, -0.5f, 0.0f,     0.0f, 0.0f,
    0.5f, 0.5f, 0.0f,       1.0f, 1.0f,
    -0.5f, 0.5f, 0.0f,      0.0f, 1.0f
};

@interface GLView ()

@property (strong, nonatomic) GLKBaseEffect *effect;
@property (nonatomic) GLuint vertexArray;
@property (nonatomic) GLuint vertexBuffer;
@property (nonatomic) GLuint tex;
@property (nonatomic) cl_image clImage;
@property (nonatomic) dispatch_queue_t queue;

@end

@implementation GLView

#pragma mark - Lifecycle methods

- (void)awakeFromNib
{
    NSOpenGLPixelFormatAttribute attrs[] =
    {
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFAOpenGLProfile,
        NSOpenGLProfileVersion3_2Core,
        NSOpenGLPFAColorSize, 8,
        NSOpenGLPFAAlphaSize, 8,
        0
    };

    NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
    [self setPixelFormat:pixelFormat];

    NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
    CGLEnable([context CGLContextObj], kCGLCECrashOnRemovedFunctions);
    [self setOpenGLContext:context];
}

- (void)dealloc
{
    [self.openGLContext makeCurrentContext];

    glDeleteBuffers(1, &_vertexBuffer);
    glDeleteVertexArrays(1, &_vertexArray);

    glDeleteTextures(1, &_tex);
    gcl_release_image(self.clImage);
}

#pragma mark - NSOpenGLView methods

- (void)prepareOpenGL
{
    GLint swapInterval = 1;
    [self.openGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];

    glGenVertexArrays(1, &_vertexArray);
    glBindVertexArray(self.vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertexData), squareVertexData, GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 20, (char *)NULL + (0));

    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 20, (char *)NULL + (12));

    glBindVertexArray(0);

    self.effect = [[GLKBaseEffect alloc] init];
    self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

    glGenTextures(1, &_tex);
    glBindTexture(GL_TEXTURE_2D, self.tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    self.effect.texture2d0.name = self.tex;

    CGLShareGroupObj sharegroup = CGLGetShareGroup(self.openGLContext.CGLContextObj);
    gcl_gl_set_sharegroup(sharegroup);

    self.clImage = gcl_gl_create_image_from_texture(GL_TEXTURE_2D, 0, self.tex);

    self.queue = gcl_create_dispatch_queue(CL_DEVICE_TYPE_GPU, NULL);

    dispatch_sync(self.queue, ^{

        cl_ndrange range = {
            2,
            { 0 },
            { WIDTH, HEIGHT },
            { 0 }
        };

        mykernel_kernel(&range, self.clImage);
    });

    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(self.vertexArray);

    [self.effect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, 6);

    glBindVertexArray(0);

    [self.openGLContext flushBuffer];
}

@end

内核代码:

kernel void mykernel(write_only image2d_t image)
{
    size_t x = get_global_id(0);
    size_t y = get_global_id(1);

    uint4 color;
    color.x = 255;
    color.w = 255;

    write_imageui(image, (int2) (x,y), color);
}
4

1 回答 1

0

我不清楚你为什么要求:

    NSOpenGLPFAColorSize, 8,

这似乎不是很多颜色。如果您要求 32 位(每像素 8 位,最后加上 alpha?),它可能会更好地工作:

    NSOpenGLPFAColorSize, 32,

无论如何,这是 Scene Kit 创建的默认值。

于 2014-04-29T09:42:13.943 回答