Skip to content

Commit

Permalink
Automatically restrict shader colours (#207)
Browse files Browse the repository at this point in the history
Adapted generate-and-run so that it no longer focuses on reference+variant, but just processes a stream of variants.  Furthermore, changed generation so that fragment shader colours are restricted to a small palette, so that a test oracle can automatically look for deviations from this palette.
  • Loading branch information
afd authored and paulthomson committed Mar 6, 2019
1 parent 9081181 commit 0b902b2
Show file tree
Hide file tree
Showing 9 changed files with 632 additions and 127 deletions.
Expand Up @@ -17,4 +17,20 @@
package com.graphicsfuzz.common.ast.visitors;

public class AbortVisitationException extends RuntimeException {

/**
* Simple form of the exception, with no message.
*/
public AbortVisitationException() {

}

/**
* Provide a message to explain why visitation was aborted.
* @param message A message providing details of the abort.
*/
public AbortVisitationException(String message) {
super(message);
}

}
@@ -0,0 +1,107 @@
/*
* Copyright 2019 The GraphicsFuzz Project Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphicsfuzz.common.util;

import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;

public class ImageColorComponents {

private static final int R_OFFSET = 16;
private static final int G_OFFSET = 8;
private static final int B_OFFSET = 0;
private static final int A_OFFSET = 24;

/**
* Determines whether the given buffered image comprises pixels whose components come only from
* the given component values.
* @param image The image to be considered.
* @param allowedComponentValues The allowed component values.
* @return True if and only if the pixels in the image only use the given components.
*/
public static boolean containsOnlyGivenComponentValues(BufferedImage image,
List<Integer> allowedComponentValues) {
final int[] colors = getRgb(image);
for (int color : colors) {
for (int componentValue : Arrays.asList(getComponentR(color), getComponentG(color),
getComponentB(color), getComponentA(color))) {
if (!allowedComponentValues.contains(componentValue)) {
return false;
}
}
}
return true;
}

/**
* Gets the R component from a pixel.
* @param pixel A pixel.
* @return The pixel's R component.
*/
public static int getComponentR(int pixel) {
return getComponent(pixel, R_OFFSET);
}

/**
* Gets the G component from a pixel.
* @param pixel A pixel.
* @return The pixel's G component.
*/
public static int getComponentG(int pixel) {
return getComponent(pixel, G_OFFSET);
}

/**
* Gets the B component from a pixel.
* @param pixel A pixel.
* @return The pixel's B component.
*/
public static int getComponentB(int pixel) {
return getComponent(pixel, B_OFFSET);
}

/**
* Gets the A component from a pixel.
* @param pixel A pixel.
* @return The pixel's A component.
*/
public static int getComponentA(int pixel) {
return getComponent(pixel, A_OFFSET);
}

/**
* Returns an array of pixel values for the given image.
* @param image Image for which pixel values are required.
* @return Pixel values for the image.
*/
public static int[] getRgb(BufferedImage image) {
return image.getRGB(
0,
0,
image.getWidth(),
image.getHeight(),
null,
0,
image.getWidth());
}

private static int getComponent(int pixel, int componentBitOffset) {
return (pixel >> componentBitOffset) & 0xff;
}

}
Expand Up @@ -757,6 +757,18 @@ public void writeStringToFile(File file, String contents) throws IOException {
FileUtils.writeStringToFile(file, contents, Charset.defaultCharset());
}

/**
* Provides an in-memory representation of the image associated with a shader job result.
* Assumes that an image file is present as part of the shader job result.
* @param shaderJobResultFile The shader job for which a result image is to be processed.
* @return In-memory representation of image.
* @throws IOException on absence of an image file or failing to read the file.
*/
public BufferedImage getBufferedImageFromShaderJobResultFile(File shaderJobResultFile)
throws IOException {
return ImageIO.read(getUnderlyingImageFileFromShaderJobResultFile(shaderJobResultFile));
}

private static void assertIsShaderJobFile(File shaderJobFile) {
if (!shaderJobFile.getName().endsWith(".json")
|| shaderJobFile.getName().endsWith(".info.json")) {
Expand Down
@@ -0,0 +1,62 @@
/*
* Copyright 2019 The GraphicsFuzz Project Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.graphicsfuzz.imagetools;

import com.graphicsfuzz.common.util.ImageColorComponents;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

public class CheckColorComponents {

public static void main(String[] args) throws ArgumentParserException, IOException {

final ArgumentParser parser = ArgumentParsers.newArgumentParser("CheckColorComponents")
.defaultHelp(true)
.description("Exits with code 0 if and only if the given image uses only the given color "
+ "components as the RGBA values of its pixels.");

// Required arguments
parser.addArgument("image")
.help("Path to PNG image")
.type(File.class);
parser.addArgument("components")
.type(Integer.class)
.nargs("+")
.help("Allowed components, each in range 0..255.");

final Namespace ns = parser.parseArgs(args);

final File image = ns.get("image");
final List<Integer> components = ns.get("components");

if (components.stream().anyMatch(item -> item < 0 || item > 255)) {
System.err.println("Error: given component list " + components + " includes elements not "
+ "in range 0..255.");
}

if (!ImageColorComponents.containsOnlyGivenComponentValues(ImageIO.read(image), components)) {
System.exit(1);
}
}

}
4 changes: 0 additions & 4 deletions generate-and-run-shaders/pom.xml
Expand Up @@ -61,10 +61,6 @@ limitations under the License.
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.graphicsfuzz</groupId>
<artifactId>util</artifactId>
Expand Down
Expand Up @@ -76,11 +76,6 @@ private static Namespace parse(String[] args) throws ArgumentParserException {
.help("File containing crash strings to ignore, one per line.")
.type(File.class);

parser.addArgument("--only-variants")
.help("Only run variant shaders (so sacrifice finding wrong images in favour of crashes.")
.type(Boolean.class)
.action(Arguments.storeTrue());

return parser.parseArgs(args);

}
Expand Down Expand Up @@ -116,7 +111,8 @@ public static void mainHelper(String[] args)
? ShadingLanguageVersion.webGlFromVersionString(ns.get("glsl_version"))
: ShadingLanguageVersion.fromVersionString(ns.get("glsl_version"));

final BlockingQueue<Pair<ShaderJob, ShaderJob>> queue =
// Queue of shader jobs to be processed.
final BlockingQueue<ShaderJob> queue =
new LinkedBlockingQueue<>();

final File crashStringsToIgnoreFile = ns.get("ignore_crash_strings");
Expand All @@ -136,18 +132,13 @@ public static void mainHelper(String[] args)
outputDir,
ns.get("server"),
ns.get("worker"),
shadingLanguageVersion,
crashStringsToIgnore,
ns.get("only_variants"),
fileOps));
consumer.start();

final int seed = ArgsUtil.getSeedArgument(ns);

final Thread producer = new Thread(new ShaderProducer(
LIMIT,
shaderJobFiles,
new RandomWrapper(seed),
queue,
referencesDir,
shadingLanguageVersion,
Expand Down

0 comments on commit 0b902b2

Please sign in to comment.