2022-01-07 12:05:52 +08:00
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
2022-01-05 16:02:07 +08:00
|
|
|
|
#include<hgl/graph/VKImageCreateInfo.h>
|
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
2023-03-25 15:47:08 +08:00
|
|
|
|
#include<hgl/graph/VKBuffer.h>
|
2022-01-05 16:02:07 +08:00
|
|
|
|
#include"BufferImageCopy2D.h"
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2022-01-07 19:39:39 +08:00
|
|
|
|
void GenerateMipmaps(TextureCmdBuffer *texture_cmd_buf,VkImage image,VkImageAspectFlags aspect_mask,VkExtent3D extent,const uint32_t mipLevels,const uint32_t layer_count);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
|
|
|
|
|
Texture2D *GPUDevice::CreateTexture2D(TextureData *tex_data)
|
|
|
|
|
{
|
|
|
|
|
if(!tex_data)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
return(new Texture2D(attr->device,tex_data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Texture2D *GPUDevice::CreateTexture2D(TextureCreateInfo *tci)
|
|
|
|
|
{
|
|
|
|
|
if(!tci)return(nullptr);
|
|
|
|
|
|
2022-01-07 11:57:13 +08:00
|
|
|
|
if(tci->extent.width*tci->extent.height<=0)
|
|
|
|
|
{
|
|
|
|
|
Clear(tci);
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2022-01-05 16:02:07 +08:00
|
|
|
|
|
|
|
|
|
if(tci->target_mipmaps==0)
|
|
|
|
|
tci->target_mipmaps=(tci->origin_mipmaps>1?tci->origin_mipmaps:1);
|
|
|
|
|
|
|
|
|
|
if(!tci->image)
|
|
|
|
|
{
|
2022-01-05 18:18:43 +08:00
|
|
|
|
Image2DCreateInfo ici(tci->usage,tci->tiling,tci->format,tci->extent,tci->target_mipmaps);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
tci->image=CreateImage(&ici);
|
|
|
|
|
|
|
|
|
|
if(!tci->image)
|
|
|
|
|
{
|
|
|
|
|
Clear(tci);
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tci->memory=CreateMemory(tci->image);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!tci->image_view)
|
|
|
|
|
tci->image_view=CreateImageView2D(attr->device,tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image);
|
|
|
|
|
|
|
|
|
|
TextureData *tex_data=new TextureData(tci);
|
|
|
|
|
|
|
|
|
|
Texture2D *tex=CreateTexture2D(tex_data);
|
|
|
|
|
|
|
|
|
|
if(!tex)
|
|
|
|
|
{
|
|
|
|
|
Clear(tci);
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if((!tci->buffer)&&tci->pixels&&tci->total_bytes>0)
|
|
|
|
|
tci->buffer=CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,tci->total_bytes,tci->pixels);
|
|
|
|
|
|
|
|
|
|
if(tci->buffer)
|
|
|
|
|
{
|
|
|
|
|
texture_cmd_buf->Begin();
|
|
|
|
|
if(tci->target_mipmaps==tci->origin_mipmaps)
|
|
|
|
|
{
|
2022-01-07 12:05:52 +08:00
|
|
|
|
if(tci->target_mipmaps<=1) //本身不含mipmaps,但也不要mipmaps
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
2022-01-07 12:05:52 +08:00
|
|
|
|
CommitTexture2D(tex,tci->buffer,VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
}
|
2022-01-07 12:05:52 +08:00
|
|
|
|
else //本身有mipmaps数据
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
2022-01-07 12:14:43 +08:00
|
|
|
|
CommitTexture2DMipmaps(tex,tci->buffer,tci->extent,tci->mipmap_zero_total_bytes);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-01-07 12:05:52 +08:00
|
|
|
|
if(tci->origin_mipmaps<=1) //本身不含mipmaps数据,又想要mipmaps
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
2022-01-07 12:05:52 +08:00
|
|
|
|
CommitTexture2D(tex,tci->buffer,VK_PIPELINE_STAGE_TRANSFER_BIT);
|
2022-01-07 19:39:39 +08:00
|
|
|
|
GenerateMipmaps(texture_cmd_buf,tex->GetImage(),tex->GetAspect(),tci->extent,tex_data->miplevel,1);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
}
|
|
|
|
|
texture_cmd_buf->End();
|
|
|
|
|
|
|
|
|
|
SubmitTexture(*texture_cmd_buf);
|
|
|
|
|
|
|
|
|
|
delete tci->buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 11:57:13 +08:00
|
|
|
|
delete tci; //"delete tci" is correct,please don't use "Clear(tci)"
|
2022-01-05 16:02:07 +08:00
|
|
|
|
return tex;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
bool GPUDevice::CommitTexture2D(Texture2D *tex,DeviceBuffer *buf,VkPipelineStageFlags destinationStage)
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tex||!buf)return(false);
|
|
|
|
|
|
|
|
|
|
BufferImageCopy buffer_image_copy(tex);
|
|
|
|
|
|
2023-09-25 12:42:48 +08:00
|
|
|
|
return CopyBufferToImage(tex,buf,&buffer_image_copy,1,1,destinationStage);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
bool GPUDevice::CommitTexture2DMipmaps(Texture2D *tex,DeviceBuffer *buf,const VkExtent3D &extent,uint32_t total_bytes)
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tex||!buf
|
2022-01-07 12:14:43 +08:00
|
|
|
|
||extent.width*extent.height<=0)
|
2022-01-05 16:02:07 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
const uint32_t miplevel=tex->GetMipLevel();
|
|
|
|
|
|
|
|
|
|
AutoDeleteArray<VkBufferImageCopy> buffer_image_copy(miplevel);
|
|
|
|
|
|
|
|
|
|
VkDeviceSize offset=0;
|
|
|
|
|
uint32_t level=0;
|
|
|
|
|
|
2022-01-07 12:14:43 +08:00
|
|
|
|
uint32_t width=extent.width;
|
|
|
|
|
uint32_t height=extent.height;
|
|
|
|
|
|
2022-01-05 16:02:07 +08:00
|
|
|
|
buffer_image_copy.zero();
|
|
|
|
|
|
|
|
|
|
for(VkBufferImageCopy &bic:buffer_image_copy)
|
|
|
|
|
{
|
|
|
|
|
bic.bufferOffset = offset;
|
|
|
|
|
bic.bufferRowLength = 0;
|
|
|
|
|
bic.bufferImageHeight = 0;
|
|
|
|
|
bic.imageSubresource.aspectMask = tex->GetAspect();
|
|
|
|
|
bic.imageSubresource.mipLevel = level++;
|
|
|
|
|
bic.imageSubresource.baseArrayLayer = 0;
|
|
|
|
|
bic.imageSubresource.layerCount = 1;
|
|
|
|
|
bic.imageOffset.x = 0;
|
|
|
|
|
bic.imageOffset.y = 0;
|
|
|
|
|
bic.imageOffset.z = 0;
|
|
|
|
|
bic.imageExtent.width = width;
|
|
|
|
|
bic.imageExtent.height= height;
|
|
|
|
|
bic.imageExtent.depth = 1;
|
|
|
|
|
|
|
|
|
|
if(total_bytes<8)
|
|
|
|
|
offset+=8;
|
|
|
|
|
else
|
|
|
|
|
offset+=total_bytes;
|
|
|
|
|
|
|
|
|
|
if(width>1){width>>=1;total_bytes>>=1;}
|
|
|
|
|
if(height>1){height>>=1;total_bytes>>=1;}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 12:42:48 +08:00
|
|
|
|
return CopyBufferToImage(tex,buf,buffer_image_copy,miplevel,1,VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
bool GPUDevice::ChangeTexture2D(Texture2D *tex,DeviceBuffer *buf,const List<Image2DRegion> &ir_list,VkPipelineStageFlags destinationStage)
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tex||!buf||ir_list.GetCount()<=0)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
const int ir_count=ir_list.GetCount();
|
|
|
|
|
int count=0;
|
|
|
|
|
|
|
|
|
|
AutoDeleteArray<VkBufferImageCopy> buffer_image_copy(ir_count);
|
|
|
|
|
VkBufferImageCopy *tp=buffer_image_copy;
|
|
|
|
|
|
|
|
|
|
VkDeviceSize offset=0;
|
|
|
|
|
|
|
|
|
|
for(const Image2DRegion &sp:ir_list)
|
|
|
|
|
{
|
|
|
|
|
tp->bufferOffset = offset;
|
|
|
|
|
tp->bufferRowLength = 0;
|
|
|
|
|
tp->bufferImageHeight = 0;
|
|
|
|
|
tp->imageSubresource.aspectMask = tex->GetAspect();
|
|
|
|
|
tp->imageSubresource.mipLevel = 0;
|
|
|
|
|
tp->imageSubresource.baseArrayLayer = 0;
|
|
|
|
|
tp->imageSubresource.layerCount = 1;
|
|
|
|
|
tp->imageOffset.x = sp.left;
|
|
|
|
|
tp->imageOffset.y = sp.top;
|
|
|
|
|
tp->imageOffset.z = 0;
|
|
|
|
|
tp->imageExtent.width = sp.width;
|
|
|
|
|
tp->imageExtent.height= sp.height;
|
|
|
|
|
tp->imageExtent.depth = 1;
|
|
|
|
|
|
|
|
|
|
offset+=sp.bytes;
|
|
|
|
|
++tp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
texture_cmd_buf->Begin();
|
2023-09-25 12:42:48 +08:00
|
|
|
|
bool result=CopyBufferToImage(tex,buf,buffer_image_copy,ir_count,1,destinationStage);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
texture_cmd_buf->End();
|
|
|
|
|
SubmitTexture(*texture_cmd_buf);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 20:25:32 +08:00
|
|
|
|
bool GPUDevice::ChangeTexture2D(Texture2D *tex,DeviceBuffer *buf,const RectScope2ui &scope,VkPipelineStageFlags destinationStage)
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tex||!buf
|
2023-06-07 20:25:32 +08:00
|
|
|
|
||scope.GetWidth()<=0
|
|
|
|
|
||scope.GetHeight()<=0
|
|
|
|
|
||scope.GetRight()>tex->GetWidth()
|
|
|
|
|
||scope.GetBottom()>tex->GetHeight())
|
2022-01-05 16:02:07 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-06-07 20:25:32 +08:00
|
|
|
|
BufferImageCopy buffer_image_copy(tex,scope);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
|
|
|
|
|
texture_cmd_buf->Begin();
|
2023-09-25 12:42:48 +08:00
|
|
|
|
bool result=CopyBufferToImage(tex,buf,&buffer_image_copy,1,1,destinationStage);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
texture_cmd_buf->End();
|
|
|
|
|
SubmitTexture(*texture_cmd_buf);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 20:25:32 +08:00
|
|
|
|
bool GPUDevice::ChangeTexture2D(Texture2D *tex,void *data,const uint32_t size,const RectScope2ui &scope,VkPipelineStageFlags destinationStage)
|
2022-01-05 16:02:07 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tex||!data
|
2023-06-07 20:25:32 +08:00
|
|
|
|
||size<=0
|
|
|
|
|
||scope.GetWidth()<=0
|
|
|
|
|
||scope.GetHeight()<=0
|
|
|
|
|
||scope.GetRight()>tex->GetWidth()
|
|
|
|
|
||scope.GetBottom()>tex->GetHeight())
|
2022-01-05 16:02:07 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *buf=CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,size,data);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
|
2023-06-07 20:25:32 +08:00
|
|
|
|
bool result=ChangeTexture2D(tex,buf,scope,destinationStage);
|
2022-01-05 16:02:07 +08:00
|
|
|
|
|
|
|
|
|
delete buf;
|
|
|
|
|
return(result);
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|