or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

algorithm-primitives.mdarray-operations.mdcore-opencl.mdindex.mdmathematical-functions.mdmemory-management.mdopengl-interop.mdrandom-number-generation.mdtools-and-utilities.md

opengl-interop.mddocs/

0

# OpenGL Interoperability

1

2

OpenGL/OpenCL interoperability enables sharing of graphics resources between OpenGL rendering and OpenCL compute contexts. This allows efficient graphics/compute workflows without costly data transfers between GPU contexts.

3

4

## Capabilities

5

6

### OpenGL Memory Objects

7

8

Create OpenCL memory objects from existing OpenGL buffers, textures, and renderbuffers for shared GPU memory access.

9

10

```python { .api }

11

class GLBuffer:

12

"""

13

OpenCL buffer created from OpenGL buffer object.

14

15

Inherits from MemoryObject with OpenGL-specific functionality.

16

"""

17

18

def __init__(self, context, flags, bufobj):

19

"""

20

Create OpenCL buffer from OpenGL buffer.

21

22

Parameters:

23

- context (Context): OpenCL context with OpenGL sharing

24

- flags (mem_flags): Memory access flags

25

- bufobj (int): OpenGL buffer object ID

26

"""

27

28

def get_gl_object_info(self):

29

"""

30

Get OpenGL object information.

31

32

Returns:

33

tuple[gl_object_type, int]: Object type and OpenGL object ID

34

"""

35

36

class GLRenderBuffer:

37

"""

38

OpenCL memory object created from OpenGL renderbuffer.

39

40

Inherits from MemoryObject for OpenGL renderbuffer sharing.

41

"""

42

43

def __init__(self, context, flags, bufobj):

44

"""

45

Create OpenCL memory object from OpenGL renderbuffer.

46

47

Parameters:

48

- context (Context): OpenCL context with OpenGL sharing

49

- flags (mem_flags): Memory access flags

50

- bufobj (int): OpenGL renderbuffer object ID

51

"""

52

53

def get_gl_object_info(self):

54

"""Get OpenGL renderbuffer information."""

55

56

class GLTexture:

57

"""

58

OpenCL image created from OpenGL texture object.

59

60

Inherits from Image with OpenGL texture sharing capabilities.

61

"""

62

63

def __init__(self, context, flags, texture_target, miplevel, texture, dims):

64

"""

65

Create OpenCL image from OpenGL texture.

66

67

Parameters:

68

- context (Context): OpenCL context with OpenGL sharing

69

- flags (mem_flags): Memory access flags

70

- texture_target (int): OpenGL texture target (GL_TEXTURE_2D, etc.)

71

- miplevel (int): Mipmap level

72

- texture (int): OpenGL texture object ID

73

- dims (int): Number of texture dimensions

74

"""

75

76

def get_gl_object_info(self):

77

"""Get OpenGL texture information."""

78

```

79

80

### OpenGL Context Synchronization

81

82

Manage synchronization between OpenGL and OpenCL operations to ensure data consistency.

83

84

```python { .api }

85

def enqueue_acquire_gl_objects(queue, mem_objects, wait_for=None):

86

"""

87

Acquire OpenGL objects for OpenCL use.

88

89

Must be called before OpenCL operations on shared OpenGL objects.

90

91

Parameters:

92

- queue (CommandQueue): OpenCL command queue

93

- mem_objects (list): List of OpenGL memory objects to acquire

94

- wait_for (list[Event], optional): Events to wait for

95

96

Returns:

97

Event: Completion event

98

"""

99

100

def enqueue_release_gl_objects(queue, mem_objects, wait_for=None):

101

"""

102

Release OpenGL objects back to OpenGL.

103

104

Must be called after OpenCL operations before OpenGL can use objects.

105

106

Parameters:

107

- queue (CommandQueue): OpenCL command queue

108

- mem_objects (list): List of OpenGL memory objects to release

109

- wait_for (list[Event], optional): Events to wait for

110

111

Returns:

112

Event: Completion event

113

"""

114

```

115

116

### OpenGL Capability Detection

117

118

Query OpenGL interoperability support and context information.

119

120

```python { .api }

121

def have_gl():

122

"""

123

Check if OpenGL interoperability is available.

124

125

Returns:

126

bool: True if OpenGL/OpenCL interop is supported

127

"""

128

129

def get_apple_cgl_share_group():

130

"""

131

Get Apple CGL share group for context creation.

132

133

macOS-specific function for OpenGL context sharing.

134

135

Returns:

136

object: CGL share group handle

137

"""

138

```

139

140

## Usage Example

141

142

```python

143

import pyopencl as cl

144

import numpy as np

145

from OpenGL.GL import *

146

147

# Create OpenCL context with OpenGL sharing

148

ctx = cl.create_some_context()

149

queue = cl.CommandQueue(ctx)

150

151

# Create OpenGL buffer

152

gl_buffer = glGenBuffers(1)

153

glBindBuffer(GL_ARRAY_BUFFER, gl_buffer)

154

glBufferData(GL_ARRAY_BUFFER, 1024, None, GL_DYNAMIC_DRAW)

155

156

# Create OpenCL buffer from OpenGL buffer

157

cl_buffer = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, gl_buffer)

158

159

# Acquire for OpenCL use

160

cl.enqueue_acquire_gl_objects(queue, [cl_buffer])

161

162

# Perform OpenCL operations

163

# ... OpenCL compute operations on cl_buffer ...

164

165

# Release back to OpenGL

166

cl.enqueue_release_gl_objects(queue, [cl_buffer])

167

168

# Now OpenGL can use the buffer

169

glBindBuffer(GL_ARRAY_BUFFER, gl_buffer)

170

# ... OpenGL rendering operations ...

171

```

172

173

## OpenGL Constants

174

175

```python { .api }

176

# OpenGL object types

177

class gl_object_type: ...

178

179

# OpenGL texture information

180

class gl_texture_info: ...

181

182

# OpenGL context properties

183

class gl_context_info: ...

184

```

185

186

OpenGL interoperability requires careful synchronization between OpenGL and OpenCL operations using acquire/release semantics to ensure data consistency across graphics and compute operations.