| Flutter Impeller |

Macros | Functions
example_vk.c File Reference
`#include <assert.h>`
`#include <stdio.h>`
`#include "GLFW/glfw3.h"`
`#include "impeller.h"`

Go to the source code of this file.

## Macros
#define | GLFW_INCLUDE_VULKAN

## Functions
void | GLFWErrorCallback (int error, const char *description)
void * | ProcAddressCallback (void *vulkan_instance, const char *vulkan_proc_name, void *user_data)
int | main (int argc, char const *argv[])

## Macro Definition Documentation

## ◆ GLFW_INCLUDE_VULKAN

#define GLFW_INCLUDE_VULKAN

Definition at line 8 of file example_vk.c.

## Function Documentation

## ◆ GLFWErrorCallback()

void GLFWErrorCallback | ( | int | error,
|  | const char * | description
| ) |  |

Definition at line 12 of file example_vk.c.

12 {
13 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
14 fprintf(stderr, "GLFW Error (%d): %s\n", error, description);
15 fflush(stderr);
16 }

Referenced by main().

## ◆ main()

int main | ( | int | argc,
|  | char const * | argv[]
| ) |  |

Definition at line 24 of file example_vk.c.

24 {
25 glfwSetErrorCallback(GLFWErrorCallback);
26 [[maybe_unused]] int result = glfwInit();
27 assert(result == GLFW_TRUE);
28
29 if (!glfwVulkanSupported()) {
30 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
31 fprintf(stderr, "Vulkan is not supported on this platform.\n");
32 fflush(stderr);
33 return -1;
34 }
35
36 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
37
38 GLFWwindow* window =
39 glfwCreateWindow(800, 600, "Impeller Example (Vulkan)", NULL, NULL);
40 assert(window != NULL);
41
42 ImpellerContextVulkanSettings vulkan_settings = {};
43 vulkan_settings.proc_address_callback = &ProcAddressCallback;
44 vulkan_settings.enable_vulkan_validation = true;
45 ImpellerContext context =
46 ImpellerContextCreateVulkanNew(IMPELLER_VERSION, &vulkan_settings);
47 assert(context != NULL);
48
49 ImpellerContextVulkanInfo info = {};
50 [[maybe_unused]] bool info_result =
51 ImpellerContextGetVulkanInfo(context, &info);
52 assert(!!info_result);
53
54 if (glfwGetPhysicalDevicePresentationSupport(
55 info.vk_instance, info.vk_physical_device,
56 info.graphics_queue_family_index) != GLFW_TRUE) {
57 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
58 fprintf(stderr, "Queue does not support presentation.\n");
59 fflush(stderr);
60 return -1;
61 }
62
63 VkSurfaceKHR vulkan_surface_khr;
64 VkResult error = glfwCreateWindowSurface(info.vk_instance, window, NULL,
65 &vulkan_surface_khr);
66 if (error) {
67 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
68 fprintf(stderr, "Could not create Vulkan surface for presentation.\n");
69 fflush(stderr);
70 return -1;
71 }
72
73 int framebuffer_width, framebuffer_height;
74 glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
75 ImpellerVulkanSwapchain swapchain =
76 ImpellerVulkanSwapchainCreateNew(context, vulkan_surface_khr);
77 assert(swapchain != NULL);
78
79 ImpellerDisplayList dl = NULL;
80
81 {
82 ImpellerDisplayListBuilder builder = ImpellerDisplayListBuilderNew(NULL);
83 ImpellerPaint paint = ImpellerPaintNew();
84
85 // Clear the background to a white color.
86 ImpellerColor clear_color = {1.0, 1.0, 1.0, 1.0};
87 ImpellerPaintSetColor(paint, &clear_color);
88 ImpellerDisplayListBuilderDrawPaint(builder, paint);
89
90 // Draw a red box.
91 ImpellerColor box_color = {1.0, 0.0, 0.0, 1.0};
92 ImpellerPaintSetColor(paint, &box_color);
93 ImpellerRect box_rect = {10, 10, 100, 100};
94 ImpellerDisplayListBuilderDrawRect(builder, &box_rect, paint);
95
96 dl = ImpellerDisplayListBuilderCreateDisplayListNew(builder);
97
98 ImpellerPaintRelease(paint);
99 ImpellerDisplayListBuilderRelease(builder);
100 }
101
102 assert(dl != NULL);
103
104 while (!glfwWindowShouldClose(window)) {
105 glfwWaitEvents();
106
107 ImpellerSurface surface =
108 ImpellerVulkanSwapchainAcquireNextSurfaceNew(swapchain);
109 assert(surface != NULL);
110 ImpellerSurfaceDrawDisplayList(surface, dl);
111 ImpellerSurfacePresent(surface);
112 ImpellerSurfaceRelease(surface);
113 }
114
115 ImpellerDisplayListRelease(dl);
116 ImpellerVulkanSwapchainRelease(swapchain);
117 ImpellerContextRelease(context);
118
119 glfwMakeContextCurrent(NULL);
120
121 glfwDestroyWindow(window);
122
123 glfwTerminate();
124 return 0;
125 }
ProcAddressCallback
void * ProcAddressCallback(void *vulkan_instance, const char *vulkan_proc_name, void *user_data)
Definition: example_vk.c:18
GLFWErrorCallback
void GLFWErrorCallback(int error, const char *description)
Definition: example_vk.c:12
ImpellerPaintRelease
IMPELLER_EXPORT void ImpellerPaintRelease(ImpellerPaint IMPELLER_NULLABLE paint)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerSurfacePresent
IMPELLER_EXPORT bool ImpellerSurfacePresent(ImpellerSurface IMPELLER_NONNULL surface)
Present the surface to the underlying window system.
ImpellerSurfaceRelease
IMPELLER_EXPORT void ImpellerSurfaceRelease(ImpellerSurface IMPELLER_NULLABLE surface)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerDisplayListBuilderDrawRect
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawRect(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, const ImpellerRect *IMPELLER_NONNULL rect, ImpellerPaint IMPELLER_NONNULL paint)
Draws a rectangle.
ImpellerPaintNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerPaint IMPELLER_NULLABLE ImpellerPaintNew()
Create a new paint with default values.
ImpellerDisplayListBuilderCreateDisplayListNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayList IMPELLER_NULLABLE ImpellerDisplayListBuilderCreateDisplayListNew(ImpellerDisplayListBuilder IMPELLER_NONNULL builder)
Create a new display list using the rendering intent already encoded in the builder....
ImpellerDisplayListBuilderRelease
IMPELLER_EXPORT void ImpellerDisplayListBuilderRelease(ImpellerDisplayListBuilder IMPELLER_NULLABLE builder)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerVulkanSwapchainAcquireNextSurfaceNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerSurface IMPELLER_NULLABLE ImpellerVulkanSwapchainAcquireNextSurfaceNew(ImpellerVulkanSwapchain IMPELLER_NONNULL swapchain)
A potentially blocking operation, acquires the next surface to render to. Since this may block,...
ImpellerPaintSetColor
IMPELLER_EXPORT void ImpellerPaintSetColor(ImpellerPaint IMPELLER_NONNULL paint, const ImpellerColor *IMPELLER_NONNULL color)
Set the paint color.
ImpellerContextGetVulkanInfo
IMPELLER_EXPORT bool ImpellerContextGetVulkanInfo(ImpellerContext IMPELLER_NONNULL context, ImpellerContextVulkanInfo *IMPELLER_NONNULL out_vulkan_info)
Get internal Vulkan handles managed by the given Vulkan context. Ownership of the handles is still ma...
ImpellerDisplayListRelease
IMPELLER_EXPORT void ImpellerDisplayListRelease(ImpellerDisplayList IMPELLER_NULLABLE display_list)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
IMPELLER_VERSION
#define IMPELLER_VERSION
Definition: impeller.h:103
ImpellerSurfaceDrawDisplayList
IMPELLER_EXPORT bool ImpellerSurfaceDrawDisplayList(ImpellerSurface IMPELLER_NONNULL surface, ImpellerDisplayList IMPELLER_NONNULL display_list)
Draw a display list onto the surface. The same display list can be drawn multiple times to different ...
ImpellerDisplayListBuilderDrawPaint
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawPaint(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, ImpellerPaint IMPELLER_NONNULL paint)
Fills the current clip with the specified paint.
ImpellerVulkanSwapchainCreateNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerVulkanSwapchain IMPELLER_NULLABLE ImpellerVulkanSwapchainCreateNew(ImpellerContext IMPELLER_NONNULL context, void *IMPELLER_NONNULL vulkan_surface_khr)
Create a new Vulkan swapchain using a VkSurfaceKHR instance. Ownership of the surface is transferred ...
ImpellerContextRelease
IMPELLER_EXPORT void ImpellerContextRelease(ImpellerContext IMPELLER_NULLABLE context)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerVulkanSwapchainRelease
IMPELLER_EXPORT void ImpellerVulkanSwapchainRelease(ImpellerVulkanSwapchain IMPELLER_NULLABLE swapchain)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerDisplayListBuilderNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
ImpellerContextCreateVulkanNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerContext IMPELLER_NULLABLE ImpellerContextCreateVulkanNew(uint32_t version, const ImpellerContextVulkanSettings *IMPELLER_NONNULL settings)
Create a Vulkan context using the provided Vulkan Settings.
ImpellerColor
Definition: impeller.h:588
ImpellerContextVulkanInfo
Definition: impeller.h:614
ImpellerContextVulkanInfo::graphics_queue_family_index
uint32_t graphics_queue_family_index
Definition: impeller.h:618
ImpellerContextVulkanInfo::vk_instance
void *IMPELLER_NULLABLE vk_instance
Definition: impeller.h:615
ImpellerContextVulkanInfo::vk_physical_device
void *IMPELLER_NULLABLE vk_physical_device
Definition: impeller.h:616
ImpellerContextVulkanSettings
Definition: impeller.h:608
ImpellerContextVulkanSettings::proc_address_callback
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:610
ImpellerContextVulkanSettings::enable_vulkan_validation
bool enable_vulkan_validation
Definition: impeller.h:611
ImpellerRect
Definition: impeller.h:478

References ImpellerContextVulkanSettings::enable_vulkan_validation, GLFWErrorCallback(), ImpellerContextVulkanInfo::graphics_queue_family_index, IMPELLER_VERSION, ImpellerContextCreateVulkanNew(), ImpellerContextGetVulkanInfo(), ImpellerContextRelease(), ImpellerDisplayListBuilderCreateDisplayListNew(), ImpellerDisplayListBuilderDrawPaint(), ImpellerDisplayListBuilderDrawRect(), ImpellerDisplayListBuilderNew(), ImpellerDisplayListBuilderRelease(), ImpellerDisplayListRelease(), ImpellerPaintNew(), ImpellerPaintRelease(), ImpellerPaintSetColor(), ImpellerSurfaceDrawDisplayList(), ImpellerSurfacePresent(), ImpellerSurfaceRelease(), ImpellerVulkanSwapchainAcquireNextSurfaceNew(), ImpellerVulkanSwapchainCreateNew(), ImpellerVulkanSwapchainRelease(), ImpellerContextVulkanSettings::proc_address_callback, ProcAddressCallback(), ImpellerContextVulkanInfo::vk_instance, and ImpellerContextVulkanInfo::vk_physical_device.

## ◆ ProcAddressCallback()

void* ProcAddressCallback | ( | void * | vulkan_instance,
|  | const char * | vulkan_proc_name,
|  | void * | user_data
| ) |  |

Definition at line 18 of file example_vk.c.

20 {
21 return glfwGetInstanceProcAddress(vulkan_instance, vulkan_proc_name);
22 }

Referenced by main().