diff options
author | Lenny Maiorani <lenny@serenityos.org> | 2022-03-13 16:09:41 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-18 19:58:57 +0100 |
commit | f912a48315c425ff49fb9421f58ea5bb834a96d6 (patch) | |
tree | 3a07abae47052211701d97e0246d62f162adb295 /Userland/Demos/VirGLDemo | |
parent | 31515a9147459d29f871d8dedfdc9a4072a9900d (diff) | |
download | serenity-f912a48315c425ff49fb9421f58ea5bb834a96d6.zip |
Userland: Change static const variables to static constexpr
`static const` variables can be computed and initialized at run-time
during initialization or the first time a function is called. Change
them to `static constexpr` to ensure they are computed at
compile-time.
This allows some removal of `strlen` because the length of the
`StringView` can be used which is pre-computed at compile-time.
Diffstat (limited to 'Userland/Demos/VirGLDemo')
-rw-r--r-- | Userland/Demos/VirGLDemo/CommandBufferBuilder.cpp | 6 | ||||
-rw-r--r-- | Userland/Demos/VirGLDemo/CommandBufferBuilder.h | 4 | ||||
-rw-r--r-- | Userland/Demos/VirGLDemo/VirGLDemo.cpp | 45 |
3 files changed, 31 insertions, 24 deletions
diff --git a/Userland/Demos/VirGLDemo/CommandBufferBuilder.cpp b/Userland/Demos/VirGLDemo/CommandBufferBuilder.cpp index 8d7b8627db..8f47c7cd8a 100644 --- a/Userland/Demos/VirGLDemo/CommandBufferBuilder.cpp +++ b/Userland/Demos/VirGLDemo/CommandBufferBuilder.cpp @@ -1,9 +1,11 @@ /* * Copyright (c) 2022, Sahan Fernando <sahan.h.fernando@gmail.com> + * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/StringView.h> #include <Kernel/API/VirGL.h> #include <sys/ioctl_numbers.h> @@ -235,9 +237,9 @@ void CommandBufferBuilder::append_set_constant_buffer(Vector<float> const& const } } -void CommandBufferBuilder::append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, const char* shader_data) +void CommandBufferBuilder::append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data) { - size_t shader_len = strlen(shader_data) + 1; // Need to remember to copy null terminator as well if needed + size_t shader_len = shader_data.length() + 1; // Need to remember to copy null terminator as well if needed CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, to_underlying(Protocol::ObjectType::SHADER)); builder.appendu32(handle.value()); // VIRGL_OBJ_CREATE_HANDLE builder.appendu32(to_underlying(shader_type)); diff --git a/Userland/Demos/VirGLDemo/CommandBufferBuilder.h b/Userland/Demos/VirGLDemo/CommandBufferBuilder.h index 8be91f9f35..34250d7f5b 100644 --- a/Userland/Demos/VirGLDemo/CommandBufferBuilder.h +++ b/Userland/Demos/VirGLDemo/CommandBufferBuilder.h @@ -1,11 +1,13 @@ /* * Copyright (c) 2022, Sahan Fernando <sahan.h.fernando@gmail.com> + * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include <AK/StringView.h> #include <AK/Vector.h> #include <sys/ioctl_numbers.h> @@ -28,7 +30,7 @@ public: void append_gl_viewport(); void append_set_framebuffer_state_no_attach(); void append_set_constant_buffer(Vector<float> const& constant_buffer); - void append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, const char* shader_data); + void append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data); void append_bind_shader(ObjectHandle handle, Gallium::ShaderType shader_type); void append_create_rasterizer(ObjectHandle handle); void append_bind_rasterizer(ObjectHandle handle); diff --git a/Userland/Demos/VirGLDemo/VirGLDemo.cpp b/Userland/Demos/VirGLDemo/VirGLDemo.cpp index 97d1a7081f..3535055f64 100644 --- a/Userland/Demos/VirGLDemo/VirGLDemo.cpp +++ b/Userland/Demos/VirGLDemo/VirGLDemo.cpp @@ -1,10 +1,13 @@ /* * Copyright (c) 2022, Sahan Fernando <sahan.h.fernando@gmail.com> + * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/Array.h> #include <AK/String.h> +#include <AK/StringView.h> #include <AK/Vector.h> #include <Kernel/API/VirGL.h> #include <LibGUI/Application.h> @@ -23,26 +26,26 @@ #include "VirGLProtocol.h" #include "Widget.h" -static const char* frag_shader = "FRAG\n" - "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n" - "DCL IN[0], COLOR, COLOR\n" - "DCL OUT[0], COLOR\n" - " 0: MOV OUT[0], IN[0]\n" - " 1: END\n"; +static constexpr StringView frag_shader = "FRAG\n" + "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n" + "DCL IN[0], COLOR, COLOR\n" + "DCL OUT[0], COLOR\n" + " 0: MOV OUT[0], IN[0]\n" + " 1: END\n"; -static const char* vert_shader = "VERT\n" - "DCL IN[0]\n" - "DCL IN[1]\n" - "DCL OUT[0], POSITION\n" - "DCL OUT[1], COLOR\n" - "DCL CONST[0..3]\n" - "DCL TEMP[0..1]\n" - " 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n" - " 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n" - " 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n" - " 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n" - " 4: MOV_SAT OUT[1], IN[1]\n" - " 5: END\n"; +static constexpr StringView vert_shader = "VERT\n" + "DCL IN[0]\n" + "DCL IN[1]\n" + "DCL OUT[0], POSITION\n" + "DCL OUT[1], COLOR\n" + "DCL CONST[0..3]\n" + "DCL TEMP[0..1]\n" + " 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n" + " 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n" + " 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n" + " 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n" + " 4: MOV_SAT OUT[1], IN[1]\n" + " 5: END\n"; struct VertexData { float r; @@ -92,7 +95,7 @@ static ResourceID create_virgl_resource(VirGL3DResourceSpec& spec) static Vector<VertexData> gen_vertex_data() { Vector<VertexData> data; - static const VertexData vertices[8] = { + static constexpr Array<VertexData, 8> vertices = { VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = -0.5, .z = -0.5 }, VertexData { .r = 0, .g = 0, .b = 0, .x = 0.5, .y = -0.5, .z = -0.5 }, VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = 0.5, .z = -0.5 }, @@ -102,7 +105,7 @@ static Vector<VertexData> gen_vertex_data() VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = 0.5, .z = 0.5 }, VertexData { .r = 0, .g = 0, .b = 0, .x = 0.5, .y = 0.5, .z = 0.5 }, }; - size_t tris[36] = { + static constexpr Array<size_t, 36> tris = { 0, 1, 2, 1, 3, 2, // Top 4, 0, 6, 0, 2, 6, // Left 4, 5, 0, 5, 1, 0, // Up |