Compare commits
3
Commits
fa787d1751
...
e8cf1c6a2f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8cf1c6a2f | ||
|
|
80ff2a091a | ||
|
|
bf99aeb1db |
@@ -17,7 +17,10 @@ import android.widget.FrameLayout;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.camera.core.AspectRatio;
|
||||||
import androidx.camera.core.CameraSelector;
|
import androidx.camera.core.CameraSelector;
|
||||||
|
import androidx.camera.core.ImageAnalysis;
|
||||||
|
import androidx.camera.core.ImageProxy;
|
||||||
import androidx.camera.core.Preview;
|
import androidx.camera.core.Preview;
|
||||||
import androidx.camera.core.SurfaceRequest;
|
import androidx.camera.core.SurfaceRequest;
|
||||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||||
@@ -26,38 +29,16 @@ import androidx.core.content.ContextCompat;
|
|||||||
|
|
||||||
import com.google.androidgamesdk.GameActivity;
|
import com.google.androidgamesdk.GameActivity;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class MainActivity extends GameActivity {
|
public class MainActivity extends GameActivity {
|
||||||
private TextureView textureView; // 使用 TextureView 替代 PreviewView
|
|
||||||
private ProcessCameraProvider cameraProvider;
|
private ProcessCameraProvider cameraProvider;
|
||||||
private FrameLayout containerLayout;
|
|
||||||
private void initCamera(){
|
private void initCamera(){
|
||||||
// 获取 GameActivity 的根视图
|
backgroundExecutor = Executors.newSingleThreadExecutor();
|
||||||
ViewGroup rootView = (ViewGroup) getWindow().getDecorView();
|
|
||||||
|
|
||||||
// 创建容器布局
|
|
||||||
containerLayout = new FrameLayout(this);
|
|
||||||
containerLayout.setLayoutParams(new ViewGroup.LayoutParams(
|
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT
|
|
||||||
));
|
|
||||||
|
|
||||||
// 创建 TextureView 用于摄像头预览
|
|
||||||
textureView = new TextureView(this);
|
|
||||||
textureView.setLayoutParams(new ViewGroup.LayoutParams(
|
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT
|
|
||||||
));
|
|
||||||
|
|
||||||
// 将 TextureView 添加到容器
|
|
||||||
containerLayout.addView(textureView);
|
|
||||||
|
|
||||||
// 将容器添加到根视图的底部(作为背景)
|
|
||||||
//rootView.addView(containerLayout); // 添加到索引0(最底层)
|
|
||||||
rootView.addView(containerLayout, 0);
|
|
||||||
|
|
||||||
|
|
||||||
if (hasCameraPermission()) {
|
if (hasCameraPermission()) {
|
||||||
startCamera();
|
startCamera();
|
||||||
} else {
|
} else {
|
||||||
@@ -76,7 +57,7 @@ public class MainActivity extends GameActivity {
|
|||||||
}
|
}
|
||||||
String[] argArray = new String[2];
|
String[] argArray = new String[2];
|
||||||
argArray[0] = "sample";
|
argArray[0] = "sample";
|
||||||
argArray[1] = "hello_triangle";
|
argArray[1] = "texture_loading";//"hello_triangle";
|
||||||
sendArgumentsToPlatform(argArray);
|
sendArgumentsToPlatform(argArray);
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
@@ -97,6 +78,8 @@ public class MainActivity extends GameActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final int REQUEST_CAMERA_PERMISSION = 1001;
|
private static final int REQUEST_CAMERA_PERMISSION = 1001;
|
||||||
|
ImageAnalysis imageAnalyzer;
|
||||||
|
ExecutorService backgroundExecutor;
|
||||||
|
|
||||||
private void startCamera() {
|
private void startCamera() {
|
||||||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
|
ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
|
||||||
@@ -106,26 +89,32 @@ public class MainActivity extends GameActivity {
|
|||||||
try {
|
try {
|
||||||
cameraProvider = cameraProviderFuture.get();
|
cameraProvider = cameraProviderFuture.get();
|
||||||
|
|
||||||
Preview preview = new Preview.Builder().build();
|
|
||||||
|
|
||||||
CameraSelector cameraSelector = new CameraSelector.Builder()
|
CameraSelector cameraSelector = new CameraSelector.Builder()
|
||||||
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
|
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// 使用 TextureView 的 SurfaceTexture
|
imageAnalyzer = new ImageAnalysis.Builder()
|
||||||
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
|
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
|
||||||
if (surfaceTexture != null) {
|
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
||||||
Surface surface = new Surface(surfaceTexture);
|
.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
|
||||||
preview.setSurfaceProvider(new Preview.SurfaceProvider() {
|
.build();
|
||||||
@Override
|
|
||||||
public void onSurfaceRequested(@NonNull SurfaceRequest request) {
|
|
||||||
request.provideSurface(surface, ContextCompat.getMainExecutor(MainActivity.this),
|
|
||||||
result -> {});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
cameraProvider.bindToLifecycle(this, cameraSelector, preview);
|
|
||||||
|
imageAnalyzer.setAnalyzer(backgroundExecutor, new ImageAnalysis.Analyzer() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void analyze(@NonNull ImageProxy image) {
|
||||||
|
try {
|
||||||
|
Log.d("Camera", "Received image: " + image.getWidth() + "x" + image.getHeight());
|
||||||
|
processImageForVulkan(image);
|
||||||
|
} finally {
|
||||||
|
image.close(); // 确保在这里关闭
|
||||||
|
Log.d("Camera", "Image closed, ready for next frame");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalyzer);
|
||||||
|
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
Log.e("MainActivity", "Error starting camera: " + e.getMessage());
|
Log.e("MainActivity", "Error starting camera: " + e.getMessage());
|
||||||
@@ -133,6 +122,33 @@ public class MainActivity extends GameActivity {
|
|||||||
}, ContextCompat.getMainExecutor(this));
|
}, ContextCompat.getMainExecutor(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void processImageForVulkan(ImageProxy image) {
|
||||||
|
// 获取图像信息
|
||||||
|
int width = image.getWidth();
|
||||||
|
int height = image.getHeight();
|
||||||
|
int format = image.getFormat();
|
||||||
|
|
||||||
|
// 获取图像数据平面
|
||||||
|
ImageProxy.PlaneProxy[] planes = image.getPlanes();
|
||||||
|
|
||||||
|
// 对于 RGBA_8888 格式,通常只有一个平面
|
||||||
|
if (planes.length > 0) {
|
||||||
|
ImageProxy.PlaneProxy plane = planes[0];
|
||||||
|
ByteBuffer buffer = plane.getBuffer();
|
||||||
|
int rowStride = plane.getRowStride();
|
||||||
|
int pixelStride = plane.getPixelStride();
|
||||||
|
|
||||||
|
// 调用 Native 方法处理图像
|
||||||
|
processImageNative(buffer, width, height, format,
|
||||||
|
rowStride, pixelStride, image.getImageInfo().getRotationDegrees());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Native 方法
|
||||||
|
private native void processImageNative(ByteBuffer buffer, int width, int height,
|
||||||
|
int format, int rowStride, int pixelStride,
|
||||||
|
int rotation);
|
||||||
|
|
||||||
|
|
||||||
private static final int REQUEST_CODE_PERMISSIONS = 1001;
|
private static final int REQUEST_CODE_PERMISSIONS = 1001;
|
||||||
private static final String[] REQUIRED_PERMISSIONS = new String[]{Manifest.permission.CAMERA};
|
private static final String[] REQUIRED_PERMISSIONS = new String[]{Manifest.permission.CAMERA};
|
||||||
|
|||||||
@@ -108,3 +108,38 @@ Java_com_khronos_vulkan_1samples_MainActivity_nativeButtonClicked(JNIEnv *env, j
|
|||||||
// TODO: implement nativeButtonClicked()
|
// TODO: implement nativeButtonClicked()
|
||||||
LOGI("call MainActivity_nativeButtonClicked.");
|
LOGI("call MainActivity_nativeButtonClicked.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern void TextureLoadProcessWithVulkan(uint8_t* data, int width, int height, int format, int rowStride, size_t dataSize);
|
||||||
|
|
||||||
|
void processWithVulkan(uint8_t* data, int width, int height, int format,
|
||||||
|
int rowStride, size_t dataSize) {
|
||||||
|
// Vulkan 处理逻辑
|
||||||
|
// 1. 创建 Vulkan 图像(如果尚未创建)
|
||||||
|
// 2. 将数据复制到 Vulkan 图像内存
|
||||||
|
// 3. 执行 Vulkan 渲染或计算操作
|
||||||
|
|
||||||
|
LOGI("ProcessingImage: %dx%d, stride: %d, size: %zu", width, height, rowStride, dataSize);
|
||||||
|
TextureLoadProcessWithVulkan(data, width, height, format, rowStride, dataSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_com_khronos_vulkan_1samples_MainActivity_processImageNative(JNIEnv *env, jobject thiz,
|
||||||
|
jobject buffer, jint width,
|
||||||
|
jint height, jint format,
|
||||||
|
jint row_stride, jint pixel_stride,
|
||||||
|
jint rotation) {
|
||||||
|
// TODO: implement processImageNative()
|
||||||
|
// 获取直接字节缓冲区指针
|
||||||
|
uint8_t* imageData = static_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
|
||||||
|
jlong capacity = env->GetDirectBufferCapacity(buffer);
|
||||||
|
|
||||||
|
if (imageData == nullptr) {
|
||||||
|
LOGI("Failed to get direct buffer address");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在这里将图像数据传递给 Vulkan
|
||||||
|
// 创建 Vulkan 图像或更新现有图像
|
||||||
|
processWithVulkan(imageData, width, height, format, row_stride, capacity);
|
||||||
|
}
|
||||||
@@ -20,12 +20,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "texture_loading.h"
|
#include "texture_loading.h"
|
||||||
|
TextureLoading* TextureLoading::loadTextIns = nullptr;
|
||||||
|
|
||||||
TextureLoading::TextureLoading()
|
TextureLoading::TextureLoading()
|
||||||
{
|
{
|
||||||
zoom = -2.5f;
|
zoom = -2.5f;
|
||||||
rotation = {0.0f, 15.0f, 0.0f};
|
rotation = {0.0f, 15.0f, 0.0f};
|
||||||
title = "Texture loading";
|
title = "Texture loading";
|
||||||
|
loadTextIns = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextureLoading::~TextureLoading()
|
TextureLoading::~TextureLoading()
|
||||||
@@ -463,6 +465,47 @@ void TextureLoading::build_command_buffers()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成简单的测试图像数据(红绿蓝三色条)
|
||||||
|
std::vector<uint8_t> generateSimpleTestImage(int width, int height, int* outRowStride = nullptr) {
|
||||||
|
int rowStride = width * 4; // RGBA 每个像素4字节
|
||||||
|
if (outRowStride) {
|
||||||
|
*outRowStride = rowStride;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t dataSize = rowStride * height;
|
||||||
|
std::vector<uint8_t> imageData(dataSize, 0);
|
||||||
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
int pixelOffset = y * rowStride + x * 4;
|
||||||
|
|
||||||
|
// 简单分成三个区域:红、绿、蓝
|
||||||
|
if (x < width / 3) {
|
||||||
|
// 红色区域
|
||||||
|
imageData[pixelOffset] = 255; // R
|
||||||
|
imageData[pixelOffset + 1] = 0; // G
|
||||||
|
imageData[pixelOffset + 2] = 0; // B
|
||||||
|
}
|
||||||
|
else if (x < 2 * width / 3) {
|
||||||
|
// 绿色区域
|
||||||
|
imageData[pixelOffset] = 0; // R
|
||||||
|
imageData[pixelOffset + 1] = 255; // G
|
||||||
|
imageData[pixelOffset + 2] = 0; // B
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 蓝色区域
|
||||||
|
imageData[pixelOffset] = 0; // R
|
||||||
|
imageData[pixelOffset + 1] = 0; // G
|
||||||
|
imageData[pixelOffset + 2] = 255; // B
|
||||||
|
}
|
||||||
|
|
||||||
|
imageData[pixelOffset + 3] = 255; // A (完全不透明)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return imageData;
|
||||||
|
}
|
||||||
|
|
||||||
void TextureLoading::draw()
|
void TextureLoading::draw()
|
||||||
{
|
{
|
||||||
ApiVulkanSample::prepare_frame();
|
ApiVulkanSample::prepare_frame();
|
||||||
@@ -721,7 +764,21 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions &options)
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
load_texture();
|
//load_texture();
|
||||||
|
int width = 640;
|
||||||
|
int height = 480;
|
||||||
|
int rowStride;
|
||||||
|
|
||||||
|
auto testImage = generateSimpleTestImage(width, height, &rowStride);
|
||||||
|
size_t dataSize = testImage.size();
|
||||||
|
|
||||||
|
std::cout << "Generated test image: " << width << "x" << height << std::endl;
|
||||||
|
std::cout << "Row stride: " << rowStride << std::endl;
|
||||||
|
std::cout << "Data size: " << dataSize << " bytes" << std::endl;
|
||||||
|
|
||||||
|
processWithVulkan(testImage.data(), width, height, 1, rowStride, dataSize);
|
||||||
|
|
||||||
|
|
||||||
generate_quad();
|
generate_quad();
|
||||||
prepare_uniform_buffers();
|
prepare_uniform_buffers();
|
||||||
setup_descriptor_set_layout();
|
setup_descriptor_set_layout();
|
||||||
@@ -739,6 +796,12 @@ void TextureLoading::render(float delta_time)
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (texture.width == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -762,3 +825,300 @@ std::unique_ptr<vkb::Application> create_texture_loading()
|
|||||||
{
|
{
|
||||||
return std::make_unique<TextureLoading>();
|
return std::make_unique<TextureLoading>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextureLoadProcessWithVulkan(uint8_t* data, int width, int height, int format, int rowStride, size_t dataSize)
|
||||||
|
{
|
||||||
|
TextureLoading::Get()->processWithVulkan(data, width, height, format, rowStride, dataSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TextureLoading::processWithVulkan(uint8_t* data, int width, int height, int format, int rowStride, size_t dataSize)
|
||||||
|
{
|
||||||
|
Texture& out_texture = texture; //cam_text;
|
||||||
|
VkDevice& device = get_device().get_handle();
|
||||||
|
const VkPhysicalDevice& physicalDevice = get_device().get_gpu().get_handle();
|
||||||
|
// 检查是否需要创建新纹理
|
||||||
|
if (out_texture.image == VK_NULL_HANDLE) {
|
||||||
|
createTexture(device, physicalDevice, width, height, format, out_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
const VkCommandPool& commandPool = get_device().get_command_pool().get_handle();
|
||||||
|
|
||||||
|
// 更新纹理数据
|
||||||
|
updateTexture(device, physicalDevice, commandPool, queue, data, width, height,
|
||||||
|
rowStride, dataSize, out_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureLoading::createTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||||
|
int width, int height, int format, Texture& texture) {
|
||||||
|
|
||||||
|
texture.width = width;
|
||||||
|
texture.height = height;
|
||||||
|
texture.mip_levels = 1;
|
||||||
|
|
||||||
|
// 创建图像
|
||||||
|
VkImageCreateInfo imageInfo = {};
|
||||||
|
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||||
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||||
|
imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM; // 匹配 RGBA_8888
|
||||||
|
imageInfo.extent.width = width;
|
||||||
|
imageInfo.extent.height = height;
|
||||||
|
imageInfo.extent.depth = 1;
|
||||||
|
imageInfo.mipLevels = 1;
|
||||||
|
imageInfo.arrayLayers = 1;
|
||||||
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||||
|
imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||||
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
|
||||||
|
if (vkCreateImage(device, &imageInfo, nullptr, &texture.image) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to create image!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 分配内存
|
||||||
|
VkMemoryRequirements memRequirements;
|
||||||
|
vkGetImageMemoryRequirements(device, texture.image, &memRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocInfo.allocationSize = memRequirements.size;
|
||||||
|
allocInfo.memoryTypeIndex = findMemoryType(physicalDevice,
|
||||||
|
memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
|
|
||||||
|
if (vkAllocateMemory(device, &allocInfo, nullptr, &texture.device_memory) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to allocate image memory!");
|
||||||
|
}
|
||||||
|
|
||||||
|
vkBindImageMemory(device, texture.image, texture.device_memory, 0);
|
||||||
|
|
||||||
|
// 创建图像视图
|
||||||
|
VkImageViewCreateInfo viewInfo = {};
|
||||||
|
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
viewInfo.image = texture.image;
|
||||||
|
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||||
|
viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
viewInfo.subresourceRange.baseMipLevel = 0;
|
||||||
|
viewInfo.subresourceRange.levelCount = 1;
|
||||||
|
viewInfo.subresourceRange.baseArrayLayer = 0;
|
||||||
|
viewInfo.subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
|
if (vkCreateImageView(device, &viewInfo, nullptr, &texture.view) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to create texture image view!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建采样器
|
||||||
|
VkSamplerCreateInfo samplerInfo = {};
|
||||||
|
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||||
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
||||||
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
||||||
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||||
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||||
|
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||||
|
samplerInfo.anisotropyEnable = VK_FALSE;
|
||||||
|
samplerInfo.maxAnisotropy = 1.0f;
|
||||||
|
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
||||||
|
samplerInfo.unnormalizedCoordinates = VK_FALSE;
|
||||||
|
samplerInfo.compareEnable = VK_FALSE;
|
||||||
|
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
||||||
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||||
|
samplerInfo.mipLodBias = 0.0f;
|
||||||
|
samplerInfo.minLod = 0.0f;
|
||||||
|
samplerInfo.maxLod = 0.0f;
|
||||||
|
|
||||||
|
if (vkCreateSampler(device, &samplerInfo, nullptr, &texture.sampler) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to create texture sampler!");
|
||||||
|
}
|
||||||
|
|
||||||
|
texture.image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureLoading::updateTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
||||||
|
VkCommandPool commandPool, VkQueue queue,
|
||||||
|
uint8_t* data, int width, int height,
|
||||||
|
int rowStride, size_t dataSize, Texture& texture) {
|
||||||
|
|
||||||
|
// 创建临时 staging buffer
|
||||||
|
VkBuffer stagingBuffer;
|
||||||
|
VkDeviceMemory stagingBufferMemory;
|
||||||
|
|
||||||
|
VkBufferCreateInfo bufferInfo = {};
|
||||||
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
|
bufferInfo.size = dataSize;
|
||||||
|
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||||
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
|
if (vkCreateBuffer(device, &bufferInfo, nullptr, &stagingBuffer) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to create staging buffer!");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkMemoryRequirements memRequirements;
|
||||||
|
vkGetBufferMemoryRequirements(device, stagingBuffer, &memRequirements);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
allocInfo.allocationSize = memRequirements.size;
|
||||||
|
allocInfo.memoryTypeIndex = findMemoryType(physicalDevice,
|
||||||
|
memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
||||||
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||||
|
|
||||||
|
if (vkAllocateMemory(device, &allocInfo, nullptr, &stagingBufferMemory) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to allocate staging buffer memory!");
|
||||||
|
}
|
||||||
|
|
||||||
|
vkBindBufferMemory(device, stagingBuffer, stagingBufferMemory, 0);
|
||||||
|
|
||||||
|
// 复制数据到 staging buffer
|
||||||
|
void* mappedData;
|
||||||
|
vkMapMemory(device, stagingBufferMemory, 0, dataSize, 0, &mappedData);
|
||||||
|
|
||||||
|
// 处理行步长不一致的情况
|
||||||
|
if (rowStride == width * 4) {
|
||||||
|
// 行步长匹配,直接复制
|
||||||
|
memcpy(mappedData, data, dataSize);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 需要逐行复制,处理 padding
|
||||||
|
uint8_t* dst = static_cast<uint8_t*>(mappedData);
|
||||||
|
const uint8_t* src = data;
|
||||||
|
size_t dstRowStride = width * 4;
|
||||||
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
memcpy(dst, src, dstRowStride);
|
||||||
|
dst += dstRowStride;
|
||||||
|
src += rowStride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vkUnmapMemory(device, stagingBufferMemory);
|
||||||
|
|
||||||
|
// 创建命令缓冲区
|
||||||
|
VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
|
||||||
|
|
||||||
|
// 转换图像布局为传输目标
|
||||||
|
transitionImageLayout(commandBuffer, texture.image,
|
||||||
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
||||||
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
||||||
|
|
||||||
|
// 复制 buffer 到 image
|
||||||
|
VkBufferImageCopy region = {};
|
||||||
|
region.bufferOffset = 0;
|
||||||
|
region.bufferRowLength = 0;
|
||||||
|
region.bufferImageHeight = 0;
|
||||||
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
region.imageSubresource.mipLevel = 0;
|
||||||
|
region.imageSubresource.baseArrayLayer = 0;
|
||||||
|
region.imageSubresource.layerCount = 1;
|
||||||
|
region.imageOffset = { 0, 0, 0 };
|
||||||
|
region.imageExtent = { static_cast<uint32_t>(width),
|
||||||
|
static_cast<uint32_t>(height), 1 };
|
||||||
|
|
||||||
|
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, texture.image,
|
||||||
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||||
|
|
||||||
|
// 转换图像布局为着色器读取
|
||||||
|
transitionImageLayout(commandBuffer, texture.image,
|
||||||
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||||
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
|
||||||
|
endSingleTimeCommands(device, commandPool, queue, commandBuffer);
|
||||||
|
|
||||||
|
// 清理临时资源
|
||||||
|
vkDestroyBuffer(device, stagingBuffer, nullptr);
|
||||||
|
vkFreeMemory(device, stagingBufferMemory, nullptr);
|
||||||
|
|
||||||
|
texture.image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 辅助函数
|
||||||
|
uint32_t TextureLoading::findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
|
||||||
|
VkMemoryPropertyFlags properties) {
|
||||||
|
VkPhysicalDeviceMemoryProperties memProperties;
|
||||||
|
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
|
||||||
|
if ((typeFilter & (1 << i)) &&
|
||||||
|
(memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::runtime_error("Failed to find suitable memory type!");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkCommandBuffer TextureLoading::beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool) {
|
||||||
|
VkCommandBufferAllocateInfo allocInfo = {};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
|
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||||
|
allocInfo.commandPool = commandPool;
|
||||||
|
allocInfo.commandBufferCount = 1;
|
||||||
|
|
||||||
|
VkCommandBuffer commandBuffer;
|
||||||
|
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
|
||||||
|
|
||||||
|
VkCommandBufferBeginInfo beginInfo = {};
|
||||||
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||||
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||||
|
|
||||||
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
||||||
|
|
||||||
|
return commandBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureLoading::endSingleTimeCommands(VkDevice device, VkCommandPool commandPool,
|
||||||
|
VkQueue queue, VkCommandBuffer commandBuffer) {
|
||||||
|
vkEndCommandBuffer(commandBuffer);
|
||||||
|
|
||||||
|
VkSubmitInfo submitInfo = {};
|
||||||
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||||
|
submitInfo.commandBufferCount = 1;
|
||||||
|
submitInfo.pCommandBuffers = &commandBuffer;
|
||||||
|
|
||||||
|
vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
|
||||||
|
vkQueueWaitIdle(queue);
|
||||||
|
|
||||||
|
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureLoading::transitionImageLayout(VkCommandBuffer commandBuffer, VkImage image,
|
||||||
|
VkImageLayout oldLayout, VkImageLayout newLayout) {
|
||||||
|
VkImageMemoryBarrier barrier = {};
|
||||||
|
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||||
|
barrier.oldLayout = oldLayout;
|
||||||
|
barrier.newLayout = newLayout;
|
||||||
|
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||||
|
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||||
|
barrier.image = image;
|
||||||
|
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
barrier.subresourceRange.baseMipLevel = 0;
|
||||||
|
barrier.subresourceRange.levelCount = 1;
|
||||||
|
barrier.subresourceRange.baseArrayLayer = 0;
|
||||||
|
barrier.subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
|
VkPipelineStageFlags sourceStage;
|
||||||
|
VkPipelineStageFlags destinationStage;
|
||||||
|
|
||||||
|
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
|
||||||
|
newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
|
||||||
|
barrier.srcAccessMask = 0;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
|
||||||
|
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
||||||
|
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||||
|
}
|
||||||
|
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL &&
|
||||||
|
newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
|
||||||
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||||
|
|
||||||
|
sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||||
|
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw std::invalid_argument("Unsupported layout transition!");
|
||||||
|
}
|
||||||
|
|
||||||
|
vkCmdPipelineBarrier(commandBuffer, sourceStage, destinationStage, 0,
|
||||||
|
0, nullptr, 0, nullptr, 1, &barrier);
|
||||||
|
}
|
||||||
@@ -47,7 +47,10 @@ class TextureLoading : public ApiVulkanSample
|
|||||||
VkImageView view;
|
VkImageView view;
|
||||||
uint32_t width, height;
|
uint32_t width, height;
|
||||||
uint32_t mip_levels;
|
uint32_t mip_levels;
|
||||||
} texture;
|
};
|
||||||
|
|
||||||
|
Texture texture = {0};
|
||||||
|
Texture cam_text = {0};
|
||||||
|
|
||||||
std::unique_ptr<vkb::core::BufferC> vertex_buffer;
|
std::unique_ptr<vkb::core::BufferC> vertex_buffer;
|
||||||
std::unique_ptr<vkb::core::BufferC> index_buffer;
|
std::unique_ptr<vkb::core::BufferC> index_buffer;
|
||||||
@@ -71,7 +74,10 @@ class TextureLoading : public ApiVulkanSample
|
|||||||
VkPipelineLayout pipeline_layout;
|
VkPipelineLayout pipeline_layout;
|
||||||
VkDescriptorSet descriptor_set;
|
VkDescriptorSet descriptor_set;
|
||||||
VkDescriptorSetLayout descriptor_set_layout;
|
VkDescriptorSetLayout descriptor_set_layout;
|
||||||
|
static TextureLoading* Get() {
|
||||||
|
return loadTextIns;
|
||||||
|
}
|
||||||
|
static TextureLoading* loadTextIns;
|
||||||
TextureLoading();
|
TextureLoading();
|
||||||
~TextureLoading();
|
~TextureLoading();
|
||||||
virtual void request_gpu_features(vkb::PhysicalDevice &gpu) override;
|
virtual void request_gpu_features(vkb::PhysicalDevice &gpu) override;
|
||||||
@@ -90,6 +96,22 @@ class TextureLoading : public ApiVulkanSample
|
|||||||
virtual void render(float delta_time) override;
|
virtual void render(float delta_time) override;
|
||||||
virtual void view_changed() override;
|
virtual void view_changed() override;
|
||||||
virtual void on_update_ui_overlay(vkb::Drawer &drawer) override;
|
virtual void on_update_ui_overlay(vkb::Drawer &drawer) override;
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void processWithVulkan(uint8_t* data, int width, int height, int format, int rowStride, size_t dataSize);
|
||||||
|
|
||||||
|
void createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, int format, Texture& texture);
|
||||||
|
|
||||||
|
void updateTexture(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue queue, uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& texture);
|
||||||
|
|
||||||
|
// 辅助函数
|
||||||
|
uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
||||||
|
VkCommandBuffer beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool);
|
||||||
|
void endSingleTimeCommands(VkDevice device, VkCommandPool commandPool, VkQueue queue, VkCommandBuffer commandBuffer);
|
||||||
|
void transitionImageLayout(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unique_ptr<vkb::Application> create_texture_loading();
|
std::unique_ptr<vkb::Application> create_texture_loading();
|
||||||
|
|||||||
Reference in New Issue
Block a user