Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Review and Unit Tests for RenderPrograms #2

Open
marlitas opened this issue Sep 26, 2023 · 1 comment
Open

Code Review and Unit Tests for RenderPrograms #2

marlitas opened this issue Sep 26, 2023 · 1 comment
Assignees

Comments

@marlitas
Copy link
Contributor

This issue will be used to track review questions/comments as well as unit test commits for RenderPrograms

@marlitas marlitas self-assigned this Sep 26, 2023
marlitas added a commit that referenced this issue Oct 2, 2023
jonathanolson added a commit that referenced this issue Oct 3, 2023
jonathanolson added a commit that referenced this issue Oct 4, 2023
marlitas added a commit that referenced this issue Oct 6, 2023
marlitas added a commit that referenced this issue Oct 10, 2023
jonathanolson added a commit that referenced this issue Oct 10, 2023
…circuit simplify on trivially empty paths, see #2
marlitas added a commit that referenced this issue Oct 10, 2023
@jonathanolson
Copy link
Contributor

If it's helpful, I added some quick non-unit testing code in the alpenglow playground under executeTest():

const program = new phet.alpenglow.RenderLinearBlend(
  phet.dot.v2( 1, 0 ),
  0.25,
  phet.alpenglow.RenderLinearBlendAccuracy.Accurate,
  new phet.alpenglow.RenderColor( phet.dot.v4( 1, 0, 0, 1 ) ),
  new phet.alpenglow.RenderColor( phet.dot.v4( 0, 1, 0, 1 ) )
);

// unit square
const context = new phet.alpenglow.RenderEvaluationContext().set(
  null, 1, phet.dot.v2( 0.5, 0.5 ), 0, 0, 1, 1
);

console.log( 'RenderProgram' );
console.log( program.toRecursiveString() );

const instructions = [];
program.writeInstructions( instructions );
console.log( '(object) instructions' );
console.log( instructions.map( instruction => instruction.toString() ).join( '\n' ) );

const encoder = new phet.alpenglow.ByteEncoder();
phet.alpenglow.RenderInstruction.instructionsToBinary( encoder, instructions );

console.log( '(binary) instructions' );
console.log( encoder.getDebug32String() );

const fromBinaryInstructions = phet.alpenglow.RenderInstruction.binaryToInstructions( encoder, 0 );
console.log( '(object) instructions (from binary)' );
console.log( fromBinaryInstructions.map( instruction => instruction.toString() ).join( '\n' ) );

const instructionsEqual = phet.alpenglow.RenderInstruction.instructionsEquals( instructions, fromBinaryInstructions );
console.log( 'instructions before/after binary conversion equal?', instructionsEqual );

const executor = new phet.alpenglow.RenderExecutor();
executor.loadInstructions( instructions );

const result = phet.dot.v4( 0, 0, 0, 0 );
executor.execute( context, result );
console.log( 'evaluation (instructions)', result );

const directResult = program.evaluate( context );
console.log( 'evaluation (RenderProgram)', directResult );

const simplifiedProgram = program.simplified();

const simplifiedResult = simplifiedProgram.evaluate( context );
console.log( 'evaluation (RenderProgram simplified)', simplifiedResult );

console.log( 'evaluations equal', result.equalsEpsilon( directResult, 1e-6 ) && result.equalsEpsilon( simplifiedResult, 1e-6 ) );

When run:
image

It takes a RenderProgram and essentially tests two things:

  1. Converts it to instruction form (RenderInstruction), converts THAT to binary instruction form (prints it out), converts THAT back into instruction form (RenderInstruction), and compares to make sure the two arrays of RenderInstructions are equivalent.
  2. Evaluates the original RenderProgram, evaluates the instruction form (RenderInstruction), and evaluates the simplified RenderProgram, and compares to see if it gets the same results.

This should work for almost all RenderPrograms (RenderImage not yet in binary form, since I'll need to figure out what info is needed, and RenderPathBoolean/RenderDepthSort should always be split/replaced away and won't need a binary form).

If this were executed by a range of valid RenderPrograms in unit tests (and ideally fuzzed with randomly-generated RenderPrograms), that would be amazing!

marlitas added a commit that referenced this issue Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants