From 23023e6b7e62f70f405e13258a39de02a821ca8a Mon Sep 17 00:00:00 2001 From: pineappleEA Date: Mon, 15 Feb 2021 20:21:49 +0100 Subject: [PATCH] early-access version 1462 --- README.md | 2 +- .../hle/service/hid/controllers/gesture.cpp | 159 +++++++++++++++++- .../hle/service/hid/controllers/gesture.h | 92 +++++++++- .../host_shaders/opengl_copy_bgr16.comp | 24 ++- .../renderer_opengl/gl_texture_cache.cpp | 21 +-- .../renderer_opengl/util_shaders.cpp | 80 +++++++-- src/video_core/renderer_opengl/util_shaders.h | 19 +++ 7 files changed, 351 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 7e0ba3a70..c52ace77f 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ yuzu emulator early access ============= -This is the source code for early-access 1461. +This is the source code for early-access 1462. ## Legal Notice diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index b7b7bfeae..e7063f8ef 100755 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -5,15 +5,25 @@ #include #include "common/common_types.h" #include "core/core_timing.h" +#include "core/frontend/emu_window.h" #include "core/hle/service/hid/controllers/gesture.h" +#include "core/settings.h" namespace Service::HID { constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3BA00; +constexpr f32 angle_threshold = 0.08f; +constexpr f32 pinch_threshold = 100.0f; Controller_Gesture::Controller_Gesture(Core::System& system) : ControllerBase(system) {} Controller_Gesture::~Controller_Gesture() = default; -void Controller_Gesture::OnInit() {} +void Controller_Gesture::OnInit() { + for (std::size_t id = 0; id < MAX_FINGERS; ++id) { + mouse_finger_id[id] = MAX_FINGERS; + keyboard_finger_id[id] = MAX_FINGERS; + udp_finger_id[id] = MAX_FINGERS; + } +} void Controller_Gesture::OnRelease() {} @@ -35,10 +45,153 @@ void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; - // TODO(ogniK): Update gesture states + + // TODO(german77): Implement all gesture types + + const Input::TouchStatus& mouse_status = touch_mouse_device->GetStatus(); + const Input::TouchStatus& udp_status = touch_udp_device->GetStatus(); + for (std::size_t id = 0; id < mouse_status.size(); ++id) { + mouse_finger_id[id] = UpdateTouchInputEvent(mouse_status[id], mouse_finger_id[id]); + udp_finger_id[id] = UpdateTouchInputEvent(udp_status[id], udp_finger_id[id]); + } + + if (Settings::values.use_touch_from_button) { + const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus(); + for (std::size_t id = 0; id < mouse_status.size(); ++id) { + keyboard_finger_id[id] = + UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]); + } + } + + TouchType type = TouchType::Idle; + Attribute attributes{}; + GestureProperties gesture = GetGestureProperties(); + if (last_gesture.active_points != gesture.active_points) { + ++last_gesture.detection_count; + } + if (gesture.active_points > 0) { + if (last_gesture.active_points == 0) { + attributes.is_new_touch.Assign(true); + last_gesture.average_distance = gesture.average_distance; + last_gesture.angle = gesture.angle; + } + + type = TouchType::Touch; + if (gesture.mid_point.x != last_entry.x || gesture.mid_point.y != last_entry.y) { + type = TouchType::Pan; + } + if (std::abs(gesture.average_distance - last_gesture.average_distance) > pinch_threshold) { + type = TouchType::Pinch; + } + if (std::abs(gesture.angle - last_gesture.angle) > angle_threshold) { + type = TouchType::Rotate; + } + + cur_entry.delta_x = gesture.mid_point.x - last_entry.x; + cur_entry.delta_y = gesture.mid_point.y - last_entry.y; + // TODO: Find how velocities are calculated + cur_entry.vel_x = static_cast(cur_entry.delta_x) * 150.1f; + cur_entry.vel_y = static_cast(cur_entry.delta_y) * 150.1f; + + // Slowdown the rate of change for less flapping + last_gesture.average_distance = + (last_gesture.average_distance * 0.9f) + (gesture.average_distance * 0.1f); + last_gesture.angle = (last_gesture.angle * 0.9f) + (gesture.angle * 0.1f); + + } else { + cur_entry.delta_x = 0; + cur_entry.delta_y = 0; + cur_entry.vel_x = 0; + cur_entry.vel_y = 0; + } + last_gesture.active_points = gesture.active_points; + cur_entry.detection_count = last_gesture.detection_count; + cur_entry.type = type; + cur_entry.attributes = attributes; + cur_entry.x = gesture.mid_point.x; + cur_entry.y = gesture.mid_point.y; + cur_entry.point_count = static_cast(gesture.active_points); + for (size_t id = 0; id < MAX_POINTS; id++) { + cur_entry.points[id].x = gesture.points[id].x; + cur_entry.points[id].y = gesture.points[id].y; + } + cur_entry.rotation_angle = 0; + cur_entry.scale = 0; std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); } -void Controller_Gesture::OnLoadInputDevices() {} +void Controller_Gesture::OnLoadInputDevices() { + touch_mouse_device = Input::CreateDevice("engine:emu_window"); + touch_udp_device = Input::CreateDevice("engine:cemuhookudp"); + touch_btn_device = Input::CreateDevice("engine:touch_from_button"); +} + +std::optional Controller_Gesture::GetUnusedFingerID() const { + std::size_t first_free_id = 0; + while (first_free_id < MAX_POINTS) { + if (!fingers[first_free_id].pressed) { + return first_free_id; + } else { + first_free_id++; + } + } + return std::nullopt; +} + +std::size_t Controller_Gesture::UpdateTouchInputEvent( + const std::tuple& touch_input, std::size_t finger_id) { + const auto& [x, y, pressed] = touch_input; + if (pressed) { + if (finger_id == MAX_POINTS) { + const auto first_free_id = GetUnusedFingerID(); + if (!first_free_id) { + // Invalid finger id do nothing + return MAX_POINTS; + } + finger_id = first_free_id.value(); + fingers[finger_id].pressed = true; + } + fingers[finger_id].x = x; + fingers[finger_id].y = y; + return finger_id; + } + + if (finger_id != MAX_POINTS) { + fingers[finger_id].pressed = false; + } + + return MAX_POINTS; +} + +Controller_Gesture::GestureProperties Controller_Gesture::GetGestureProperties() { + GestureProperties gesture; + std::array active_fingers; + const auto end_iter = std::copy_if(fingers.begin(), fingers.end(), active_fingers.begin(), + [](const auto& finger) { return finger.pressed; }); + gesture.active_points = + static_cast(std::distance(active_fingers.begin(), end_iter)); + + for (size_t id = 0; id < gesture.active_points; ++id) { + gesture.points[id].x = + static_cast(active_fingers[id].x * Layout::ScreenUndocked::Width); + gesture.points[id].y = + static_cast(active_fingers[id].y * Layout::ScreenUndocked::Height); + gesture.mid_point.x += static_cast(gesture.points[id].x / gesture.active_points); + gesture.mid_point.y += static_cast(gesture.points[id].y / gesture.active_points); + } + + for (size_t id = 0; id < gesture.active_points; ++id) { + const double distance = + std::pow(static_cast(gesture.mid_point.x - gesture.points[id].x), 2) + + std::pow(static_cast(gesture.mid_point.y - gesture.points[id].y), 2); + gesture.average_distance += + static_cast(distance) / static_cast(gesture.active_points); + } + + gesture.angle = std::atan2(static_cast(gesture.mid_point.y - gesture.points[0].y), + static_cast(gesture.mid_point.x - gesture.points[0].x)); + return gesture; +} + } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index f650b8338..7ab479c9a 100755 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -5,8 +5,10 @@ #pragma once #include +#include "common/bit_field.h" #include "common/common_types.h" #include "common/swap.h" +#include "core/frontend/input.h" #include "core/hle/service/hid/controllers/controller_base.h" namespace Service::HID { @@ -28,29 +30,64 @@ public: void OnLoadInputDevices() override; private: - struct Locations { + static constexpr size_t MAX_FINGERS = 16; + static constexpr size_t MAX_POINTS = 4; + + enum class TouchType : u32 { + Idle, // Nothing touching the screen + Complete, // Unknown. End of touch? + Cancel, // Never triggered + Touch, // Pressing without movement + Press, // Never triggered + Tap, // Fast press then release + Pan, // All points moving together across the screen + Swipe, // Fast press movement and release of a single point + Pinch, // All points moving away/closer to the midpoint + Rotate, // All points rotating from the midpoint + }; + + enum class Direction : u32 { + None, + Left, + Up, + Right, + Down, + }; + + struct Attribute { + union { + u32_le raw{}; + + BitField<0, 1, u32> is_new_touch; + BitField<1, 1, u32> is_double_tap; + }; + }; + static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size"); + + struct Points { s32_le x; s32_le y; }; + static_assert(sizeof(Points) == 8, "Points is an invalid size"); struct GestureState { s64_le sampling_number; s64_le sampling_number2; s64_le detection_count; - s32_le type; - s32_le dir; + TouchType type; + Direction dir; s32_le x; s32_le y; s32_le delta_x; s32_le delta_y; f32 vel_x; f32 vel_y; - s32_le attributes; - f32 scale; - f32 rotation; - s32_le location_count; - std::array locations; + Attribute attributes; + u32 scale; + u32 rotation_angle; + s32_le point_count; + std::array points; }; static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); @@ -58,6 +95,45 @@ private: CommonHeader header; std::array gesture_states; }; + static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size"); + + struct Finger { + f32 x{}; + f32 y{}; + bool pressed{}; + }; + + struct GestureProperties { + std::array points{}; + std::size_t active_points{}; + Points mid_point{}; + s64_le detection_count{}; + u64_le delta_time{}; + float average_distance{}; + float angle{}; + }; + + // Returns an unused finger id, if there is no fingers avaliable MAX_FINGERS will be returned + std::optional GetUnusedFingerID() const; + + // If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no + // changes will be made. Updates the coordinates if the finger id it's already set. If the touch + // ends delays the output by one frame to set the end_touch flag before finally freeing the + // finger id + size_t UpdateTouchInputEvent(const std::tuple& touch_input, + size_t finger_id); + + // Returns the average distance, angle and middle point of the active fingers + GestureProperties GetGestureProperties(); + SharedMemory shared_memory{}; + std::unique_ptr touch_mouse_device; + std::unique_ptr touch_udp_device; + std::unique_ptr touch_btn_device; + std::array mouse_finger_id; + std::array keyboard_finger_id; + std::array udp_finger_id; + std::array fingers; + GestureProperties last_gesture; }; } // namespace Service::HID diff --git a/src/video_core/host_shaders/opengl_copy_bgr16.comp b/src/video_core/host_shaders/opengl_copy_bgr16.comp index fd4d01fb7..a0e9bf451 100755 --- a/src/video_core/host_shaders/opengl_copy_bgr16.comp +++ b/src/video_core/host_shaders/opengl_copy_bgr16.comp @@ -4,11 +4,27 @@ #version 430 core -layout (local_size_x = 4, local_size_y = 4) in; +layout (local_size_x = 1, local_size_y = 1) in; -layout(binding = 0, r16ui) readonly uniform uimage2D bgr_input; -layout(binding = 1, r16ui) writeonly uniform uimage2D bgr_output; +layout(binding = 0) buffer BgrImage { + uint bgr_copy[]; +}; void main() { - imageStore(bgr_output, ivec2(gl_GlobalInvocationID.xy), imageLoad(bgr_input, ivec2(gl_GlobalInvocationID.xy))); + const uint index = gl_GlobalInvocationID.y * gl_NumWorkGroups.x + gl_GlobalInvocationID.x; + const uint packed_bits = bgr_copy[index]; + uint swapped = 0; + // The buffer is packed 16-bit shorts, we need to swizzle two pixels per element + for (int i = 0; i < 2; i++) { + // R5 G6 B5 + // RRRRRGGG GGGBBBBB + const int offset = i * 16; + const uint blue = bitfieldExtract(packed_bits, offset, 5); + const uint green = bitfieldExtract(packed_bits, 5 + offset, 6); + const uint red = bitfieldExtract(packed_bits, 11 + offset, 5); + const uint temp = ((blue << 11) | (green << 5 ) | red) << offset; + swapped |= temp; + } + bgr_copy[index] = swapped; + return; } diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp index f37b290ae..29105ecad 100755 --- a/src/video_core/renderer_opengl/gl_texture_cache.cpp +++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp @@ -399,9 +399,7 @@ void AttachTexture(GLuint fbo, GLenum attachment, const ImageView* image_view) { [[nodiscard]] bool IsPixelFormatBGR(PixelFormat format) { switch (format) { - // TODO: B5G6R5 is currently not rendering after the compute copy. - // uncomment when this is resolved. - // case PixelFormat::B5G6R5_UNORM: + case PixelFormat::B5G6R5_UNORM: case PixelFormat::B8G8R8A8_UNORM: case PixelFormat::B8G8R8A8_SRGB: return true; @@ -410,16 +408,6 @@ void AttachTexture(GLuint fbo, GLenum attachment, const ImageView* image_view) { } } -[[nodiscard]] GLenum GetStorageInternalFormat(PixelFormat format) { - switch (format) { - case PixelFormat::R5G6B5_UNORM: - case PixelFormat::B5G6R5_UNORM: - return GL_RGB565; - default: - return GL_RGBA8; - } -} - } // Anonymous namespace ImageBufferMap::~ImageBufferMap() { @@ -803,8 +791,6 @@ GLuint Image::StorageHandle() noexcept { switch (info.format) { case PixelFormat::A8B8G8R8_SRGB: case PixelFormat::B8G8R8A8_SRGB: - case PixelFormat::R5G6B5_UNORM: - case PixelFormat::B5G6R5_UNORM: case PixelFormat::BC1_RGBA_SRGB: case PixelFormat::BC2_SRGB: case PixelFormat::BC3_SRGB: @@ -824,9 +810,8 @@ GLuint Image::StorageHandle() noexcept { return store_view.handle; } store_view.Create(); - glTextureView(store_view.handle, ImageTarget(info), texture.handle, - GetStorageInternalFormat(info.format), 0, info.resources.levels, 0, - info.resources.layers); + glTextureView(store_view.handle, ImageTarget(info), texture.handle, GL_RGBA8, 0, + info.resources.levels, 0, info.resources.layers); return store_view.handle; default: return texture.handle; diff --git a/src/video_core/renderer_opengl/util_shaders.cpp b/src/video_core/renderer_opengl/util_shaders.cpp index d68c04aa5..bdf175baa 100755 --- a/src/video_core/renderer_opengl/util_shaders.cpp +++ b/src/video_core/renderer_opengl/util_shaders.cpp @@ -40,7 +40,6 @@ using VideoCommon::SwizzleParameters; using VideoCommon::Accelerated::MakeBlockLinearSwizzle2DParams; using VideoCommon::Accelerated::MakeBlockLinearSwizzle3DParams; using VideoCore::Surface::BytesPerBlock; -using VideoCore::Surface::PixelFormat; namespace { @@ -286,24 +285,29 @@ void UtilShaders::CopyBGR(Image& dst_image, Image& src_image, std::span copies) { static constexpr GLuint BINDING_INPUT_IMAGE = 0; static constexpr GLuint BINDING_OUTPUT_IMAGE = 1; - - GLenum format{}; + static constexpr VideoCommon::Offset3D zero_offset{0, 0, 0}; const u32 bytes_per_block = BytesPerBlock(dst_image.info.format); if (bytes_per_block == 2) { // BGR565 Copy program_manager.BindHostCompute(copy_bgr16_program.handle); - format = GL_R16UI; + for (const ImageCopy& copy : copies) { + ASSERT(copy.src_offset == zero_offset); + ASSERT(copy.dst_offset == zero_offset); + bgr_copy_pass.Execute(dst_image, src_image, copy); + } } else if (bytes_per_block == 4) { // BGRA8 Copy program_manager.BindHostCompute(copy_bgra_program.handle); - format = GL_RGBA8; - } - for (const ImageCopy& copy : copies) { - glBindImageTexture(BINDING_INPUT_IMAGE, src_image.StorageHandle(), - copy.src_subresource.base_level, GL_FALSE, 0, GL_READ_ONLY, format); - glBindImageTexture(BINDING_OUTPUT_IMAGE, dst_image.StorageHandle(), - copy.dst_subresource.base_level, GL_FALSE, 0, GL_WRITE_ONLY, format); - glDispatchCompute(copy.extent.width, copy.extent.height, copy.extent.depth); + constexpr GLenum format = GL_RGBA8; + for (const ImageCopy& copy : copies) { + ASSERT(copy.src_offset == zero_offset); + ASSERT(copy.dst_offset == zero_offset); + glBindImageTexture(BINDING_INPUT_IMAGE, src_image.StorageHandle(), + copy.src_subresource.base_level, GL_FALSE, 0, GL_READ_ONLY, format); + glBindImageTexture(BINDING_OUTPUT_IMAGE, dst_image.StorageHandle(), + copy.dst_subresource.base_level, GL_FALSE, 0, GL_WRITE_ONLY, format); + glDispatchCompute(copy.extent.width, copy.extent.height, copy.extent.depth); + } } program_manager.RestoreGuestCompute(); } @@ -325,4 +329,56 @@ GLenum StoreFormat(u32 bytes_per_block) { return GL_R8UI; } +void Bgr565CopyPass::Execute(const Image& dst_image, const Image& src_image, + const ImageCopy& copy) { + static constexpr GLuint BINDING_INPUT_IMAGE = 0; + static constexpr GLenum format = GL_RGB565; + static constexpr GLenum target = GL_TEXTURE_2D_ARRAY; + + if (CopyBufferCreationNeeded(copy)) { + CreateNewCopyBuffer(copy, target, format); + } + // Copy from source to PBO + glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glBindBuffer(GL_PIXEL_PACK_BUFFER, bgr16_pbo.handle); + glBindTexture(target, src_image.Handle()); + glFinish(); + glPixelStorei(GL_PACK_ROW_LENGTH, copy.extent.width); + glGetTextureSubImage(src_image.Handle(), 0, 0, 0, 0, copy.extent.width, copy.extent.height, + copy.src_subresource.num_layers, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, + static_cast(bgr16_pbo_size), 0); + + // Swizzle PBO in compute shader + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_IMAGE, bgr16_pbo.handle); + glDispatchCompute(copy.extent.width, copy.extent.height, copy.extent.depth); + + // Copy from PBO to destination + glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, bgr16_pbo.handle); + glBindTexture(target, dst_image.Handle()); + glPixelStorei(GL_UNPACK_ROW_LENGTH, copy.extent.width); + glTextureSubImage3D(dst_image.Handle(), 0, 0, 0, 0, copy.extent.width, copy.extent.height, + copy.dst_subresource.num_layers, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0); + + // Unbind the buffer + glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); +} + +bool Bgr565CopyPass::CopyBufferCreationNeeded(const ImageCopy& copy) { + return bgr16_pbo_size < + (copy.extent.width * copy.extent.height * copy.src_subresource.num_layers * sizeof(u16)); +} + +void Bgr565CopyPass::CreateNewCopyBuffer(const ImageCopy& copy, GLenum target, GLuint format) { + bgr16_pbo.Release(); + bgr16_pbo.Create(); + bgr16_pbo_size = + (copy.extent.width * copy.extent.height * copy.src_subresource.num_layers * sizeof(u16)); + glBindBuffer(GL_PIXEL_PACK_BUFFER, bgr16_pbo.handle); + glBufferData(GL_PIXEL_PACK_BUFFER, bgr16_pbo_size, 0, GL_STREAM_DRAW); +} + } // namespace OpenGL diff --git a/src/video_core/renderer_opengl/util_shaders.h b/src/video_core/renderer_opengl/util_shaders.h index fc6309dc8..6456ddb9b 100755 --- a/src/video_core/renderer_opengl/util_shaders.h +++ b/src/video_core/renderer_opengl/util_shaders.h @@ -19,6 +19,23 @@ class ProgramManager; struct ImageBufferMap; +class Bgr565CopyPass { +public: + Bgr565CopyPass() = default; + ~Bgr565CopyPass() = default; + + void Execute(const Image& dst_image, const Image& src_image, + const VideoCommon::ImageCopy& copy); + +private: + OGLBuffer bgr16_pbo{}; + size_t bgr16_pbo_size{}; + + [[nodiscard]] bool CopyBufferCreationNeeded(const VideoCommon::ImageCopy& copy); + + void CreateNewCopyBuffer(const VideoCommon::ImageCopy& copy, GLenum target, GLuint format); +}; + class UtilShaders { public: explicit UtilShaders(ProgramManager& program_manager); @@ -55,6 +72,8 @@ private: OGLProgram copy_bgr16_program; OGLProgram copy_bgra_program; OGLProgram copy_bc4_program; + + Bgr565CopyPass bgr_copy_pass; }; GLenum StoreFormat(u32 bytes_per_block);