Skip to content

Commit

Permalink
colour management: globally added to vulkan fragment shader
Browse files Browse the repository at this point in the history
  • Loading branch information
hanatos committed Jan 22, 2020
1 parent 3fe6ae9 commit 608c97e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 30 deletions.
126 changes: 96 additions & 30 deletions examples/imgui_impl_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,38 +175,77 @@ static uint32_t __glsl_shader_vert_spv[] =
layout(location = 0) out vec4 fColor;
layout(set=0, binding=0) uniform sampler2D sTexture;
layout(location = 0) in struct { vec4 Color; vec2 UV; } In;
layout(push_constant) uniform uPushConstant
{
vec2 uScale;
vec2 uTranslate;
vec4 gamma;
mat3 rec2020_to_dspy;
} pc;
void main()
{
fColor = In.Color * texture(sTexture, In.UV.st);
// convert linear rec2020 to profile
fColor = In.Color * texture(sTexture, In.UV.st);
fColor.rgb = pc.rec2020_to_dspy * fColor.rgb;
fColor.rgb = pow(fColor.rgb, vec3(pc.gamma.rgb));
}
*/
static uint32_t __glsl_shader_frag_spv[] =
{
0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
0x00010038
0x07230203,0x00010000,0x00080008,0x0000003a,0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
0x00000000,0x00060005,0x00000020,0x73755075,0x6e6f4368,0x6e617473,0x00000074,0x00050006,
0x00000020,0x00000000,0x61635375,0x0000656c,0x00060006,0x00000020,0x00000001,0x61725475,
0x616c736e,0x00006574,0x00050006,0x00000020,0x00000002,0x6d6d6167,0x00000061,0x00070006,
0x00000020,0x00000003,0x32636572,0x5f303230,0x645f6f74,0x00797073,0x00030005,0x00000022,
0x00006370,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
0x00000000,0x00050048,0x00000020,0x00000000,0x00000023,0x00000000,0x00050048,0x00000020,
0x00000001,0x00000023,0x00000008,0x00050048,0x00000020,0x00000002,0x00000023,0x00000010,
0x00040048,0x00000020,0x00000003,0x00000005,0x00050048,0x00000020,0x00000003,0x00000023,
0x00000020,0x00050048,0x00000020,0x00000003,0x00000007,0x00000010,0x00030047,0x00000020,
0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00040017,0x0000001e,0x00000006,
0x00000003,0x00040018,0x0000001f,0x0000001e,0x00000003,0x0006001e,0x00000020,0x0000000a,
0x0000000a,0x00000007,0x0000001f,0x00040020,0x00000021,0x00000009,0x00000020,0x0004003b,
0x00000021,0x00000022,0x00000009,0x0004002b,0x0000000e,0x00000023,0x00000003,0x00040020,
0x00000024,0x00000009,0x0000001f,0x0004002b,0x0000000e,0x0000002e,0x00000002,0x00040020,
0x0000002f,0x00000009,0x00000007,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,
0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,0x0000000f,0x0004003d,
0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,0x00000016,0x00050041,
0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,0x0000001b,0x0000001a,
0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,0x00000007,0x0000001d,
0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x00050041,0x00000024,0x00000025,
0x00000022,0x00000023,0x0004003d,0x0000001f,0x00000026,0x00000025,0x0004003d,0x00000007,
0x00000027,0x00000009,0x0008004f,0x0000001e,0x00000028,0x00000027,0x00000027,0x00000000,
0x00000001,0x00000002,0x00050091,0x0000001e,0x00000029,0x00000026,0x00000028,0x0004003d,
0x00000007,0x0000002a,0x00000009,0x0009004f,0x00000007,0x0000002b,0x0000002a,0x00000029,
0x00000004,0x00000005,0x00000006,0x00000003,0x0003003e,0x00000009,0x0000002b,0x0004003d,
0x00000007,0x0000002c,0x00000009,0x0008004f,0x0000001e,0x0000002d,0x0000002c,0x0000002c,
0x00000000,0x00000001,0x00000002,0x00050041,0x0000002f,0x00000030,0x00000022,0x0000002e,
0x0004003d,0x00000007,0x00000031,0x00000030,0x0008004f,0x0000001e,0x00000032,0x00000031,
0x00000031,0x00000000,0x00000001,0x00000002,0x00050051,0x00000006,0x00000033,0x00000032,
0x00000000,0x00050051,0x00000006,0x00000034,0x00000032,0x00000001,0x00050051,0x00000006,
0x00000035,0x00000032,0x00000002,0x00060050,0x0000001e,0x00000036,0x00000033,0x00000034,
0x00000035,0x0007000c,0x0000001e,0x00000037,0x00000001,0x0000001a,0x0000002d,0x00000036,
0x0004003d,0x00000007,0x00000038,0x00000009,0x0009004f,0x00000007,0x00000039,0x00000038,
0x00000037,0x00000004,0x00000005,0x00000006,0x00000003,0x0003003e,0x00000009,0x00000039,
0x000100fd,0x00010038
};

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -302,9 +341,19 @@ static void ImGui_ImplVulkan_SetupRenderState(ImDrawData* draw_data, VkCommandBu
float translate[2];
translate[0] = -1.0f - draw_data->DisplayPos.x * scale[0];
translate[1] = -1.0f - draw_data->DisplayPos.y * scale[1];
vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
vkCmdPushConstants(command_buffer, g_PipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
sizeof(float) * 0, sizeof(float) * 2, scale);
vkCmdPushConstants(command_buffer, g_PipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
sizeof(float) * 2, sizeof(float) * 2, translate);
}

// upload colour management data
vkCmdPushConstants(command_buffer, g_PipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
sizeof(float) * 4, sizeof(float) * 4*4,
g_VulkanInitInfo.DisplayProfile);
}

// Render function
Expand Down Expand Up @@ -655,9 +704,9 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
{
// Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
VkPushConstantRange push_constants[1] = {};
push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
push_constants[0].offset = sizeof(float) * 0;
push_constants[0].size = sizeof(float) * 4;
push_constants[0].size = sizeof(float) * (4 + 4*4);
VkDescriptorSetLayout set_layout[1] = { g_DescriptorSetLayout };
VkPipelineLayoutCreateInfo layout_info = {};
layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
Expand Down Expand Up @@ -1220,3 +1269,20 @@ void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVul
buffers->Index = 0;
buffers->Count = 0;
}

void ImGui_ImplVulkan_SetDisplayProfile(float gamma[3], float rec2020_to_display[9])
{
memset(g_VulkanInitInfo.DisplayProfile, 0, sizeof(float)*16);
memcpy(g_VulkanInitInfo.DisplayProfile, gamma, sizeof(float)*3);
// glsl will transpose the matrix, so we do the same here:
float *f = g_VulkanInitInfo.DisplayProfile + 4;
f[0] = rec2020_to_display[0];
f[1] = rec2020_to_display[3];
f[2] = rec2020_to_display[6];
f[4] = rec2020_to_display[1];
f[5] = rec2020_to_display[4];
f[6] = rec2020_to_display[7];
f[8] = rec2020_to_display[2];
f[9] = rec2020_to_display[5];
f[10] = rec2020_to_display[8];
}
2 changes: 2 additions & 0 deletions examples/imgui_impl_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct ImGui_ImplVulkan_InitInfo
VkSampleCountFlagBits MSAASamples; // >= VK_SAMPLE_COUNT_1_BIT
const VkAllocationCallbacks* Allocator;
void (*CheckVkResultFn)(VkResult err);
float DisplayProfile[16]; // gamma (vec3+pad) rec2020 to dspy (mat3 padded to 3xvec4) = 16 floats
};

// Called by user code
Expand All @@ -50,6 +51,7 @@ IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, V
IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer);
IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontUploadObjects();
IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
IMGUI_IMPL_API void ImGui_ImplVulkan_SetDisplayProfile(float gamma[3], float rec2020_to_display[9]);


//-------------------------------------------------------------------------
Expand Down

0 comments on commit 608c97e

Please sign in to comment.