DX12: 16 bit textures.

This commit is contained in:
Artem Kharytoniuk 2017-12-23 00:02:41 +01:00
parent 6e4a2f13aa
commit c81a77fe4b
2 changed files with 40 additions and 41 deletions

View File

@ -1333,17 +1333,9 @@ void dx_begin_frame() {
dx.frame_index = dx.swapchain->GetCurrentBackBufferIndex();
// Command list allocators can only be reset when the associated
// command lists have finished execution on the GPU; apps should use
// fences to determine GPU execution progress.
DX_CHECK(dx.command_allocator->Reset());
// However, when ExecuteCommandList() is called on a particular command
// list, that command list can then be reset at any time and must be before
// re-recording.
DX_CHECK(dx.command_list->Reset(dx.command_allocator, nullptr));
// Set necessary state.
dx.command_list->SetGraphicsRootSignature(dx.root_signature);
ID3D12DescriptorHeap* heaps[] = { dx.srv_heap, dx.sampler_heap };
@ -1357,14 +1349,10 @@ void dx_begin_frame() {
CD3DX12_CPU_DESCRIPTOR_HANDLE rtv_handle(dx.rtv_heap->GetCPUDescriptorHandleForHeapStart(), dx.frame_index, dx.rtv_descriptor_size);
dx.command_list->OMSetRenderTargets(1, &rtv_handle, FALSE, &dsv_handle);
// Record commands.
const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
dx.command_list->ClearRenderTargetView(rtv_handle, clearColor, 0, nullptr);
dx.command_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D12_CLEAR_FLAGS flags = D3D12_CLEAR_FLAG_DEPTH;
if (r_shadows->integer == 2)
flags |= D3D12_CLEAR_FLAG_STENCIL;
if (r_shadows->integer == 2)
flags |= D3D12_CLEAR_FLAG_STENCIL;
dx.command_list->ClearDepthStencilView(dsv_handle, flags, 1.0f, 0, 0, nullptr);
dx.xyz_elements = 0;

View File

@ -714,44 +714,49 @@ static Dx_Image upload_dx_image(const Image_Upload_Data& upload_data, bool repea
}
}
byte* buffer = upload_data.buffer;
DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
if (r_texturebits->integer <= 16) {
format = has_alpha ? DXGI_FORMAT_B4G4R4A4_UNORM : DXGI_FORMAT_B5G5R5A1_UNORM;
}
int bytes_per_pixel = 4;
if (format == DXGI_FORMAT_B5G5R5A1_UNORM) {
if (r_texturebits->integer <= 16) {
buffer = (byte*) ri.Hunk_AllocateTempMemory( upload_data.buffer_size / 2 );
format = has_alpha ? DXGI_FORMAT_B4G4R4A4_UNORM : DXGI_FORMAT_B5G5R5A1_UNORM;
bytes_per_pixel = 2;
auto p = (uint16_t*)upload_data.buffer;
}
if (format == DXGI_FORMAT_B5G5R5A1_UNORM) {
auto p = (uint16_t*)buffer;
for (int i = 0; i < upload_data.buffer_size; i += 4, p++) {
byte r = upload_data.buffer[i+0];
byte g = upload_data.buffer[i+1];
byte b = upload_data.buffer[i+2];
*p = uint32_t((b/255.0) * 31.0 + 0.5) |
(uint32_t((g/255.0) * 31.0 + 0.5) << 5) |
(uint32_t((r/255.0) * 31.0 + 0.5) << 10) |
(1 << 15);
*p = (uint32_t((b/255.0) * 31.0 + 0.5) << 0) |
(uint32_t((g/255.0) * 31.0 + 0.5) << 5) |
(uint32_t((r/255.0) * 31.0 + 0.5) << 10) |
(1 << 15);
}
} else if (format == DXGI_FORMAT_B4G4R4A4_UNORM) {
bytes_per_pixel = 2;
auto p = (uint16_t*)upload_data.buffer;
auto p = (uint16_t*)buffer;
for (int i = 0; i < upload_data.buffer_size; i += 4, p++) {
byte r = upload_data.buffer[i+0];
byte g = upload_data.buffer[i+1];
byte b = upload_data.buffer[i+2];
byte a = upload_data.buffer[i+3];
*p = uint32_t((a/255.0) * 15.0 + 0.5) |
(uint32_t((r/255.0) * 15.0 + 0.5) << 4) |
(uint32_t((g/255.0) * 15.0 + 0.5) << 8) |
(uint32_t((b/255.0) * 15.0 + 0.5) << 12);
*p =(uint32_t((b/255.0) * 15.0 + 0.5) << 0) |
(uint32_t((g/255.0) * 15.0 + 0.5) << 4) |
(uint32_t((r/255.0) * 15.0 + 0.5) << 8) |
(uint32_t((a/255.0) * 15.0 + 0.5) << 12);
}
}
Dx_Image image = dx_create_image(w, h, format, upload_data.mip_levels, repeat_texture, image_index);
dx_upload_image_data(image.texture, w, h, upload_data.mip_levels, upload_data.buffer, bytes_per_pixel);
dx_upload_image_data(image.texture, w, h, upload_data.mip_levels, buffer, bytes_per_pixel);
if (bytes_per_pixel == 2)
ri.Hunk_FreeTempMemory(buffer);
return image;
}
@ -767,16 +772,19 @@ static Vk_Image upload_vk_image(const Image_Upload_Data& upload_data, bool repea
break;
}
}
VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
if (r_texturebits->integer <= 16) {
format = has_alpha ? VK_FORMAT_B4G4R4A4_UNORM_PACK16 : VK_FORMAT_A1R5G5B5_UNORM_PACK16;
}
byte* buffer = upload_data.buffer;
VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
int bytes_per_pixel = 4;
if (format == VK_FORMAT_A1R5G5B5_UNORM_PACK16) {
if (r_texturebits->integer <= 16) {
buffer = (byte*) ri.Hunk_AllocateTempMemory( upload_data.buffer_size / 2 );
format = has_alpha ? VK_FORMAT_B4G4R4A4_UNORM_PACK16 : VK_FORMAT_A1R5G5B5_UNORM_PACK16;
bytes_per_pixel = 2;
auto p = (uint16_t*)upload_data.buffer;
}
if (format == VK_FORMAT_A1R5G5B5_UNORM_PACK16) {
auto p = (uint16_t*)buffer;
for (int i = 0; i < upload_data.buffer_size; i += 4, p++) {
byte r = upload_data.buffer[i+0];
byte g = upload_data.buffer[i+1];
@ -788,8 +796,7 @@ static Vk_Image upload_vk_image(const Image_Upload_Data& upload_data, bool repea
(1 << 15);
}
} else if (format == VK_FORMAT_B4G4R4A4_UNORM_PACK16) {
bytes_per_pixel = 2;
auto p = (uint16_t*)upload_data.buffer;
auto p = (uint16_t*)buffer;
for (int i = 0; i < upload_data.buffer_size; i += 4, p++) {
byte r = upload_data.buffer[i+0];
byte g = upload_data.buffer[i+1];
@ -804,7 +811,11 @@ static Vk_Image upload_vk_image(const Image_Upload_Data& upload_data, bool repea
}
Vk_Image image = vk_create_image(w, h, format, upload_data.mip_levels, repeat_texture);
vk_upload_image_data(image.handle, w, h, upload_data.mip_levels > 1, upload_data.buffer, bytes_per_pixel);
vk_upload_image_data(image.handle, w, h, upload_data.mip_levels > 1, buffer, bytes_per_pixel);
if (bytes_per_pixel == 2)
ri.Hunk_FreeTempMemory(buffer);
return image;
}