Setting up profiling etc

This commit is contained in:
Carlos Sanchez 2024-07-23 19:37:02 -04:00
parent be3d3e4b25
commit 415b817013
4 changed files with 64 additions and 0 deletions

View File

@ -31,6 +31,11 @@ func NewFramebuffer(width uint, height uint) Framebuffer {
} }
} }
// Sure hope this gets inlined...
func (fb *Framebuffer) Set(x uint, y uint, color uint) {
fb.Data[x+y*fb.Width] = color
}
// Given some image data, return a string that is the ppm of it // Given some image data, return a string that is the ppm of it
func (fb *Framebuffer) ExportPPM() string { func (fb *Framebuffer) ExportPPM() string {
var result strings.Builder var result strings.Builder

View File

@ -1,8 +1,11 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"os"
"runtime/pprof" // For performance whatever
) )
const ( const (
@ -10,9 +13,41 @@ const (
Height = 512 Height = 512
) )
func must(err error) {
if err != nil {
panic(err)
}
}
// However flag works... idk
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() { func main() {
log.Printf("Program start") log.Printf("Program start")
// Little section for doing cpu profiling. I guess that's all you have to do?
flag.Parse()
if *cpuprofile != "" {
log.Printf("CPU profiling requested, write to %s", *cpuprofile)
f, err := os.Create(*cpuprofile)
must(err)
defer f.Close()
err = pprof.StartCPUProfile(f)
must(err)
defer pprof.StopCPUProfile()
}
fb := NewFramebuffer(Width, Height) fb := NewFramebuffer(Width, Height)
log.Printf("Running render")
// Just draw a simple line
LineDumb(&fb, 0xFFFFFF, 100, 100, 350, 200)
LineDumb2(&fb, 0xFF0000, 120, 100, 200, 350)
LineDumb2(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
log.Printf("Exporting ppm to stdout")
fmt.Print(fb.ExportPPM()) fmt.Print(fb.ExportPPM())
log.Printf("Program end") log.Printf("Program end")
} }

21
tinyrender1/render.go Normal file
View File

@ -0,0 +1,21 @@
package main
// Draw a line using dumb
func LineDumb(fb *Framebuffer, color uint, x0 uint, y0 uint, x1 uint, y1 uint) {
var t float32
for t = 0; t < 1; t += 0.01 {
// Very simple interpolation between x and y
x := x0 + uint(float32(x1-x0)*t)
y := y0 + uint(float32(y1-y0)*t)
fb.Set(x, y, color)
}
}
func LineDumb2(fb *Framebuffer, color uint, x0 uint, y0 uint, x1 uint, y1 uint) {
for x := x0; x < x1; x++ {
// For each pixel across, compute how far across we are and interpolate y
t := float32(x-x0) / float32(x1-x0)
y := uint(float32(y0)*(1-t) + float32(y1)*t)
fb.Set(x, y, color)
}
}

3
tinyrender1/run.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
go build && ./tinyrender1 >image.ppm