Skip to content

Commit

Permalink
Fix input handling.
Browse files Browse the repository at this point in the history
makevk2dfd.pl has been made "strict" and "warning" free.
Since the related fixes did not fix the input problem,
the same was not done to makedfd2vk.pl.
  • Loading branch information
MarkCallow committed Oct 27, 2023
1 parent b18a2f8 commit 61009ed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
16 changes: 13 additions & 3 deletions lib/dfdutils/makedfd2vk.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
# N.B. 0 arguments, read stdin, write stdout.
# 1 argument, read ARGV[0], write stdout.
# 2 arguments, read ARGV[0], write ARGV[1].
if (@ARGV > 1) {
open (my $output, '>', $ARGV[1]);
my $infile = shift @ARGV;
my $outfile = shift @ARGV;
my $input;
if (defined $infile) {
open($input, '<', $infile);
} else {
$input = *STDIN;
}
if (defined $outfile) {
open (my $output, '>', $outfile);
select $output;
}

Expand All @@ -27,7 +35,9 @@
print " *************************************************************************/\n";

# Loop over each line of input
while ($line = <>) {
# IMPORTANT: Do not use `<>` as that reads all files given on the
# command line.
while ($line = <$input>) {

# Match any format that starts with a channel description (some number
# of R, G, B, A or a number). # In PERL, "=~" performs a regex
Expand Down
74 changes: 46 additions & 28 deletions lib/dfdutils/makevk2dfd.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,40 @@
# Copyright 2019-2020 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

use strict;
use warnings;

# N.B. 0 arguments, read stdin, write stdout.
# 1 argument, read ARGV[0], write stdout.
# 2 arguments, read ARGV[0], write ARGV[1].
if (@ARGV > 1) {
open (my $output, '>', $ARGV[1]);
my $infile = shift @ARGV;
my $outfile = shift @ARGV;
my $input;
if (defined $infile) {
open($input, '<', $infile);
} else {
$input = *STDIN;
}
if (defined $outfile) {
open (my $output, '>', $outfile);
select $output;
}

# Endianness is a parameter to the (non-block-compressed) generators
# This doesn't have to be a number: $bigEndian = "myBigEndianFlag" will drop this argument in the generated code
$bigEndian = 0;
my $bigEndian = 0;

# Keep track of formats we've seen to avoid duplicates
%foundFormats = ();
my %foundFormats = ();

# Check if we've processed a format. Returns true for extension formats
# that have been promoted to core when the core format has been processed.
# Usually only _KHR and _EXT extensions are promoted so only those are
# checked.
sub formatProcessed {
$format =$_[0];
$format_noext = $format;
$format_noext =~ s/(_EXT|KHR)$//;
my $format =$_[0];
my $format_noext = $format;
$format_noext =~ s/_(EXT|KHR)$//;

return (exists($foundFormats{$format})
|| exists($foundFormats{$format_noext}));
Expand All @@ -40,11 +51,17 @@ sub formatProcessed {
print " *************************************************************************/\n\n";

# Loop over each line of input
while ($line = <>) {
# IMPORTANT: Do not use `<>` as that reads all files given on the
# command line.
while (my $line = <$input>) {
# Match any format that starts with a channel description (some number of R, G, B, A or a number)
# In PERL, "=~" performs a regex operation on the left argument
# m/<regex>/ matches the regular expression

my $format = "";
my $suffix = "";
my $channels = "";
my $scheme = "";
# VK_FORMAT_ is a simple 422 format
if ($line =~ m/(VK_FORMAT_[RGBX0-9]+_422_UNORM(_4PACK16)?)/) {
$format = $1; # Extract the format identifier from the rest of the line
Expand All @@ -61,20 +78,19 @@ sub formatProcessed {
# VK_FORMAT_B16G16R16G16_422_UNORM,

$format =~ m/VK_FORMAT_([RGBX0-9]+)_422_UNORM(_4PACK16)?/;
$formatChannels = $1;
my $formatChannels = $1;

$channels = "";
$bits = "";
$paddings = "";
$position_xs = "";
$position_ys = "";
$last_green = 1;
my $bits = "";
my $paddings = "";
my $position_xs = "";
my $position_ys = "";
my $last_green = 1;

while ($formatChannels =~ m/([RGBX0-9]*)([RGB])([0-9]+)X?([0-9]+)?/) {
# Processing from right to left for ease of regex expression
$channel = $2;
$bit = $3;
$padding = $4;
my $channel = $2;
my $bit = $3;
my $padding = $4 ? $4 : "";

if ($channels ne "") {
$channels = ", " . $channels;
Expand Down Expand Up @@ -162,8 +178,8 @@ sub formatProcessed {
# N.B. We don't care about the total bit count (after "PACK" in the name)

# Create an empty array of channel names and corresponding bits
@packChannels = ();
@packBits = ();
my @packChannels = ();
my @packBits = ();

# Loop over channels, separating out the last letter followed by a number
while ($channels =~ m/([RGBA0-9]*)([RGBA])([0-9]+)/) {
Expand All @@ -179,13 +195,13 @@ sub formatProcessed {
}

# The number of channels we've found is the array length we've built
$numChannels = @packChannels;
my $numChannels = @packChannels;

# Packed output needs a C block for local variables
print "case $format: {\n";

# Start with a null list of channel ids
$channelIds = "";
my $channelIds = "";

# Loop over the channel names we've found
foreach (@packChannels) {
Expand All @@ -202,7 +218,7 @@ sub formatProcessed {

# Channel bit counts are easier: we can use join() to make a comma-separated
# string of the numbers in the array
$channelBits = join (',', @packBits);
my $channelBits = join (',', @packBits);

# Print initialisation for the two arrays we've created
print " int channels[] = {" . $channelIds . "}; ";
Expand All @@ -223,7 +239,7 @@ sub formatProcessed {

# Extract our "suffix" (e.g. "UNORM") and bytes per channel
$suffix = $2;
$bytesPerChannel = $1 / 8;
my $bytesPerChannel = $1 / 8;

# Output the case entry
print "case $format: return createDFDAlpha($bigEndian, $bytesPerChannel, s_$suffix);\n";
Expand All @@ -232,6 +248,8 @@ sub formatProcessed {
# Extract our "channels" (e.g. "B8G8R8") and "suffix" (e.g. "UNORM")
$channels = $1;
$suffix = $2;
my $rbswap = 0;
my $numChannels = 0;

# Non-packed format either start with red (R8G8B8A8) or blue (B8G8R8A8)
# We have a special case to notice when we start with blue
Expand Down Expand Up @@ -269,7 +287,7 @@ sub formatProcessed {
$channels =~ m/R([0-9]+)/;

# For unpacked, we need bytes per channel, not bits
$bytesPerChannel = $1 / 8;
my $bytesPerChannel = $1 / 8;

# Output the case entry
print "case $format: return createDFDUnpacked($bigEndian, $numChannels, $bytesPerChannel, $rbswap, s_$suffix);\n";
Expand Down Expand Up @@ -384,9 +402,9 @@ sub formatProcessed {

# Finally, ASTC and PVRTC, the only cases where the block size is a parameter
} elsif ($line =~ m/VK_FORMAT_ASTC_([0-9]+)x([0-9]+)(x([0-9]+))?_([A-Z]+)_BLOCK(_EXT)?/) {
$w = $1;
$h = $2;
$d = $4 ? $4 : '1';
my $w = $1;
my $h = $2;
my $d = $4 ? $4 : '1';
$suffix = $5;
print "case $format: return createDFDCompressed(c_ASTC, $w, $h, $d, s_$suffix);\n";

Expand Down

0 comments on commit 61009ed

Please sign in to comment.