KKMMMTTT / AstroSoar

0 stars 0 forks source link

[#8] Implemented BatchTextureContext #11

Closed MatthewChrobak closed 4 years ago

MatthewChrobak commented 4 years ago

Implements and closes #8

Benchmark

For rending 1,000,000 32x32 tiles from the same texture, here are the results I observe on a DEBUG build. Approach Observed Memory Consumption FPS
Batch ~400mb 48 fps
TextureContext ~600mb 0.5 fps

Benchmark code for BatchTextureContext

Benchmark Texture

image

Benchmark Scene

    public class MainMenu : Scene
    {
        private readonly BatchTextureContext batch;

        public MainMenu() {
            var rng = new Random();

            byte nextByte() {
                return (byte)rng.Next(0, 256);
            }

            var pos = new List<(float, float)>();
            var rect = new List<(int, int, int, int)>();
            var size = new List<(float, float)>();
            var color = new List<RGBA>();
            for (int y = 0; y < 1000; y++) {
                for (int x = 0; x < 1000; x++) {
                    pos.Add((x * 32, y * 32));
                    size.Add((32, 32));
                    rect.Add((rng.Next(0, 2) * 32, rng.Next(0, 2) * 32, 32, 32));
                    var randomColor = new RGBA(nextByte(), nextByte(), nextByte());
                    color.Add(randomColor);
                }
            }

            batch = new BatchTextureContext("tiles.png", pos.ToArray(), size.ToArray(), rect.ToArray(), color.ToArray());
        }
        public override void Draw(ICanvas canvas) {
            canvas.Draw(batch);
        }
    }

Observed View

image

kwreen commented 4 years ago

Just commenting with a question to make sure I understand the change though: the goal of BatchTextureContext is to generate randomized pixels for a scene texture?

MatthewChrobak commented 4 years ago

Just commenting with a question to make sure I understand the change though: the goal of BatchTextureContext is to generate randomized pixels for a scene texture?

This BatchTextureContext will let you draw the same texture in different locations, different sizes, and different color tints, in a single Draw call. This feature will be useful for your #4 task where you'll need to render a star texture hundreds of times in different locations in a scene.