完成在Android上渲染三角形
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = vec4(fragColor, 1.0);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,19 @@
|
||||
#version 450
|
||||
layout(location = 0) out vec3 fragColor;
|
||||
|
||||
vec2 positions[3] = vec2[](
|
||||
vec2(0.0, -0.5),
|
||||
vec2(0.5, 0.5),
|
||||
vec2(-0.5, 0.5)
|
||||
);
|
||||
|
||||
vec3 colors[3] = vec3[](
|
||||
vec3(1.0, 0.0, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec3(0.0, 0.0, 1.0)
|
||||
);
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
|
||||
fragColor = colors[gl_VertexIndex];
|
||||
}
|
||||
Binary file not shown.
@@ -4,6 +4,7 @@
|
||||
#include <game-activity/GameActivity.h>
|
||||
#include "AndroidOut.h"
|
||||
#include "Application.h"
|
||||
#include <android/asset_manager.h>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
@@ -12,6 +13,7 @@ extern "C" {
|
||||
|
||||
android_app* g_android_app = nullptr;
|
||||
Application* g_Application = nullptr;
|
||||
AAssetManager* g_assetManager = nullptr;
|
||||
|
||||
void handle_cmd(android_app *pApp, int32_t cmd) {
|
||||
switch (cmd) {
|
||||
@@ -30,6 +32,7 @@ void handle_cmd(android_app *pApp, int32_t cmd) {
|
||||
void android_main(struct android_app *pApp) {
|
||||
aout << "Welcome to android_main" << std::endl;
|
||||
g_android_app = pApp;
|
||||
g_assetManager = pApp->activity->assetManager;
|
||||
Application application;
|
||||
g_Application = &application;
|
||||
pApp->onAppCmd = handle_cmd;
|
||||
|
||||
+24
-2
@@ -1,14 +1,23 @@
|
||||
#include "AppBase.h"
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#else
|
||||
#include <android/asset_manager.h>
|
||||
extern AAssetManager* g_assetManager;
|
||||
#endif // _WIN32
|
||||
|
||||
|
||||
void VK_CHECK(VkResult ret)
|
||||
{
|
||||
assert(ret == VK_SUCCESS);
|
||||
}
|
||||
|
||||
std::vector<char> AppBase::readFile(const std::string& enginePath)
|
||||
std::vector<char> AppBase::readFile(const std::string& path)
|
||||
{
|
||||
std::vector<char> buffer;
|
||||
#ifdef _WIN32
|
||||
std::string enginePath = "app/src/main/assets/" + path;
|
||||
std::ifstream file{ enginePath, std::ios::ate | std::ios::binary };
|
||||
|
||||
if (!file.is_open())
|
||||
@@ -17,13 +26,26 @@ std::vector<char> AppBase::readFile(const std::string& enginePath)
|
||||
}
|
||||
|
||||
size_t fileSize = static_cast<size_t>(file.tellg());
|
||||
std::vector<char> buffer(fileSize);
|
||||
buffer.resize(fileSize);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(buffer.data(), fileSize);
|
||||
file.close();
|
||||
#else
|
||||
AAsset* asset = AAssetManager_open(g_assetManager, path.c_str(), AASSET_MODE_BUFFER);
|
||||
if (!asset) {
|
||||
logOut << "Failed to load file: " << path.c_str() << std::endl;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
size_t length = AAsset_getLength(asset);
|
||||
buffer.resize(length);
|
||||
AAsset_read(asset, buffer.data(), length);
|
||||
AAsset_close(asset);
|
||||
#endif // _WIN32
|
||||
return buffer;
|
||||
}
|
||||
|
||||
VkShaderModule AppBase::createShaderModule(VkDevice& device, const std::vector<char>& code)
|
||||
{
|
||||
VkShaderModuleCreateInfo createInfo{};
|
||||
|
||||
+6
-32
@@ -181,47 +181,22 @@ void Application::createLogicalDevice() {
|
||||
vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
|
||||
}
|
||||
|
||||
inline VkExtent2D choose_extent(
|
||||
VkExtent2D request_extent,
|
||||
const VkExtent2D &min_image_extent,
|
||||
const VkExtent2D &max_image_extent,
|
||||
const VkExtent2D ¤t_extent)
|
||||
{
|
||||
if (current_extent.width == 0xFFFFFFFF)
|
||||
{
|
||||
return request_extent;
|
||||
}
|
||||
|
||||
if (request_extent.width < 1 || request_extent.height < 1)
|
||||
{
|
||||
logOut << "(Swapchain) Image extent ("<< request_extent.width << ", " << request_extent.height << ") not supported. Selecting ("<< current_extent.width << ", " << current_extent.height << ")." << std::endl;
|
||||
return current_extent;
|
||||
}
|
||||
|
||||
request_extent.width = std::max(request_extent.width, min_image_extent.width);
|
||||
request_extent.width = std::min(request_extent.width, max_image_extent.width);
|
||||
|
||||
request_extent.height = std::max(request_extent.height, min_image_extent.height);
|
||||
request_extent.height = std::min(request_extent.height, max_image_extent.height);
|
||||
|
||||
return request_extent;
|
||||
}
|
||||
|
||||
void Application::createSwapChain() {
|
||||
// 选择交换链格式、颜色空间和分辨率
|
||||
VkSurfaceFormatKHR surfaceFormat = { VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
|
||||
VkSurfaceFormatKHR surfaceFormat = { VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
|
||||
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
VkExtent2D extent = { 1, 1 };
|
||||
|
||||
VkSurfaceCapabilitiesKHR surface_capabilities{};
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physicalDevice, this->surface, &surface_capabilities);
|
||||
|
||||
extent = choose_extent(extent, surface_capabilities.minImageExtent, surface_capabilities.maxImageExtent, surface_capabilities.currentExtent);
|
||||
VkExtent2D extent = surface_capabilities.currentExtent;
|
||||
|
||||
uint32_t imageCount = 2;
|
||||
// 创建交换链
|
||||
VkSwapchainCreateInfoKHR createInfo{};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
createInfo.surface = surface;
|
||||
createInfo.minImageCount = 2; // 双缓冲
|
||||
createInfo.minImageCount = imageCount; // 双缓冲
|
||||
createInfo.imageFormat = surfaceFormat.format;
|
||||
createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
||||
createInfo.imageExtent = extent;
|
||||
@@ -252,8 +227,7 @@ void Application::createSwapChain() {
|
||||
throw std::runtime_error("failed to create swap chain!");
|
||||
}
|
||||
|
||||
// 获取交换链图像
|
||||
uint32_t imageCount;
|
||||
|
||||
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
|
||||
swapChainImages.resize(imageCount);
|
||||
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
|
||||
|
||||
Reference in New Issue
Block a user