Flutter Engine
 
Loading...
Searching...
No Matches
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}
const uint8_t uint32_t uint32_t GError ** error

References error.

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 = {};
44 vulkan_settings.enable_vulkan_validation = true;
45 ImpellerContext context =
47 assert(context != NULL);
48
50 [[maybe_unused]] bool info_result =
51 ImpellerContextGetVulkanInfo(context, &info);
52 assert(!!info_result);
53
54 if (glfwGetPhysicalDevicePresentationSupport(
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);
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
97
100 }
101
102 assert(dl != NULL);
103
104 while (!glfwWindowShouldClose(window)) {
105 glfwWaitEvents();
106
107 ImpellerSurface surface =
109 assert(surface != NULL);
111 ImpellerSurfacePresent(surface);
112 ImpellerSurfaceRelease(surface);
113 }
114
117 ImpellerContextRelease(context);
118
119 glfwMakeContextCurrent(NULL);
120
121 glfwDestroyWindow(window);
122
123 glfwTerminate();
124 return 0;
125}
void GLFWErrorCallback(int error, const char *description)
Definition example_vk.c:12
void * ProcAddressCallback(void *vulkan_instance, const char *vulkan_proc_name, void *user_data)
Definition example_vk.c:18
VkSwapchainKHR swapchain
Definition main.cc:80
GLFWwindow * window
Definition main.cc:60
VkSurfaceKHR surface
Definition main.cc:65
#define GLFW_TRUE
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...
IMPELLER_EXPORT bool ImpellerSurfacePresent(ImpellerSurface IMPELLER_NONNULL surface)
Present the surface to the underlying window system.
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...
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawRect(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, const ImpellerRect *IMPELLER_NONNULL rect, ImpellerPaint IMPELLER_NONNULL paint)
Draws a rectangle.
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerPaint IMPELLER_NULLABLE ImpellerPaintNew()
Create a new paint with default values.
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....
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...
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,...
IMPELLER_EXPORT void ImpellerPaintSetColor(ImpellerPaint IMPELLER_NONNULL paint, const ImpellerColor *IMPELLER_NONNULL color)
Set the paint color.
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...
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...
#define IMPELLER_VERSION
Definition impeller.h:103
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 ...
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawPaint(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, ImpellerPaint IMPELLER_NONNULL paint)
Fills the current clip with the specified paint.
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 ...
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...
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...
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
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.
uint32_t graphics_queue_family_index
Definition impeller.h:642
void *IMPELLER_NULLABLE vk_instance
Definition impeller.h:639
void *IMPELLER_NULLABLE vk_physical_device
Definition impeller.h:640
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition impeller.h:634

References ImpellerContextVulkanSettings::enable_vulkan_validation, error, GLFW_TRUE, 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(), surface, swapchain, ImpellerContextVulkanInfo::vk_instance, ImpellerContextVulkanInfo::vk_physical_device, and window.

◆ 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().