Skip to content

Commit

Permalink
Allow A8B8G8R8 formats. (#861)
Browse files Browse the repository at this point in the history
Fixes issue #860.

Updates the switches used by the the GL loader to those generated by
`generate_format_switches.rb` in the KTX-Specification repo as a way to
get them updated for these and some other recently added Vulkan formats.
The glloader now loads the newly allowed formats.

VkFormat declarations for Java and Python are now generated by
`mkvkformatfiles` which has been moved to ci_scripts since it now
generates files outside the `lib` directory. The unattractive alternative
was to hand edit these files to add the newly allowed formats.

CI testing of vkformat-related file generation (`mkvk`) has been moved
to a dedicated GitHub workflow and removed from the main CI builds. This
is because testing now requires a clone of the KTX-Specification repo due
to the use of `generate_format_switches.rb`.

The change to `createdfd.c` is to remove a duplicated line of code and
is unrelated to the purpose of this PR.

KhronosGroup/KTX-Software-CTS#19 removes the
tests for these as prohibited and adds new tests for the formats.
  • Loading branch information
MarkCallow authored Feb 24, 2024
1 parent 83b6386 commit 5ac5cf1
Show file tree
Hide file tree
Showing 26 changed files with 1,178 additions and 497 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/check-mkvk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2015-2020 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0
name: KTX-Software Check mkvk CI

# Seems no way to avoid duplicating this on logic in each .yml file.
# See https://github.com/actions/starter-workflows/issues/245.
on:
# Trigger the workflow on a pull request,
pull_request:

push:
# And on pushes to main, which will occur when a PR is merged.
branches:
- main
# Also trigger on push of release tags to any branch. Useful
# for testing release builds before merging to main.
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
- 'v[0-9]+.[0-9]+.[0-9]+-*'
paths-ignore:
- .appveyor.yml
- .travis.yml

# Allow manual trigger
workflow_dispatch:

jobs:
check-mkvk:
strategy:
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]

runs-on: ${{ matrix.os }}
env:
BUILD_DIR: "build"
WERROR: ON

steps:
- uses: actions/checkout@v4

- name: Clone KTX-Specification repo
run: |
pushd ..; git clone https://github.com/KhronosGroup/KTX-Specification.git; popd
- name: Configure CMake build
run: cmake -B ${{ env.BUILD_DIR }} -DKTX_GENERATE_VK_FILES=ON -DKTX_FEATURE_TESTS=OFF -DKTX_FEATURE_TOOLS=OFF -DKTX_WERROR=${{ env.WERROR }}
- name: Test file generation
run: |
cmake --build ${{ env.BUILD_DIR }} --target mkvk --clean-first
git diff --quiet HEAD
5 changes: 0 additions & 5 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ jobs:
sse: ON, opencl: ON}
]
include:
- os: windows-latest
generator: 'Visual Studio 17 2022'
toolset: CLangCL
arch: x64
check_mkvk: ONLY
- os: windows-2019
generator: 'Visual Studio 16 2019'
toolset: v142
Expand Down
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ env:
# FEATURE_PY is off for arm64 macOS because we'd need a cross-compiled version
# of Python for it to correctly build the Python extensions (the CI
# runs on x86_64)
- CHECK_MKVK=ONLY

- CONFIGURATION=Debug,Release PLATFORM=macOS ARCHS=x86_64
FEATURE_DOC=ON FEATURE_JNI=ON FEATURE_PY=ON FEATURE_LOADTESTS=OpenGL+Vulkan FEATURE_TOOLS=ON
FEATURE_TOOLS_CTS=ON LOADTESTS_USE_LOCAL_DEPENDENCIES=ON SUPPORT_SSE=ON SUPPORT_OPENCL=OFF DEPLOY_DOCS=YES PACKAGE=YES
Expand Down Expand Up @@ -89,10 +87,6 @@ jobs:
dist: jammy
env:
- CHECK_REUSE: ONLY
- os: linux
dist: jammy
env:
- CHECK_MKVK: ONLY
- os: linux
dist: jammy
addons:
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ set(KTX_MAIN_SRC
lib/texture2.c
lib/texture2.h
lib/uthash.h
lib/vk2gl.h
lib/vk_format.h
lib/vkFormat2glFormat.inl
lib/vkFormat2glInternalFormat.inl
lib/vkFormat2glType.inl
lib/vkformat_check.c
lib/vkformat_enum.h
lib/vkformat_str.c
Expand Down
132 changes: 103 additions & 29 deletions lib/mkvkformatfiles → ci_scripts/mkvkformatfiles
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
#! /usr/bin/awk -f
#! /usr/bin/env -S awk -f
# Copyright 2019-2024 Mark Callow
# SPDX-License-Identifier: Apache-2.0

# Usage: mkvkformatfiles [output-dir path/to/vulkan_core]
#
# output_dir defaults to the current directory.
# path/to/vulkan_core defaults to ENVIRON["Vulkan_INCLUDE_DIR"]. If not
# set, default is lib/dfdutils/vulkan/vulkan_core.h.
#
# When specifying path/to/vulkan_core, output-dir must also be
# specified.

# This script creates 5 files from vulkan/vulkan_core.h:
#
# - vkformat_enum.h, the VkFormat enum declaration for those who don't
# want to include vulkan_core.h.
# - vkformat_list.inl, a list of VkFormat enum names for use in
# initializing tables, etc.
# - vkformat_str.c, a switch statement for converting VkFormat enums to
# - lib/vkformat_enum.h: the VkFormat enum declaration for those who don't
# want to include vulkan_core.h. extension token names for
# extensions that have been moved to core are omitted.
#
# - lib/vkformat_str.c: a switch statement for converting VkFormat enums to
# to strings corresponding to the format names.
# - vkformat_typesize.c, function to return the type-size for each
# - lib/vkformat_typesize.c, function to return the type-size for each
# format.
# - vkformat_check.c, 2 functions: 1 to checking if a format is prohibited in
# KTX2 and the other to see if a format is valid.
# KTX2.
#
# - lib/vkformat_check.c: 2 functions: 1 to check if a format is prohibited in
# KTX2 and the other to see if a format is a valid Vulkan format.
#
# - interface/python_binding/pyktx/vk_format.py, VkFormat enumerators for
# Python.
#
# - interface/java_binding/src/main/java/org/khronos/ktx/VkFormat.java,
# VkFormat enumerators for Java.
#
# - tests/unittests/vkformat_list.inl, a list of VkFormat enum names for use in
# initializing tables, etc.
#
# KTX v2 prohibited formats are excluded from the last three files.

BEGIN {
if (index(tolower(ENVIRON["OS"]), "windows") > 0) {
ORS = "\r\n";
}
# ARGV[0] is "awk".
if (ARGC > 1) {
output_dir = ARGV[1] "/";
delete ARGV[1];
} else {
output_dir = "./";
}
# awk reads files whose names are found in the positional parameters,
# hence setting ARGV[1] here and deleting a possible ARGV[1] above.
if (ARGC == 1) {
if (ENVIRON["Vulkan_INCLUDE_DIR"]) {
ARGV[1] = ENVIRON["Vulkan_INCLUDE_DIR"] "/vulkan/vulkan_core.h"; ARGC = 2
} else {
# Use local vulkan_core.h until ASTC 3D texture extension is released.
# ARGV[1] = "/usr/include";
ARGV[1] = "lib/dfdutils/vulkan/vulkan_core.h"; ARGC = 2
}
}

# Hard-coded paths are somewhat unfortunate but major changes in
# the source code organization are unlikely.
format_hdr = output_dir "lib/vkformat_enum.h"
format_typesize = output_dir "lib/vkformat_typesize.c"
format_inl = output_dir "tests/unittests/vkformat_list.inl"
format_strings = output_dir "lib/vkformat_str.c"
format_check = output_dir "lib/vkformat_check.c"
format_java = output_dir "interface/java_binding/src/main/java/org/khronos/ktx/VkFormat.java"
format_python = output_dir "interface/python_binding/pyktx/vk_format.py"

vulkan_core = "vulkan_core.h"
processing_core_formats = 0
processing_extension_formats = 0
Expand All @@ -33,33 +82,21 @@ BEGIN {
extension_formats = ""
end_range = ""
max_std_format_enum = 0
java = "package org.khronos.ktx;" ORS "public class VkFormat {" ORS
prohibited = "#include <stdint.h>" ORS "#include <stdbool.h>" ORS ORS
prohibited = prohibited "#include \"vkformat_enum.h\"" ORS ORS
prohibited = prohibited "bool" ORS "isProhibitedFormat(VkFormat format)" ORS "{" ORS
prohibited = prohibited " switch (format) {" ORS;
python = "from enum import IntEnum" ORS ORS "class VkFormat(IntEnum):" ORS
python = python " \"\"\"Vulkan texture format constants.\"\"\"" ORS ORS
valid = "bool" ORS "isValidFormat(VkFormat format)" ORS "{" ORS
valid = valid " // On MSVC VkFormat can be a signed integer" ORS
valid = valid " if ((uint32_t) format <= VK_FORMAT_MAX_STANDARD_ENUM)" ORS
valid = valid " return true;" ORS " else switch(format) {" ORS
if (ARGC == 2) {
output_dir = ARGV[1] "/";
}
format_hdr = output_dir "vkformat_enum.h"
format_typesize = output_dir "vkformat_typesize.c"
format_inl = output_dir "vkformat_list.inl"
format_strings = output_dir "vkformat_str.c"
format_check = output_dir "vkformat_check.c"
if (ENVIRON["Vulkan_INCLUDE_DIR"]) {
ARGV[1] = ENVIRON["Vulkan_INCLUDE_DIR"] "/vulkan/vulkan_core.h"; ARGC = 2
} else {
# Use local vulkan_core.h until ASTC 3D texture extension is released.
# ARGV[1] = "/usr/include";
ARGV[1] = "lib/dfdutils/vulkan/vulkan_core.h"; ARGC = 2
}
}

# A range pattern to extract the copyright message.
/\*\* Copyright*/,/\*\// { copyright = copyright $0 ORS }
/\*\* Copyright*/,/\*\// { copyright = copyright $0 ORS; }

$2 == "VK_HEADER_VERSION" {
banner = ORS
Expand Down Expand Up @@ -89,8 +126,12 @@ $2 == "VK_HEADER_VERSION" {
if (enum_val+0 > max_std_format_enum) {
max_std_format_enum = enum_val+0;
}
if ($1 !~ /UNDEFINED/) {
format_name_list = format_name_list $1 "," ORS
if ($1 !~ /SCALED/) {
if ($1 !~ /UNDEFINED/) {
format_name_list = format_name_list $1 "," ORS
}
java = java " public static final int " $1 " = " enum_val ";" ORS
python = python " " $1 " = " enum_val ORS
}
}
}
Expand All @@ -99,7 +140,7 @@ $2 == "VK_HEADER_VERSION" {
}
}

/.*SCALED|A8B8G8R8_.*_PACK32/ { prohibited = prohibited " case " $1 ":" ORS; }
/.*SCALED/ { prohibited = prohibited " case " $1 ":" ORS; }
#/A8B8G8R8_.*_PACK32/ { prohibited = prohibited " case " $1 ":" ORS; }
# Multiplane formats.
/VK_FORMAT_[^F]/ && (/PLANE/ || /420/) {
Expand All @@ -113,6 +154,10 @@ $2 == "VK_HEADER_VERSION" {
/VK_FORMAT_[^F].* = 1000/ && ! /PLANE/ && !/420/ {
valid = valid " case " $1 ":" ORS;
format_name_list = format_name_list $1 "," ORS
enum_val = $3;
sub(/,$/, "", enum_val);
java = java " public static final int " $1 " = " enum_val ";" ORS
python = python " " $1 " = " enum_val ORS
}

function removePrefix(string, prefix) {
Expand Down Expand Up @@ -177,9 +222,31 @@ function write_source_file(body, filename) {
print body > filename
}

function write_python_source_file(body, filename) {
pybanner = banner;
sub(/\/\*/, "#***", pybanner);
sub(/\*\//, "****", pybanner);
sub(/ Auto/, "# Auto", pybanner);
regexp = ORS " \\*";
sub(regexp, ORS "#", pybanner);

pycopyright = copyright;
regexp = "/\\*" ORS;
sub(regexp, "", pycopyright);
gsub(/\*\*/, "#", pycopyright);
regexp = "\\*/" ORS;
sub(regexp, "", pycopyright);

print pybanner > filename
print pycopyright > filename
print body > filename
}

END {
# vkformat_enum.h
write_header_file("_VKFORMAT_ENUM_H_", "VULKAN_CORE_H_", format_decl ORS end_range, format_hdr);

# vkformat_list.inl
write_source_file(format_name_list ORS, format_inl)

# vkformat_typesize.c
Expand Down Expand Up @@ -255,6 +322,13 @@ END {
end_str2vk = " return VK_FORMAT_UNDEFINED;" ORS;
end_str2vk = end_str2vk "}"
write_source_file(prelude switch_body_vk2str postscript begin_str2vk switch_body_str2vk end_str2vk, format_strings);

# VkFormat.java
end_java = "}" ORS
write_source_file(java end_java, format_java);

# vk_format.py
write_python_source_file(python, format_python);
}

# vim:ai:ts=4:sts=4:sw=2:expandtab:textwidth=70
Expand Down
Loading

0 comments on commit 5ac5cf1

Please sign in to comment.