srp
software rendering pipeline
Loading...
Searching...
No Matches
Rasterization

Primitive rasterization functions. More...

Collaboration diagram for Rasterization:

Files

file  fragment.c
 
file  fragment.h
 
file  line.c
 
file  line.h
 
file  point.c
 
file  point.h
 
file  triangle.c
 
file  triangle.h
 

Classes

struct  SRPLine
 
struct  SRPPoint
 
struct  SRPTriangle
 
typedef struct SRPLine SRPLine
 
typedef struct SRPPoint SRPPoint
 
typedef struct SRPTriangle SRPTriangle
 
static bool scissorTest (size_t x, size_t y)
 
static bool stencilTest (uint8_t *stencil, const SRPStencilFaceState *s)
 
static bool stencilCompare (uint8_t ref, uint8_t stored, uint8_t mask, SRPCompareOp op)
 
static void stencilFailOp (uint8_t *stencil, const SRPStencilFaceState *s)
 
static void stencilDepthFailOp (uint8_t *stencil, const SRPStencilFaceState *s)
 
static void stencilPassOp (uint8_t *stencil, const SRPStencilFaceState *s)
 
static uint8_t applyStencilOp (SRPStencilOp op, uint8_t stored, uint8_t ref)
 
static bool depthTest (float incoming, float stored)
 
void emitFragment (const SRPFramebuffer *fb, const SRPShaderProgram *sp, int x, int y, SRPFragmentShaderIn *fsIn)
 
static void lineInterpolateData (SRPLine *line, float t, const SRPShaderProgram *restrict sp, SRPInterpolated *pInterpolatedBuffer, float *depth, float *recIntInvW)
 
void rasterizeLine (SRPLine *line, const SRPFramebuffer *fb, const SRPShaderProgram *restrict sp, void *interpolatedBuffer)
 
void setupLine (SRPLine *line, const SRPFramebuffer *fb)
 
static bool computeMathAndRasterBoundaries (vec3 ss, float pointSize, const SRPFramebuffer *fb, vec2 *outMinBP, vec2 *outMaxBP, int *outMinX, int *outMaxX, int *outMinY, int *outMaxY)
 
void rasterizePoint (SRPPoint *point, const SRPFramebuffer *fb, const SRPShaderProgram *restrict sp)
 
void setupPoint (SRPPoint *p)
 
static float signedAreaParallelogram (const vec3 *restrict a, const vec3 *restrict b)
 
static bool shouldCullTriangle (const SRPTriangle *tri, bool *isCCW, bool *isFrontFacing)
 
static void triangleChangeWinding (SRPTriangle *tri)
 
static void calculateBarycentrics (SRPTriangle *tri, float areaX2, vec2 point)
 
static bool isEdgeFlatTopOrLeft (const vec3 *restrict edge)
 
static void triangleInterpolateData (SRPTriangle *tri, const SRPShaderProgram *restrict sp, SRPInterpolated *pInterpolatedBuffer, float *depth, float *recIntInvW)
 
void rasterizeTriangle (SRPTriangle *tri, const SRPFramebuffer *fb, const SRPShaderProgram *restrict sp, void *interpolatedBuffer)
 
bool setupTriangle (SRPTriangle *tri, const SRPFramebuffer *fb)
 

Detailed Description

Primitive rasterization functions.

Typedef Documentation

◆ SRPLine

typedef struct SRPLine SRPLine

Line primitive. Stores data needed for its rasterization

◆ SRPPoint

typedef struct SRPPoint SRPPoint

Point primitive. Stores data needed for its rasterization

◆ SRPTriangle

typedef struct SRPTriangle SRPTriangle

Triangle primitive. Stores data needed for its rasterization

Function Documentation

◆ applyStencilOp()

static uint8_t applyStencilOp ( SRPStencilOp  op,
uint8_t  stored,
uint8_t  ref 
)
inlinestatic

Apply an operation to a stencil value

◆ calculateBarycentrics()

static void calculateBarycentrics ( SRPTriangle tri,
float  areaX2,
vec2  point 
)
static

Calculate barycentric coordinates for a point and barycentric coordinates' delta values.

Parameters
[in]triTriangle to calculate barycentric coordinates for. Must have its ss and edge fields initialized.
[in]areaX2The triangle's area multiplied by 2 (to avoid division)
[in]pointPoint to calculate barycentric coordinates for (screen-space)

◆ computeMathAndRasterBoundaries()

static bool computeMathAndRasterBoundaries ( vec3  ss,
float  pointSize,
const SRPFramebuffer fb,
vec2 outMinBP,
vec2 outMaxBP,
int *  outMinX,
int *  outMaxX,
int *  outMinY,
int *  outMaxY 
)
static

Given screen-space point position, compute its math and raster boundaries.

Parameters
[in]ssScreen-space point position
[in]pointSizePoint size, in pixels
[in]fbPointer to the framebuffer, used to clip partially-OOB points
[out]outMinBP,outMaxBPBounding points representing mathematical boundaries
[out]outMinX,outMaxX,outMinY,outMaxYRaster boundaries
Returns
true if point is fully OOB, false otherwise

◆ depthTest()

static bool depthTest ( float  incoming,
float  stored 
)
inlinestatic

Perform a depth test, respecting the user-set compare operation and whether or not the test is enabled

Parameters
[in]incomingThe depth value of the currently processed fragment
[in]storedThe depth value stored in the depth buffer
Returns
true if depth test passes, false otherwise

◆ emitFragment()

void emitFragment ( const SRPFramebuffer fb,
const SRPShaderProgram sp,
int  x,
int  y,
SRPFragmentShaderIn fsIn 
)

Perform (early) depth check, run fragment shader, and draw a pixel

Parameters
[in]fbThe framebuffer to use
[in]spThe shader program to use
[in]x,yCoordinates of the pixel
[in]fsInFragment shader input

◆ isEdgeFlatTopOrLeft()

static bool isEdgeFlatTopOrLeft ( const vec3 *restrict  edge)
static

Check if a triangle's edge is flat top or left. Assumes counter-clockwise vertex order!

Parameters
[in]edgeA pointer to an edge vector (pointing from one vertex to the other)
Returns
Whether or not this edge is flat top or left

◆ lineInterpolateData()

static void lineInterpolateData ( SRPLine line,
float  t,
const SRPShaderProgram *restrict  sp,
SRPInterpolated pInterpolatedBuffer,
float *  depth,
float *  recIntInvW 
)
static

Interpolate the fragment position and vertex variables inside the line.

Parameters
[in]lineLine to interpolate data for
[in]tInterpolation parameter (0 -> 0th vertex; 1 -> 1st vertex)
[in]spA pointer to shader program to use
[out]pInterpolatedBufferA pointer to the buffer where interpolated variables will appear. Must be big enough to hold all of them
[out]depthFragment depth
[out]recIntInvWReciprocal of interpolated inverse Wclip

◆ rasterizeLine()

void rasterizeLine ( SRPLine line,
const SRPFramebuffer fb,
const SRPShaderProgram *restrict  sp,
void *  interpolatedBuffer 
)

Rasterize a point

Parameters
[in]linePointer to the line to draw
[in]fbThe framebuffer to draw to
[in]spThe shader program to use
[in]interpolatedBufferPointer to a temporary buffer where varyings for each fragment will be stored. Must be big enough to hold all interpolated attributes for ONE vertex.

◆ rasterizePoint()

void rasterizePoint ( SRPPoint point,
const SRPFramebuffer fb,
const SRPShaderProgram *restrict  sp 
)

Rasterize a point

Parameters
[in]pointPointer to the point to draw
[in]fbThe framebuffer to draw to
[in]spThe shader program to use

◆ rasterizeTriangle()

void rasterizeTriangle ( SRPTriangle triangle,
const SRPFramebuffer fb,
const SRPShaderProgram *restrict  sp,
void *  interpolatedBuffer 
)

Rasterize a triangle

Parameters
[in]trianglePointer to the triangle to draw
[in]fbThe framebuffer to draw to
[in]spThe shader program to use
[in]interpolatedBufferPointer to a temporary buffer where varyings for each fragment will be stored. Must be big enough to hold all interpolated attributes for ONE vertex.

◆ scissorTest()

static bool scissorTest ( size_t  x,
size_t  y 
)
inlinestatic

Perform a scissor test, respecting the user-set runtime options

Parameters
[in]x,yWindow-space position of the currently processed fragment
Returns
true if scissor test passes, false otherwise

◆ setupLine()

void setupLine ( SRPLine line,
const SRPFramebuffer fb 
)

Setup line for rasterization, performing perspective divide and calculating internal variables

Parameters
[in]lineThe line to set up. Its v field is required to be filled
[in]fbThe framebuffer to use for NDC to screen-space conversion

◆ setupPoint()

void setupPoint ( SRPPoint p)

Perform perspective divide on a point. Created to be structurally similar to setupTriangle(), setupLine()

Parameters
[in]pPoint to setup

◆ setupTriangle()

bool setupTriangle ( SRPTriangle tri,
const SRPFramebuffer fb 
)

Setup triangle for rasterization, performing perspective divide and calculating internal variables

Parameters
[in]triThe triangle to set up. Its v field is required to be filled
[in]fbThe framebuffer to use for NDC to screen-space conversion
Returns
false if it is culled and should not be rasterized, true otherwise

◆ shouldCullTriangle()

static bool shouldCullTriangle ( const SRPTriangle tri,
bool *  isCCW,
bool *  isFrontFacing 
)
static

Determine if a triangle should be culled (back-face culling)

Parameters
[in]triTriangle to check. Must have its p_ndc field initialized.
[out]isCCWWhether or not the triangle's vertices are in counter-clockwise order
[out]isFrontFacingWhether or not the triangle is front facing
Returns
Whether or not the triangle should be culled

◆ signedAreaParallelogram()

static float signedAreaParallelogram ( const vec3 *restrict  a,
const vec3 *restrict  b 
)
static

Get a parallelogram's signed area. The two vectors define a parallelogram. Used for barycentric coordinates' initialization in calculateBarycentrics()

◆ stencilCompare()

static bool stencilCompare ( uint8_t  ref,
uint8_t  stored,
uint8_t  mask,
SRPCompareOp  op 
)
inlinestatic

Compare two masked stencil values with a certain operation

Parameters
[in]refThe reference stencil value
[in]storedThe stored stencil value
[in]maskThe mask to use with both values
[in]opOperation to compare with

◆ stencilDepthFailOp()

static void stencilDepthFailOp ( uint8_t *  stencil,
const SRPStencilFaceState s 
)
inlinestatic

Apply a "stencil test passed, depth test failed" operation to a stencil value

Parameters
[in]stencilPointer to the current stencil value
[in]sPointer to the stencil state to use

◆ stencilFailOp()

static void stencilFailOp ( uint8_t *  stencil,
const SRPStencilFaceState s 
)
inlinestatic

Apply a "stencil test failed" operation to a stencil value

Parameters
[in]stencilPointer to the current stencil value
[in]sPointer to the stencil state to use

◆ stencilPassOp()

static void stencilPassOp ( uint8_t *  stencil,
const SRPStencilFaceState s 
)
inlinestatic

Apply a "stencil and depth tests passed" operation to a stencil value

Parameters
[in]stencilPointer to the current stencil value
[in]sPointer to the stencil state to use

◆ stencilTest()

static bool stencilTest ( uint8_t *  stencil,
const SRPStencilFaceState s 
)
inlinestatic

Perform an initial stage of the stencil test and potentially apply fail operation to the stencil value. Checks if the stencil test is enabled.

Parameters
[in]stencilPointer to the current stencil value
[in]sPointer to the stencil state to use

◆ triangleChangeWinding()

static void triangleChangeWinding ( SRPTriangle tri)
static

Change the winding order of a triangle.

Parameters
[in]triTriangle to change the winding order of. Must have its v, p_ndc and invW fields initialized.

◆ triangleInterpolateData()

static void triangleInterpolateData ( SRPTriangle tri,
const SRPShaderProgram *restrict  sp,
SRPInterpolated pInterpolatedBuffer,
float *  depth,
float *  recIntInvW 
)
static

Interpolate the fragment position and vertex variables inside the triangle.

Parameters
[in]triTriangle to interpolate data for
[in]spA pointer to shader program to use
[out]pInterpolatedBufferA pointer to the buffer where interpolated variables will appear. Must be big enough to hold all of them
[out]depthFragment depth
[out]recIntInvWReciprocal of interpolated inverse Wclip