only upload data to texture if *data is not NULL
refactor create_shader_texture(path) to use the other create_shader_texture(name, data, width. height, channels)
This commit is contained in:
parent
cdf6cf00a8
commit
fc88859eaf
228
src/main.cpp
228
src/main.cpp
@ -372,110 +372,6 @@ static void change_map_size(char direction, int amount) {
|
||||
SDL_ReleaseGPUBuffer(device, old_world_buffer);
|
||||
}
|
||||
|
||||
static SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
int width = 0, height = 0, channels = 0;
|
||||
stbi_uc *data = stbi_load(path, &width, &height, &channels, 0);
|
||||
if (!data) {
|
||||
log_error("Failed to load texture (\"%s\").", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
|
||||
|
||||
if (format == SDL_GPU_TEXTUREFORMAT_INVALID) {
|
||||
log_error("Failed to find texture format for texture (\"%s\").", path);
|
||||
stbi_image_free(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_PropertiesID properties = SDL_CreateProperties();
|
||||
if (properties) SDL_SetStringProperty(properties, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, path);
|
||||
|
||||
SDL_GPUTextureCreateInfo texture_info = {
|
||||
.type = SDL_GPU_TEXTURETYPE_2D,
|
||||
.format = format,
|
||||
.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
|
||||
.width = (Uint32)width,
|
||||
.height = (Uint32)height,
|
||||
|
||||
.layer_count_or_depth = 1,
|
||||
.num_levels = 1,
|
||||
|
||||
.props = properties,
|
||||
};
|
||||
|
||||
SDL_GPUTexture *texture = SDL_CreateGPUTexture(device, &texture_info);
|
||||
if (!texture) {
|
||||
log_error("Failed to create gpu texture (%s).", SDL_GetError());
|
||||
stbi_image_free(data);
|
||||
return NULL;
|
||||
}
|
||||
if (properties) SDL_DestroyProperties(properties);
|
||||
|
||||
Uint32 upload_size = width * height * channels;
|
||||
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = upload_size,
|
||||
};
|
||||
|
||||
SDL_GPUTransferBuffer *transfer_buffer = SDL_CreateGPUTransferBuffer(device, &transfer_buffer_info);
|
||||
if (!transfer_buffer) {
|
||||
log_error("Failed to create gpu transfer buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
stbi_image_free(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *transfer_data = SDL_MapGPUTransferBuffer(device, transfer_buffer, false);
|
||||
if (!transfer_data) {
|
||||
log_error("Failed to map gpu transfer buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
stbi_image_free(data);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(transfer_data, data, upload_size);
|
||||
SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
|
||||
stbi_image_free(data);
|
||||
|
||||
SDL_GPUTextureTransferInfo transfer_info = {
|
||||
.transfer_buffer = transfer_buffer,
|
||||
.pixels_per_row = (Uint32)width,
|
||||
};
|
||||
|
||||
SDL_GPUTextureRegion tetxure_region = {
|
||||
.texture = texture,
|
||||
.w = (Uint32)width,
|
||||
.h = (Uint32)height,
|
||||
.d = 1,
|
||||
};
|
||||
|
||||
SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device);
|
||||
if (!command_buffer) {
|
||||
log_error("Failed to acquire gpu command buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_GPUCopyPass *copy_pass = SDL_BeginGPUCopyPass(command_buffer);
|
||||
SDL_UploadToGPUTexture(copy_pass, &transfer_info, &tetxure_region, false);
|
||||
SDL_EndGPUCopyPass(copy_pass);
|
||||
|
||||
if (!SDL_SubmitGPUCommandBuffer(command_buffer)) {
|
||||
log_error("Failed to submit gpu command buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
SDL_CancelGPUCommandBuffer(command_buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
|
||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||
@ -509,65 +405,87 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data,
|
||||
}
|
||||
if (properties) SDL_DestroyProperties(properties);
|
||||
|
||||
Uint32 upload_size = width * height * channels;
|
||||
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = upload_size,
|
||||
};
|
||||
if (data) {
|
||||
Uint32 upload_size = width * height * channels;
|
||||
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = upload_size,
|
||||
};
|
||||
|
||||
SDL_GPUTransferBuffer *transfer_buffer = SDL_CreateGPUTransferBuffer(device, &transfer_buffer_info);
|
||||
if (!transfer_buffer) {
|
||||
log_error("Failed to create gpu transfer buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
SDL_GPUTransferBuffer *transfer_buffer = SDL_CreateGPUTransferBuffer(device, &transfer_buffer_info);
|
||||
if (!transfer_buffer) {
|
||||
log_error("Failed to create gpu transfer buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *transfer_data = SDL_MapGPUTransferBuffer(device, transfer_buffer, false);
|
||||
if (!transfer_data) {
|
||||
log_error("Failed to map gpu transfer buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(transfer_data, data, upload_size);
|
||||
SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
|
||||
|
||||
SDL_GPUTextureTransferInfo transfer_info = {
|
||||
.transfer_buffer = transfer_buffer,
|
||||
.pixels_per_row = (Uint32)width,
|
||||
};
|
||||
|
||||
SDL_GPUTextureRegion tetxure_region = {
|
||||
.texture = texture,
|
||||
.w = (Uint32)width,
|
||||
.h = (Uint32)height,
|
||||
.d = 1,
|
||||
};
|
||||
|
||||
SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device);
|
||||
if (!command_buffer) {
|
||||
log_error("Failed to acquire gpu command buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_GPUCopyPass *copy_pass = SDL_BeginGPUCopyPass(command_buffer);
|
||||
SDL_UploadToGPUTexture(copy_pass, &transfer_info, &tetxure_region, false);
|
||||
SDL_EndGPUCopyPass(copy_pass);
|
||||
|
||||
if (!SDL_SubmitGPUCommandBuffer(command_buffer)) {
|
||||
log_error("Failed to submit gpu command buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
SDL_CancelGPUCommandBuffer(command_buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *transfer_data = SDL_MapGPUTransferBuffer(device, transfer_buffer, false);
|
||||
if (!transfer_data) {
|
||||
log_error("Failed to map gpu transfer buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(transfer_data, data, upload_size);
|
||||
SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
|
||||
|
||||
SDL_GPUTextureTransferInfo transfer_info = {
|
||||
.transfer_buffer = transfer_buffer,
|
||||
.pixels_per_row = (Uint32)width,
|
||||
};
|
||||
|
||||
SDL_GPUTextureRegion tetxure_region = {
|
||||
.texture = texture,
|
||||
.w = (Uint32)width,
|
||||
.h = (Uint32)height,
|
||||
.d = 1,
|
||||
};
|
||||
|
||||
SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device);
|
||||
if (!command_buffer) {
|
||||
log_error("Failed to acquire gpu command buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_GPUCopyPass *copy_pass = SDL_BeginGPUCopyPass(command_buffer);
|
||||
SDL_UploadToGPUTexture(copy_pass, &transfer_info, &tetxure_region, false);
|
||||
SDL_EndGPUCopyPass(copy_pass);
|
||||
|
||||
if (!SDL_SubmitGPUCommandBuffer(command_buffer)) {
|
||||
log_error("Failed to submit gpu command buffer (%s).", SDL_GetError());
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
SDL_ReleaseGPUTexture(device, texture);
|
||||
SDL_CancelGPUCommandBuffer(command_buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
int width = 0, height = 0, channels = 0;
|
||||
stbi_uc *data = stbi_load(path, &width, &height, &channels, 0);
|
||||
if (!data) {
|
||||
log_error("Failed to load texture (\"%s\").", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_GPUTexture *result = create_shader_texture(path, (char *)data, width, height, channels);
|
||||
if (!result) {
|
||||
log_error("Failed to load texture (\"%s\").", path);
|
||||
stbi_image_free(data);
|
||||
return NULL;
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void blit(char *dst, Sint32 dst_pitch, Sint32 dst_x, Sint32 dst_y, char *src, Sint32 src_pitch, Sint32 width, Sint32 height, int components = 4) {
|
||||
for (Sint32 y = 0; y < height; y++)
|
||||
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user