
<!-- I release these notes into the public domain -->

# 2D Graphics (`gfx2d`)

## Table of Contents

<!-- TOC -->
* [Home](./CONTENTS.md#planning-documentation-for-fennec)
* [2D Graphics (`gfx2d`)](#2d-graphics-gfx2d)
  * [Table of Contents](#table-of-contents)
  * [Introduction](#introduction)
    * [Structures (`gfx2d`)](#structures-gfx2d)
<!-- TOC -->


## Introduction

&ensp; This system handles the rendering of 2D meshes and textures.

Links:
- https://en.wikipedia.org/wiki/Quadtree
- https://developer.nvidia.com/gpugems/gpugems3/part-iv-image-effects/chapter-25-rendering-vector-art-gpu


### Structures (`gfx2d`)

For the 2d rendering framework, Materials need to be rendered independently because we have
no size constraints for images. This disallows us from using a meta-shader like in
the 3d rendering framework.

I may have come up with a solution for the above problem. We can define software
subsections for images that are of differing size, then fit them to the optimal
texture array size. The textures will be centered in the storage, then have padding
that fits one of the wrapping models supported by samplers

```c++
struct Object
{
    vec2 location, scale; // A matrix would be 36 bytes, this is instead 20 bytes
    float rotation;
}
```

- BVH
    - Quadtree
        - Leaf Size and Tree Depth should be calculated by the scene, constraints are as follows:
            - Min Object Size
            - Max Object Size
            - Scene Center
            - Scene Edge
        - Insertions and Updates are done on the CPU
        - Nodes
            - Start Index 32-bits
            - Object Count 32-bits
        - Objects
            - Buffer of Object IDs grouped by Octree Node
    - Culling
        - Starting at each Octree Leaf, traverse upwards.
        - Insert Visible Leaf IDs
            - Track using atomic buffer
    - Generate the Command Buffer for Culled Meshes from the Visible Leaf Buffer
        - Count Materials
        - Count Meshes per Material
    - Generate the Culled Object Buffer by copying objects from the Object Buffer
        - Adjust Buffer Size using the counts
        - Insert using another atomic buffer

- Translucent objects will be sorted. We can cheat by using a z-index instead of a z-coordinate.
  This will allow us to sort objects as they are created. We can still bulk render each z-index,
  with meshes and objects being grouped by material.

