فهرست منبع

Format SDL_GPU backend

Michael Ragazzon 4 ماه پیش
والد
کامیت
21aa47e5b4

+ 158 - 158
Backends/RmlUi_Backend_SDL_GPU.cpp

@@ -39,209 +39,209 @@
     Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  */
 struct BackendData {
-    BackendData(SDL_GPUDevice* device, SDL_Window* window) : render_interface(device, window) {}
+	BackendData(SDL_GPUDevice* device, SDL_Window* window) : render_interface(device, window) {}
 
-    SystemInterface_SDL system_interface;
-    RenderInterface_SDL_GPU render_interface;
+	SystemInterface_SDL system_interface;
+	RenderInterface_SDL_GPU render_interface;
 
-    SDL_Window* window = nullptr;
-    SDL_GPUDevice* device = nullptr;
-    SDL_GPUCommandBuffer* command_buffer = nullptr;
+	SDL_Window* window = nullptr;
+	SDL_GPUDevice* device = nullptr;
+	SDL_GPUCommandBuffer* command_buffer = nullptr;
 
-    bool running = true;
+	bool running = true;
 };
 static Rml::UniquePtr<BackendData> data;
 
 bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
 {
-    RMLUI_ASSERT(!data);
-
-    if (!SDL_Init(SDL_INIT_VIDEO))
-        return false;
-
-    SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
-
-    const float window_size_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
-    SDL_PropertiesID props = SDL_CreateProperties();
-    SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, window_name);
-    SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
-    SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
-    SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, int(width * window_size_scale));
-    SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, int(height * window_size_scale));
-    SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, allow_resize);
-    SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
-    SDL_Window* window = SDL_CreateWindowWithProperties(props);
-    SDL_DestroyProperties(props);
-
-    if (!window)
-    {
-        Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s\n", SDL_GetError());
-        return false;
-    }
-
-    props = SDL_CreateProperties();
-    SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, true);
-    SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, true);
-    SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN, true);
+	RMLUI_ASSERT(!data);
+
+	if (!SDL_Init(SDL_INIT_VIDEO))
+		return false;
+
+	SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
+
+	const float window_size_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+	SDL_PropertiesID props = SDL_CreateProperties();
+	SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, window_name);
+	SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
+	SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
+	SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, int(width * window_size_scale));
+	SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, int(height * window_size_scale));
+	SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, allow_resize);
+	SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
+	SDL_Window* window = SDL_CreateWindowWithProperties(props);
+	SDL_DestroyProperties(props);
+
+	if (!window)
+	{
+		Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s\n", SDL_GetError());
+		return false;
+	}
+
+	props = SDL_CreateProperties();
+	SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, true);
+	SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, true);
+	SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN, true);
 #ifdef RMLUI_DEBUG
-    SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, true);
+	SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, true);
 #endif
-    SDL_GPUDevice* device = SDL_CreateGPUDeviceWithProperties(props);
-    SDL_DestroyProperties(props);
+	SDL_GPUDevice* device = SDL_CreateGPUDeviceWithProperties(props);
+	SDL_DestroyProperties(props);
 
-    if (!device)
-        return false;
+	if (!device)
+		return false;
 
-    if (!SDL_ClaimWindowForGPUDevice(device, window))
-        return false;
+	if (!SDL_ClaimWindowForGPUDevice(device, window))
+		return false;
 
-    data = Rml::MakeUnique<BackendData>(device, window);
-    data->window = window;
-    data->device = device;
-    data->system_interface.SetWindow(window);
+	data = Rml::MakeUnique<BackendData>(device, window);
+	data->window = window;
+	data->device = device;
+	data->system_interface.SetWindow(window);
 
-    const char* renderer_name = SDL_GetGPUDeviceDriver(device);
-    data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Using SDL device driver: %s", renderer_name));
+	const char* renderer_name = SDL_GetGPUDeviceDriver(device);
+	data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Using SDL device driver: %s", renderer_name));
 
-    return true;
+	return true;
 }
 
 void Backend::Shutdown()
 {
-    RMLUI_ASSERT(data);
+	RMLUI_ASSERT(data);
 
-    data->render_interface.Shutdown();
+	data->render_interface.Shutdown();
 
-    SDL_ReleaseWindowFromGPUDevice(data->device, data->window);
-    SDL_DestroyGPUDevice(data->device);
-    SDL_DestroyWindow(data->window);
+	SDL_ReleaseWindowFromGPUDevice(data->device, data->window);
+	SDL_DestroyGPUDevice(data->device);
+	SDL_DestroyWindow(data->window);
 
-    data.reset();
+	data.reset();
 
-    SDL_Quit();
+	SDL_Quit();
 }
 
 Rml::SystemInterface* Backend::GetSystemInterface()
 {
-    RMLUI_ASSERT(data);
-    return &data->system_interface;
+	RMLUI_ASSERT(data);
+	return &data->system_interface;
 }
 
 Rml::RenderInterface* Backend::GetRenderInterface()
 {
-    RMLUI_ASSERT(data);
-    return &data->render_interface;
+	RMLUI_ASSERT(data);
+	return &data->render_interface;
 }
 
 bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
 {
-    RMLUI_ASSERT(data && context);
-
-    auto GetKey = [](const SDL_Event& event) { return event.key.key; };
-    auto GetDisplayScale = []() { return SDL_GetWindowDisplayScale(data->window); };
-    constexpr auto event_quit = SDL_EVENT_QUIT;
-    constexpr auto event_key_down = SDL_EVENT_KEY_DOWN;
-    bool has_event = false;
-
-    bool result = data->running;
-    data->running = true;
-
-    SDL_Event ev;
-    if (power_save)
-        has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
-    else
-        has_event = SDL_PollEvent(&ev);
-
-    while (has_event)
-    {
-        bool propagate_event = true;
-        switch (ev.type)
-        {
-        case event_quit:
-        {
-            propagate_event = false;
-            result = false;
-        }
-        break;
-        case event_key_down:
-        {
-            propagate_event = false;
-            const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(GetKey(ev));
-            const int key_modifier = RmlSDL::GetKeyModifierState();
-            const float native_dp_ratio = GetDisplayScale();
-
-            // See if we have any global shortcuts that take priority over the context.
-            if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
-                break;
-            // Otherwise, hand the event over to the context by calling the input handler as normal.
-            if (!RmlSDL::InputEventHandler(context, data->window, ev))
-                break;
-            // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
-            if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
-                break;
-        }
-        break;
-        default: break;
-        }
-
-        if (propagate_event)
-            RmlSDL::InputEventHandler(context, data->window, ev);
-
-        has_event = SDL_PollEvent(&ev);
-    }
-
-    return result;
+	RMLUI_ASSERT(data && context);
+
+	auto GetKey = [](const SDL_Event& event) { return event.key.key; };
+	auto GetDisplayScale = []() { return SDL_GetWindowDisplayScale(data->window); };
+	constexpr auto event_quit = SDL_EVENT_QUIT;
+	constexpr auto event_key_down = SDL_EVENT_KEY_DOWN;
+	bool has_event = false;
+
+	bool result = data->running;
+	data->running = true;
+
+	SDL_Event ev;
+	if (power_save)
+		has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
+	else
+		has_event = SDL_PollEvent(&ev);
+
+	while (has_event)
+	{
+		bool propagate_event = true;
+		switch (ev.type)
+		{
+		case event_quit:
+		{
+			propagate_event = false;
+			result = false;
+		}
+		break;
+		case event_key_down:
+		{
+			propagate_event = false;
+			const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(GetKey(ev));
+			const int key_modifier = RmlSDL::GetKeyModifierState();
+			const float native_dp_ratio = GetDisplayScale();
+
+			// See if we have any global shortcuts that take priority over the context.
+			if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
+				break;
+			// Otherwise, hand the event over to the context by calling the input handler as normal.
+			if (!RmlSDL::InputEventHandler(context, data->window, ev))
+				break;
+			// The key was not consumed by the context either, try keyboard shortcuts of lower priority.
+			if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
+				break;
+		}
+		break;
+		default: break;
+		}
+
+		if (propagate_event)
+			RmlSDL::InputEventHandler(context, data->window, ev);
+
+		has_event = SDL_PollEvent(&ev);
+	}
+
+	return result;
 }
 
 void Backend::RequestExit()
 {
-    RMLUI_ASSERT(data);
-    data->running = false;
+	RMLUI_ASSERT(data);
+	data->running = false;
 }
 
 void Backend::BeginFrame()
 {
-    RMLUI_ASSERT(data);
-
-    data->command_buffer = SDL_AcquireGPUCommandBuffer(data->device);
-    if (!data->command_buffer)
-    {
-        Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to acquire command buffer: %s", SDL_GetError());
-        return;
-    }
-
-    SDL_GPUTexture* swapchain_texture;
-    uint32_t width;
-    uint32_t height;
-    if (!SDL_WaitAndAcquireGPUSwapchainTexture(data->command_buffer, data->window, &swapchain_texture, &width, &height))
-    {
-        Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to acquire swapchain texture: %s", SDL_GetError());
-        return;
-    }
-
-    if (!swapchain_texture || !width || !height)
-    {
-        // Not an error. Happens on minimize
-        SDL_CancelGPUCommandBuffer(data->command_buffer);
-        return;
-    }
-
-    // Do your normal draw operations (make sure you clear the swapchain texture)
-    SDL_GPUColorTargetInfo color_info{};
-    color_info.texture = swapchain_texture;
-    color_info.load_op = SDL_GPU_LOADOP_CLEAR;
-    color_info.store_op = SDL_GPU_STOREOP_STORE;
-    SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(data->command_buffer, &color_info, 1, nullptr);
-    SDL_EndGPURenderPass(render_pass);
-
-    data->render_interface.BeginFrame(data->command_buffer, swapchain_texture, width, height);
+	RMLUI_ASSERT(data);
+
+	data->command_buffer = SDL_AcquireGPUCommandBuffer(data->device);
+	if (!data->command_buffer)
+	{
+		Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to acquire command buffer: %s", SDL_GetError());
+		return;
+	}
+
+	SDL_GPUTexture* swapchain_texture;
+	uint32_t width;
+	uint32_t height;
+	if (!SDL_WaitAndAcquireGPUSwapchainTexture(data->command_buffer, data->window, &swapchain_texture, &width, &height))
+	{
+		Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to acquire swapchain texture: %s", SDL_GetError());
+		return;
+	}
+
+	if (!swapchain_texture || !width || !height)
+	{
+		// Not an error. Happens on minimize
+		SDL_CancelGPUCommandBuffer(data->command_buffer);
+		return;
+	}
+
+	// Do your normal draw operations (make sure you clear the swapchain texture)
+	SDL_GPUColorTargetInfo color_info{};
+	color_info.texture = swapchain_texture;
+	color_info.load_op = SDL_GPU_LOADOP_CLEAR;
+	color_info.store_op = SDL_GPU_STOREOP_STORE;
+	SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(data->command_buffer, &color_info, 1, nullptr);
+	SDL_EndGPURenderPass(render_pass);
+
+	data->render_interface.BeginFrame(data->command_buffer, swapchain_texture, width, height);
 }
 
 void Backend::PresentFrame()
 {
-    RMLUI_ASSERT(data);
-    data->render_interface.EndFrame();
+	RMLUI_ASSERT(data);
+	data->render_interface.EndFrame();
 
-    SDL_SubmitGPUCommandBuffer(data->command_buffer);
-    data->command_buffer = nullptr;
-}
+	SDL_SubmitGPUCommandBuffer(data->command_buffer);
+	data->command_buffer = nullptr;
+}

+ 2 - 2
Backends/RmlUi_Renderer_SDL_GPU.cpp

@@ -210,7 +210,7 @@ void RenderInterface_SDL_GPU::Shutdown()
 		SDL_ReleaseGPUTransferBuffer(device, buffer->transfer_buffer);
 		SDL_ReleaseGPUBuffer(device, buffer->buffer);
 	}
-    SDL_ReleaseGPUSampler(device, linear_sampler);
+	SDL_ReleaseGPUSampler(device, linear_sampler);
 	SDL_ReleaseGPUGraphicsPipeline(device, color_pipeline);
 	SDL_ReleaseGPUGraphicsPipeline(device, texture_pipeline);
 }
@@ -655,4 +655,4 @@ RenderInterface_SDL_GPU::Buffer* RenderInterface_SDL_GPU::RequestBuffer(int capa
 	buffer->capacity = capacity;
 	auto inserted_it = buffers.insert(it, std::move(buffer));
 	return inserted_it->get();
-}
+}

+ 1 - 1
Backends/RmlUi_Renderer_SDL_GPU.h

@@ -149,4 +149,4 @@ private:
 	Buffer* RequestBuffer(int capacity, SDL_GPUBufferUsageFlags usage);
 };
 
-#endif
+#endif

+ 10 - 10
Backends/RmlUi_SDL_GPU/compile_shaders.py

@@ -1,20 +1,20 @@
 # This source file is part of RmlUi, the HTML/CSS Interface Middleware
-# 
+#
 # For the latest information, see http://github.com/mikke89/RmlUi
-# 
+#
 # Copyright (c) 2008-2014 CodePoint Ltd, Shift Technology Ltd, and contributors
 # Copyright (c) 2019-2023 The RmlUi Team, and contributors
-# 
+#
 # Permission is hereby granted, free of charge, to any person obtaining a copy
 # of this software and associated documentation files (the "Software"), to deal
 # in the Software without restriction, including without limitation the rights
 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 # copies of the Software, and to permit persons to whom the Software is
 # furnished to do so, subject to the following conditions:
-# 
+#
 # The above copyright notice and this permission notice shall be included in all
 # copies or substantial portions of the Software.
-# 
+#
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -54,11 +54,11 @@ with open(out_path,'w') as result_file:
 				temp_path = os.path.join(current_dir, ".temp.{}".format(target))
 
 				print(temp_path)
-			
+
 				subprocess.run([shadercross_path, "-s", "hlsl", shader_path, "-o", temp_path, "-d", target], check = True)
-			
+
 				print("Success, writing output to variable '{}' in {}".format(variable_name, out_file))
-		
+
 				i = 0
 				result_file.write('\nalignas(uint32_t) static const unsigned char {}[] = {{'.format(variable_name))
 				for b in open(temp_path, 'rb').read():
@@ -66,9 +66,9 @@ with open(out_path,'w') as result_file:
 						result_file.write('\n\t')
 					result_file.write('0x%02X,' % b)
 					i += 1
-				
+
 				result_file.write('\n};\n')
-				
+
 				os.remove(temp_path)
 
 			compile("spirv")

+ 11 - 11
Backends/RmlUi_Vulkan/compile_shaders.py

@@ -1,20 +1,20 @@
 # This source file is part of RmlUi, the HTML/CSS Interface Middleware
-# 
+#
 # For the latest information, see http://github.com/mikke89/RmlUi
-# 
+#
 # Copyright (c) 2008-2014 CodePoint Ltd, Shift Technology Ltd, and contributors
 # Copyright (c) 2019-2023 The RmlUi Team, and contributors
-# 
+#
 # Permission is hereby granted, free of charge, to any person obtaining a copy
 # of this software and associated documentation files (the "Software"), to deal
 # in the Software without restriction, including without limitation the rights
 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 # copies of the Software, and to permit persons to whom the Software is
 # furnished to do so, subject to the following conditions:
-# 
+#
 # The above copyright notice and this permission notice shall be included in all
 # copies or substantial portions of the Software.
-# 
+#
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -43,13 +43,13 @@ with open(out_path,'w') as result_file:
 		if file.endswith(".vert") or file.endswith(".frag"):
 			shader_path = os.path.join(current_dir, file)
 			variable_name = os.path.splitext(file)[0]
-			
+
 			print("Compiling '{}' to SPIR-V using glslc.".format(file))
-			
+
 			subprocess.run(["glslc", shader_path, "-o", temp_spirv_path], check = True)
-			
+
 			print("Success, writing output to variable '{}' in {}".format(variable_name, out_file))
-		
+
 			i = 0
 			result_file.write('\nalignas(uint32_t) static const unsigned char {}[] = {{'.format(variable_name))
 			for b in open(temp_spirv_path, 'rb').read():
@@ -57,7 +57,7 @@ with open(out_path,'w') as result_file:
 					result_file.write('\n\t')
 				result_file.write('0x%02X,' % b)
 				i += 1
-				
+
 			result_file.write('\n};\n')
-			
+
 			os.remove(temp_spirv_path)