summaryrefslogtreecommitdiff
path: root/gsk/gskvulkanshader.c
blob: 1010b213041d742afaa9d7363e0888789ad8f12a (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
#include "config.h"

#include "gskvulkanshaderprivate.h"
#include "gskvulkanpipelineprivate.h"

struct _GskVulkanShader
{
  GdkVulkanContext *vulkan;

  GskVulkanShaderType type;
  VkShaderModule vk_shader;
};

static GskVulkanShader *
gsk_vulkan_shader_new_from_bytes (GdkVulkanContext     *context,
                                  GskVulkanShaderType   type,
                                  GBytes               *bytes,
                                  GError              **error)
{
  GskVulkanShader *self;
  VkShaderModule shader;
  VkResult res;

  res = GSK_VK_CHECK (vkCreateShaderModule, gdk_vulkan_context_get_device (context),
                                            &(VkShaderModuleCreateInfo) {
                                                .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
                                                .codeSize = g_bytes_get_size (bytes),
                                                .pCode = (uint32_t *) g_bytes_get_data (bytes, NULL),
                                            },
                                            NULL,
                                            &shader);
  if (res != VK_SUCCESS)
    {
      /* Someone invent better error categories plz */
      g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_UNSUPPORTED,
                   "Could not create shader: %s", gdk_vulkan_strerror (res));
      return NULL;
    }

  self = g_slice_new0 (GskVulkanShader);

  self->vulkan = g_object_ref (context);
  self->type = type;
  self->vk_shader = shader;

  return self;
}

GskVulkanShader *
gsk_vulkan_shader_new_from_resource (GdkVulkanContext     *context,
                                     GskVulkanShaderType   type,
                                     const char           *resource_name,
                                     GError              **error)
{
  GskVulkanShader *self;
  GBytes *bytes;
  GError *local_error = NULL;
  char *path;

  path = g_strconcat ("/org/gtk/libgsk/vulkan/",
                      resource_name, 
                      type == GSK_VULKAN_SHADER_VERTEX ? ".vert.spv" : ".frag.spv",
                      NULL);
  bytes = g_resources_lookup_data (path, 0, &local_error);
  g_free (path);
  if (bytes == NULL)
    {
      GSK_NOTE (VULKAN, g_printerr ("Error loading shader data: %s\n", local_error->message));
      g_propagate_error (error, local_error);
      return NULL;
    }

  self = gsk_vulkan_shader_new_from_bytes (context, type, bytes, error);
  g_bytes_unref (bytes);

  return self;
}

void
gsk_vulkan_shader_free (GskVulkanShader *self)
{
  vkDestroyShaderModule (gdk_vulkan_context_get_device (self->vulkan),
                         self->vk_shader,
                         NULL);

  g_object_unref (self->vulkan);

  g_slice_free (GskVulkanShader, self);
}

GskVulkanShaderType
gsk_vulkan_shader_get_type (GskVulkanShader *shader)
{
  return shader->type;
}

VkShaderModule
gsk_vulkan_shader_get_module (GskVulkanShader *shader)
{
  return shader->vk_shader;
}