From 2fd7797e7d511831f7b17b240120976ef59d8525 Mon Sep 17 00:00:00 2001 From: ktyl Date: Tue, 21 Jun 2022 00:41:52 +0100 Subject: [PATCH] add option to stop time --- launch | 7 ++++--- makefile | 6 +++++- src/cam.c | 4 +--- src/cam.h | 2 +- src/clock.c | 4 ++-- src/clock.h | 7 ++++++- src/gfx.c | 10 ---------- src/main.c | 48 ++++++++++++++++++++++++++++++++++++------------ src/sphere.c | 5 +---- src/sphere.h | 2 +- 10 files changed, 57 insertions(+), 38 deletions(-) diff --git a/launch b/launch index 11dc6e1..c2977be 100755 --- a/launch +++ b/launch @@ -1,12 +1,13 @@ #!/usr/bin/env bash executable="$1" +options="$2" prime="prime-run" if command -v $prime &> /dev/null then - echo "launching $executable with nvidia prime offloading" - $prime $executable + echo "launching $executable $options with nvidia prime offloading" + $prime $executable $options else - $executable + $executable $options fi diff --git a/makefile b/makefile index 04775bb..6b586b4 100644 --- a/makefile +++ b/makefile @@ -46,7 +46,11 @@ clean: -rm -r $(BIN_DIR) -rm */*.o +# TODO: extract run-* out to like, python scripts? arguments can be implemented with argparse to +# manage simple interface with C binary run: $(TARGET) - $(LAUNCH) $(TARGET) + $(LAUNCH) $(TARGET) 1.0 +run-converge: $(TARGET) + $(LAUNCH) $(TARGET) 0.0 .PHONY: run clean diff --git a/src/cam.c b/src/cam.c index a2864e5..cb87a0b 100644 --- a/src/cam.c +++ b/src/cam.c @@ -2,10 +2,8 @@ const float FOVY = 90.0; -void updateCameraUniforms(GLuint shaderProgram, float aspect) +void updateCameraUniforms(GLuint shaderProgram, float aspect, float t) { - float t = now(); - // wobble up dir vec3 cdir, cright, cup; vec3 up = {0.1*sin(t),1.0,0.05*cos(0.5*t)}; diff --git a/src/cam.h b/src/cam.h index 4004a75..a066326 100644 --- a/src/cam.h +++ b/src/cam.h @@ -6,4 +6,4 @@ #include "clock.h" -void updateCameraUniforms(GLuint shaderProgram, float aspect); +void updateCameraUniforms(GLuint shaderProgram, float aspect, float t); diff --git a/src/clock.c b/src/clock.c index 00c36b3..3307417 100644 --- a/src/clock.c +++ b/src/clock.c @@ -1,6 +1,6 @@ #include "clock.h" -float now() +float now(struct Epoch t) { - return (float)SDL_GetTicks() / 1000.0; + return t.speed * (float)SDL_GetTicks() / 1000.0; } diff --git a/src/clock.h b/src/clock.h index c7786c5..1b6f85e 100644 --- a/src/clock.h +++ b/src/clock.h @@ -2,5 +2,10 @@ #include +struct Epoch +{ + float speed; +}; + // seconds since program start -float now(); +float now(struct Epoch e); diff --git a/src/gfx.c b/src/gfx.c index 306a4cb..461185b 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -174,16 +174,6 @@ void setVertexAttributes() // color glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3*sizeof(float))); glEnableVertexAttribArray(1); - - - - - - - - - - // uv glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6*sizeof(float))); glEnableVertexAttribArray(2); diff --git a/src/main.c b/src/main.c index 14140a1..ffcef95 100644 --- a/src/main.c +++ b/src/main.c @@ -10,12 +10,13 @@ const int WIDTH = 420; const int HEIGHT = 420; -void updateUniforms(GLuint shaderProgram); +void updateUniforms(GLuint shaderProgram, float t); SDL_Window *window; struct Shaders shaders; struct Textures textures; +struct Epoch epoch; void initialise() { @@ -32,15 +33,29 @@ void initialise() createTextures(WIDTH, HEIGHT, shaders, &textures); } -int main() +void parseArgs(int argc, char* argv[], struct Epoch* e) { + // check we have the right number of arguments + if (argc != 2) + { + fprintf(stderr, "usage: oglc TIMESPEED\n"); + } + + sscanf(argv[1], "%f", &(e->speed)); +} + +int main(int argc, char* argv[]) +{ + parseArgs(argc, argv, &epoch); + initialise(); - float start = now(); + float start = now(epoch); int frames; for (frames = 0; !checkQuit(); frames++) { GLuint shader; + float t = now(epoch); // prepass // TODO: write output to different texture than main output @@ -48,18 +63,17 @@ int main() glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures.g0); glUseProgram(shader); - updateUniforms(shader); + updateUniforms(shader, t); glDispatchCompute((GLuint)WIDTH, (GLuint)HEIGHT, 1); // make sure we're finished writing to the texture before trying to read it glMemoryBarrier(GL_ALL_BARRIER_BITS); - // dispatch compute shaders shader = shaders.lighting; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures.target); glUseProgram(shader); - updateUniforms(shader); + updateUniforms(shader, t); //int loc = glGetUniformLocation(shader, "_g0"); //glUniform1i(loc, textures.g0); glDispatchCompute((GLuint)WIDTH, (GLuint)HEIGHT, 1); @@ -67,6 +81,18 @@ int main() // make sure we're finished writing to the texture before trying to read it glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + // TODO: frame blending + // + // before we get to our normal rendering pass we need to blend our samples + // across multiple frames. to do this, we should maintain an accumulation + // buffer where we can store pixel data while a new frame is being generated. + // this can be a collection of two buffers, where one is written to and the + // other is read from. to render a new frame, the newly sampled raw frame can + // be combined with data from the previous frame. by repeatedly taking the + // average over a large number of frames a cleaner image can be generated. + // this is most effective for static images, and will produce motion blur on + // a moving image. + // normal drawing pass shader = shaders.quad; glUseProgram(shader); @@ -78,7 +104,7 @@ int main() SDL_GL_SwapWindow(window); } - float elapsed = now()-start; + float elapsed = now(epoch)-start; printf("%d frames in %fs [%f fps]\n", frames, elapsed, @@ -87,7 +113,7 @@ int main() return 0; } -void updateUniforms(GLuint shaderProgram) +void updateUniforms(GLuint shaderProgram, float t) { // update random values vec4 seed = @@ -100,19 +126,17 @@ void updateUniforms(GLuint shaderProgram) int loc = glGetUniformLocation(shaderProgram, "_seed"); glUniform4fv(loc, 1, seed); - // update time - float t = now(); float sin_t = sin(t); loc = glGetUniformLocation(shaderProgram, "_t"); glUniform4f(loc, t, sin_t, (1.0 + sin_t)*0.5, 0.0f); // update camera float aspect = (float)WIDTH/(float)HEIGHT; - updateCameraUniforms(shaderProgram, aspect); + updateCameraUniforms(shaderProgram, aspect, t); // make and update spheres const int sphereCount = 42; struct Sphere spheres[sphereCount]; - makeSpheres(spheres, sphereCount); + makeSpheres(spheres, sphereCount, t); updateSphereUniforms(shaderProgram, spheres, sphereCount); } diff --git a/src/sphere.c b/src/sphere.c index 3121e9d..7a1d8f8 100644 --- a/src/sphere.c +++ b/src/sphere.c @@ -1,9 +1,7 @@ #include "sphere.h" -void makeSpheres(struct Sphere *spheres, int count) +void makeSpheres(struct Sphere *spheres, int count, float t) { - //float t = time(); - vec3 albedos[] = { {0.0,0.0,1.0}, @@ -21,7 +19,6 @@ void makeSpheres(struct Sphere *spheres, int count) float x; vec3 sc = {0.0,0.0,1.0}; - float t = now(); for (int i = 0; i < count; i++) { x = 2.0*CGLM_PI * (float)i/(float)count; diff --git a/src/sphere.h b/src/sphere.h index 67579ec..e7b0f12 100644 --- a/src/sphere.h +++ b/src/sphere.h @@ -16,7 +16,7 @@ struct Sphere int material; }; -void makeSpheres(struct Sphere *spheres, int count); +void makeSpheres(struct Sphere *spheres, int count, float t); struct Sphere makeSphere(vec3 center, float radius, vec3 albedo, int material); //void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere); void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count);