aboutsummaryrefslogtreecommitdiffstats
path: root/texture-share-vk.lua
blob: 3ec12504a9fbfd5423b341577f40035b073b6663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
local ffi = require 'ffi'

ffi.cdef [[
// GL/gl.h
typedef int GLint;
typedef unsigned int GLuint;
typedef unsigned int GLenum;

void glGetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
                                           GLenum pname, GLint *params);

// texture_share_ipc/texture_share_ipc.h
typedef enum ImgFormat {
    R8G8B8A8,
    R8G8B8,
    B8G8R8A8,
    B8G8R8,
    Undefined,
} ImgFormat;

typedef uint8_t ImgName[1024];

typedef struct ShmemDataInternal {
    ImgName name;
    uint32_t handle_id;
    uint32_t width;
    uint32_t height;
    enum ImgFormat format;
    uint64_t allocation_size;
    uint64_t gpu_device_uuid_0;
    uint64_t gpu_device_uuid_1;
} ShmemDataInternal;

// texture_share_gl_client/texture_share_gl_client.h
typedef enum ImageLookupResult {
    Error = -1,
    NotFound = 0,
    Found = 1,
    RequiresUpdate = 2,
} ImageLookupResult;

typedef struct ClientImageDataGuard ClientImageDataGuard;

typedef struct GlClient GlClient;

typedef struct GlImageExtent {
    GLint top_left[2];
    GLint bottom_right[2];
} GlImageExtent;

bool gl_client_initialize_external_gl(void);

struct GlClient *gl_client_new(const char *socket_path, uint64_t timeout_in_millis);
struct GlClient *gl_client_new_with_server_launch(const char *socket_path,
                                                  uint64_t client_timeout_in_millis,
                                                  const char *server_program,
                                                  const char *server_lock_path,
                                                  const char *server_socket_path,
                                                  const char *shmem_prefix,
                                                  uint64_t server_socket_timeout_in_millis,
                                                  uint64_t server_connection_wait_timeout_in_millis,
                                                  uint64_t server_ipc_timeout_in_millis,
                                                  uint64_t server_lockfile_timeout_in_millis,
                                                  uint64_t server_spawn_timeout_in_millis);

void gl_client_destroy(struct GlClient *gl_client);

enum ImageLookupResult gl_client_init_image(struct GlClient *gl_client,
                                            const char *image_name,
                                            uint32_t width,
                                            uint32_t height,
                                            ImgFormat format,
                                            bool overwrite_existing);

enum ImageLookupResult gl_client_find_image(struct GlClient *gl_client,
                                            const char *image_name,
                                            bool force_update);

struct ClientImageDataGuard *gl_client_find_image_data(struct GlClient *gl_client,
                                                       const char *image_name,
                                                       bool force_update);

const ShmemDataInternal *gl_client_image_data_guard_read(const struct ClientImageDataGuard *image_data_guard);

void gl_client_image_data_guard_destroy(struct ClientImageDataGuard *image_data_guard);

int gl_client_send_image(struct GlClient *gl_client,
                         const char *image_name,
                         GLuint src_texture_id,
                         GLenum src_texture_target,
                         bool invert,
                         GLuint prev_fbo,
                         const struct GlImageExtent *extents);

int gl_client_recv_image(struct GlClient *gl_client,
                         const char *image_name,
                         GLuint src_texture_id,
                         GLenum src_texture_target,
                         bool invert,
                         GLuint prev_fbo,
                         const struct GlImageExtent *extents);
]]

-- from texture_share_vk/config.h
local VK_SERVER_EXECUTABLE                  = "/usr/bin/texture-share-vk-server"
local VK_SERVER_DEFAULT_LOCKFILE_PATH       = "/tmp/vk_server/vk_server.lock"
local VK_SERVER_DEFAULT_SOCKET_PATH         = "/tmp/vk_server/vk_server.sock"
local VK_SERVER_DEFAULT_SHMEM_PREFIX        = "shmem_img_"

-- from GL/gl.h
local GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = 0x8CD1
local GL_COLOR_ATTACHMENT0                  = 0x8CE0
local GL_FRAMEBUFFER                        = 0x8D40
local GL_TEXTURE_2D                         = 0x0DE1

local IMAGE_FORMATS = {
  rgba8 = ffi.C.R8G8B8A8,
  rgb8 = ffi.C.R8G8B8,
  [ffi.C.R8G8B8A8] = "rgba8",
  [ffi.C.R8G8B8] = "rgb8",
}

local tvs = ffi.load("texture_share_gl_client")
local gl = ffi.load("GL")

local Client, Canvas = {}, {}
Client.__index = Client

function Client.new()
  assert(tvs.gl_client_initialize_external_gl(), "failed to init tsv OpenGL")
  local client = assert(tvs.gl_client_new_with_server_launch(
    VK_SERVER_DEFAULT_SOCKET_PATH,
    1000,
    VK_SERVER_EXECUTABLE,
    VK_SERVER_DEFAULT_LOCKFILE_PATH,
    VK_SERVER_DEFAULT_SOCKET_PATH,
    VK_SERVER_DEFAULT_SHMEM_PREFIX,
    2000,
    2000,
    2000,
    2000,
    2000
  ), "failed to connect or launch tsv server")

  return setmetatable({ client = client }, Client)
end

function Client:newSharedCanvas(name, ...)
  local canvas = love.graphics.newCanvas(...)
  local width, height = canvas:getDimensions()
  local format = assert(IMAGE_FORMATS[canvas:getFormat()], "unsupported Texture format")
  assert(
    tvs.gl_client_init_image(self.client, name, width, height, format, true) > 0,
    "failed to init image"
  )

  local last_canvas = love.graphics.getCanvas()
  love.graphics.setCanvas(canvas)

  local name_ptr = ffi.typeof('GLint[1]')()
  gl.glGetFramebufferAttachmentParameteriv(
    GL_FRAMEBUFFER,
    GL_COLOR_ATTACHMENT0,
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
    name_ptr
  )
  love.graphics.setCanvas(last_canvas)

  return setmetatable({
    name = name,
    canvas = canvas,
    client = self,
    texture_id = name_ptr[0],
  }, Canvas)
end

function Client:loadSharedCanvas(name)
  local guard = assert(
    tvs.gl_client_find_image_data(self.client, name, false),
    "failed to load image"
  )

  local data = tvs.gl_client_image_data_guard_read(guard)
  local format = assert(IMAGE_FORMATS[tonumber(data.format)], "unsupported Texture format")
  local canvas = love.graphics.newCanvas(data.width, data.height, { format = format })
  tvs.gl_client_image_data_guard_destroy(guard)

  local last_canvas = love.graphics.getCanvas()
  love.graphics.setCanvas(canvas)

  local name_ptr = ffi.typeof('GLint[1]')()
  gl.glGetFramebufferAttachmentParameteriv(
    GL_FRAMEBUFFER,
    GL_COLOR_ATTACHMENT0,
    GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
    name_ptr
  )
  love.graphics.setCanvas(last_canvas)

  return setmetatable({
    name = name,
    canvas = canvas,
    client = self,
    texture_id = name_ptr[0],
  }, Canvas)
end

function Client:__gc()
  tvs.gl_client_destroy(self)
end

function Canvas:send(fail_silently)
  local status = tvs.gl_client_send_image(
    self.client.client,
    self.name,
    self.texture_id,
    GL_TEXTURE_2D,
    false,
    0,
    nil
  )
  assert(status >= 0, "error sending image")
  assert(fail_silently or (status == 1), "send: image not found")
end

function Canvas:recv(fail_silently)
  local status = tvs.gl_client_recv_image(
    self.client.client,
    self.name,
    self.texture_id,
    GL_TEXTURE_2D,
    false,
    0,
    nil
  )
  assert(status >= 0, "error receiving image")
  assert(fail_silently or (status == 1), "recv: image not found")
end

function Canvas:__index(key)
  local val = Canvas[key]
  if val then return val end

  val = self.canvas[key]
  if val then
    Canvas[key] = function (shared, ...)
      return val(shared.canvas, ...)
    end

    return Canvas[key]
  end
end

return Client