mipmaps
This commit is contained in:
@ -36,6 +36,9 @@ namespace ia::iae
|
||||
|
||||
g_linearClampSampler = SDL_CreateGPUSampler(Renderer::GetDevice(), &createInfo);
|
||||
|
||||
createInfo.min_filter = SDL_GPU_FILTER_NEAREST;
|
||||
createInfo.mag_filter = SDL_GPU_FILTER_NEAREST;
|
||||
createInfo.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR;
|
||||
createInfo.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
|
||||
createInfo.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
|
||||
createInfo.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
|
||||
@ -62,15 +65,19 @@ namespace ia::iae
|
||||
|
||||
SDL_GPUTexture *GPUResourceManager::CreateTexture(IN SDL_GPUTextureUsageFlags usage, IN INT32 width,
|
||||
IN INT32 height, IN PCUINT8 rgbaData,
|
||||
IN SDL_GPUTextureFormat format)
|
||||
IN SDL_GPUTextureFormat format, IN BOOL generateMipmaps)
|
||||
{
|
||||
const auto mipLevels = generateMipmaps ? floor(log2(ia_max(width, height))) + 1 : 1;
|
||||
|
||||
STATIC Vector<UINT8> TMP_COLOR_BUFFER;
|
||||
|
||||
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
|
||||
.format = format,
|
||||
.usage = usage,
|
||||
.width = (UINT32) width,
|
||||
.height = (UINT32) height,
|
||||
.layer_count_or_depth = 1,
|
||||
.num_levels = 1,
|
||||
.num_levels = (UINT32) mipLevels,
|
||||
.sample_count = SDL_GPU_SAMPLECOUNT_1};
|
||||
const auto result = SDL_CreateGPUTexture(Renderer::GetDevice(), &createInfo);
|
||||
if (!result)
|
||||
@ -80,14 +87,25 @@ namespace ia::iae
|
||||
}
|
||||
if (rgbaData)
|
||||
{
|
||||
TMP_COLOR_BUFFER.reset();
|
||||
TMP_COLOR_BUFFER.resize(width * height * 4);
|
||||
for (SIZE_T i = 0; i < TMP_COLOR_BUFFER.size() >> 2; i++)
|
||||
{
|
||||
const auto a = static_cast<FLOAT32>(rgbaData[i * 4 + 3]) / 255.0f;
|
||||
TMP_COLOR_BUFFER[i * 4 + 0] = static_cast<UINT8>(rgbaData[i * 4 + 0] * a);
|
||||
TMP_COLOR_BUFFER[i * 4 + 1] = static_cast<UINT8>(rgbaData[i * 4 + 1] * a);
|
||||
TMP_COLOR_BUFFER[i * 4 + 2] = static_cast<UINT8>(rgbaData[i * 4 + 2] * a);
|
||||
TMP_COLOR_BUFFER[i * 4 + 3] = rgbaData[i * 4 + 3];
|
||||
}
|
||||
|
||||
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = (UINT32) width * (UINT32) height * 4};
|
||||
const auto stagingBuffer = SDL_CreateGPUTransferBuffer(Renderer::GetDevice(), &stagingBufferCreateInfo);
|
||||
const auto mappedPtr = SDL_MapGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer, false);
|
||||
SDL_memcpy(mappedPtr, rgbaData, width * height * 4);
|
||||
SDL_memcpy(mappedPtr, TMP_COLOR_BUFFER.data(), width * height * 4);
|
||||
SDL_UnmapGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer);
|
||||
|
||||
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
|
||||
auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
|
||||
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
|
||||
|
||||
SDL_GPUTextureTransferInfo transferInfo{.transfer_buffer = stagingBuffer, .offset = 0};
|
||||
@ -98,6 +116,14 @@ namespace ia::iae
|
||||
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
||||
SDL_WaitForGPUIdle(Renderer::GetDevice());
|
||||
SDL_ReleaseGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer);
|
||||
|
||||
if (mipLevels > 1)
|
||||
{
|
||||
cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
|
||||
SDL_GenerateMipmapsForGPUTexture(cmdBuffer, result);
|
||||
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
||||
SDL_WaitForGPUIdle(Renderer::GetDevice());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -138,7 +164,8 @@ namespace ia::iae
|
||||
|
||||
VOID GPUResourceManager::DestroyTexture(IN SDL_GPUTexture *handle)
|
||||
{
|
||||
if(!handle) return;
|
||||
if (!handle)
|
||||
return;
|
||||
SDL_ReleaseGPUTexture(Renderer::GetDevice(), handle);
|
||||
}
|
||||
|
||||
@ -175,8 +202,8 @@ namespace ia::iae
|
||||
return result;
|
||||
}
|
||||
|
||||
SDL_GPUTexture* GPUResourceManager::CombineTextures(IN SDL_GPUTexture** textures, IN INT32 unitWidth, IN INT32 unitHeight,
|
||||
IN INT32 unitCountX, IN INT32 unitCountY)
|
||||
SDL_GPUTexture *GPUResourceManager::CombineTextures(IN SDL_GPUTexture **textures, IN INT32 unitWidth,
|
||||
IN INT32 unitHeight, IN INT32 unitCountX, IN INT32 unitCountY)
|
||||
{
|
||||
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
|
||||
.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
|
||||
@ -198,7 +225,8 @@ namespace ia::iae
|
||||
for (INT32 x = 0; x < unitCountX; x++)
|
||||
{
|
||||
SDL_GPUTextureLocation src{.texture = textures[x + (y * unitCountX)], .x = 0, .y = 0};
|
||||
SDL_GPUTextureLocation dst{.texture = result, .x = (UINT32)(x * unitWidth), .y = (UINT32)(y * unitHeight)};
|
||||
SDL_GPUTextureLocation dst{
|
||||
.texture = result, .x = (UINT32) (x * unitWidth), .y = (UINT32) (y * unitHeight)};
|
||||
SDL_CopyGPUTextureToTexture(copyPass, &src, &dst, unitWidth, unitHeight, 1, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,7 +51,14 @@ namespace ia::iae
|
||||
|
||||
SDL_GPUColorTargetDescription colorTargetDesc = {
|
||||
.format = Renderer::GetRenderTargetFormat(),
|
||||
};
|
||||
.blend_state = {.src_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE,
|
||||
.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
|
||||
.color_blend_op = SDL_GPU_BLENDOP_ADD,
|
||||
.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE,
|
||||
.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
|
||||
.alpha_blend_op = SDL_GPU_BLENDOP_ADD,
|
||||
.enable_blend = true,
|
||||
.enable_color_write_mask = false}};
|
||||
|
||||
SDL_GPUVertexBufferDescription vertexBufferDesc = {
|
||||
.slot = 0,
|
||||
@ -85,16 +92,15 @@ namespace ia::iae
|
||||
.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE},
|
||||
.depth_stencil_state = SDL_GPUDepthStencilState{.compare_op = SDL_GPU_COMPAREOP_GREATER_OR_EQUAL,
|
||||
.write_mask = 0xFF,
|
||||
.enable_depth_test = enableDepthTest,
|
||||
.enable_depth_write = enableDepthTest,
|
||||
.enable_depth_test = false,
|
||||
.enable_depth_write = false,
|
||||
.enable_stencil_test = false},
|
||||
.target_info = {.color_target_descriptions = &colorTargetDesc,
|
||||
.num_color_targets = 1,
|
||||
.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM,
|
||||
.has_depth_stencil_target = enableDepthTest},
|
||||
.has_depth_stencil_target = false},
|
||||
};
|
||||
|
||||
if(!(m_handle = SDL_CreateGPUGraphicsPipeline(Renderer::GetDevice(), &createInfo)))
|
||||
if (!(m_handle = SDL_CreateGPUGraphicsPipeline(Renderer::GetDevice(), &createInfo)))
|
||||
THROW_UNKNOWN("Failed to create a SDL graphics pipeline: ", SDL_GetError());
|
||||
|
||||
SDL_ReleaseGPUShader(Renderer::GetDevice(), pixelShader);
|
||||
|
||||
@ -145,8 +145,8 @@ namespace ia::iae
|
||||
s_state.ColorTargetInfo.clear_color = SDL_FColor{clearColor.x, clearColor.y, clearColor.z, 1.0f};
|
||||
s_state.ColorTargetInfo.texture = s_renderTargetSceneColor;
|
||||
s_state.DepthStencilTargetInfo.texture = s_renderTargetSceneDepth;
|
||||
s_state.ActiveRenderPass = SDL_BeginGPURenderPass(s_state.ActiveCommandBuffer, &s_state.ColorTargetInfo, 1,
|
||||
&s_state.DepthStencilTargetInfo);
|
||||
s_state.ActiveRenderPass =
|
||||
SDL_BeginGPURenderPass(s_state.ActiveCommandBuffer, &s_state.ColorTargetInfo, 1, nullptr);
|
||||
|
||||
SDL_BindGPUGraphicsPipeline(s_state.ActiveRenderPass, s_geometryPipeline->GetHandle());
|
||||
SDL_PushGPUVertexUniformData(
|
||||
@ -185,8 +185,8 @@ namespace ia::iae
|
||||
SDL_BeginGPURenderPass(s_state.ActiveCommandBuffer, &s_state.ColorTargetInfo, 1, nullptr);
|
||||
SDL_BindGPUGraphicsPipeline(s_state.ActiveRenderPass, s_postprocessPipeline->GetHandle());
|
||||
SDL_GPUTextureSamplerBinding textureBindings[2] = {
|
||||
{.texture = s_renderTargetSceneColor, .sampler = GPUResourceManager::GetSampler_LinearRepeat()},
|
||||
{.texture = s_renderTargetDebugDrawColor, .sampler = GPUResourceManager::GetSampler_LinearRepeat()},
|
||||
{.texture = s_renderTargetSceneColor, .sampler = GPUResourceManager::GetSampler_LinearClamp()},
|
||||
{.texture = s_renderTargetDebugDrawColor, .sampler = GPUResourceManager::GetSampler_LinearClamp()},
|
||||
};
|
||||
SDL_BindGPUFragmentSamplers(s_state.ActiveRenderPass, 0, textureBindings, 2);
|
||||
SDL_DrawGPUPrimitives(s_state.ActiveRenderPass, 6, 1, 0, 0);
|
||||
@ -287,8 +287,11 @@ namespace ia::iae
|
||||
s_fragmentUniform.FlippedH = s_state.FlippedH;
|
||||
s_fragmentUniform.FlippedV = s_state.FlippedV;
|
||||
s_fragmentUniform.TextureOffset = s_state.TextureOffset;
|
||||
SDL_GPUTextureSamplerBinding textureBinding{.texture = s_state.ActiveTexture,
|
||||
.sampler = ((s_state.TextureOffset.x <= FLOAT32_EPSILON) && (s_state.TextureOffset.y <= FLOAT32_EPSILON)) ? GPUResourceManager::GetSampler_LinearClamp() : GPUResourceManager::GetSampler_LinearRepeat()};
|
||||
SDL_GPUTextureSamplerBinding textureBinding{
|
||||
.texture = s_state.ActiveTexture,
|
||||
.sampler = ((s_state.TextureOffset.x <= FLOAT32_EPSILON) && (s_state.TextureOffset.y <= FLOAT32_EPSILON))
|
||||
? GPUResourceManager::GetSampler_LinearClamp()
|
||||
: GPUResourceManager::GetSampler_LinearRepeat()};
|
||||
SDL_BindGPUFragmentSamplers(s_state.ActiveRenderPass, 0, &textureBinding, 1);
|
||||
SDL_PushGPUFragmentUniformData(s_state.ActiveCommandBuffer, 0, &s_fragmentUniform, sizeof(s_fragmentUniform));
|
||||
|
||||
@ -404,6 +407,19 @@ namespace ia::iae
|
||||
sizeof(Mat4));
|
||||
}
|
||||
|
||||
VOID Engine::SetRenderState_TransformUI(IN Vec2 position, IN Vec2 scale, IN FLOAT32 rotation)
|
||||
{
|
||||
Renderer::s_state.ModelMatrix =
|
||||
glm::translate(glm::mat4(1.0f), glm::vec3{position.x, position.y,
|
||||
static_cast<FLOAT32>(0xFF << 13)});
|
||||
Renderer::s_state.ModelMatrix =
|
||||
glm::rotate(Renderer::s_state.ModelMatrix, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
Renderer::s_state.ModelMatrix = glm::scale(Renderer::s_state.ModelMatrix, glm::vec3{scale.x, scale.y, 1.0f});
|
||||
|
||||
SDL_PushGPUVertexUniformData(Renderer::s_state.ActiveCommandBuffer, 2, &Renderer::s_state.ModelMatrix,
|
||||
sizeof(Mat4));
|
||||
}
|
||||
|
||||
VOID Engine::SetRenderState_YSortingEnabled(IN BOOL value)
|
||||
{
|
||||
Renderer::s_state.YSortingEnabled = value;
|
||||
|
||||
Reference in New Issue
Block a user