Smarter headless mode

parent 90009436
...@@ -31,7 +31,7 @@ $(BIULDDIR)/ui.o: $(SRCDIR)/ui.c ...@@ -31,7 +31,7 @@ $(BIULDDIR)/ui.o: $(SRCDIR)/ui.c
$(CC) -c $(CFLAGS) $(DEBUG_FLAGS) $< -o $@ $(CC) -c $(CFLAGS) $(DEBUG_FLAGS) $< -o $@
clean: clean:
rm mandelbrot $(OBJS) rm fast mandelbrot $(OBJS)
fast: $(SRCDIR)/mandelbrot.c $(OBJS) fast: $(SRCDIR)/mandelbrot.c $(OBJS)
$(CC) $(CFLAGS) $(LDLIBS) $^ -o $@ $(CC) $(CFLAGS) $(LDLIBS) $^ -o $@
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <complex.h> #include <complex.h>
#include <omp.h> #include <omp.h>
#include <stdlib.h>
#include <x86intrin.h> #include <x86intrin.h>
#include <argp.h> #include <argp.h>
...@@ -173,62 +174,20 @@ calculate_set_omp(Array *arr, int height, int width, ViewInfo view, short unsign ...@@ -173,62 +174,20 @@ calculate_set_omp(Array *arr, int height, int width, ViewInfo view, short unsign
} }
void void
update_texture(App *app, Array *arr) { update_texture(int width, int height, SDL_Texture *texture, Array *arr) {
void *pixels; void *pixels;
int pitch; int pitch;
if (SDL_LockTexture(app->texture, NULL, &pixels, &pitch) != 0) { if (SDL_LockTexture(texture, NULL, &pixels, &pitch) != 0) {
fprintf(stderr, "Failed to lock texture: %s\n", SDL_GetError()); fprintf(stderr, "Failed to lock texture: %s\n", SDL_GetError());
return; return;
} }
memcpy(pixels, arr->pointer, app->win_width * app->win_height * sizeof(Color)); memcpy(pixels, arr->pointer, width* height * sizeof(Color));
SDL_UnlockTexture(app->texture); SDL_UnlockTexture(texture);
} }
void
save_texture(const char* file_name, SDL_Renderer* renderer, SDL_Texture* texture) {
SDL_Texture* target = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, texture);
int width, height;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
SDL_RenderReadPixels(renderer, NULL, surface->format->format, surface->pixels, surface->pitch);
IMG_SavePNG(surface, file_name);
SDL_FreeSurface(surface);
SDL_SetRenderTarget(renderer, target);
}
void
render_cl(Array *arr, App *app, ViewInfo view, short unsigned use_avx, short unsigned use_omp) {
void (*calculate_set_func)(Array *arr, int height, int width,
ViewInfo view, short unsigned use_avx);
if (use_omp == 0)
calculate_set_func = calculate_set;
else
calculate_set_func = calculate_set_omp;
SDL_SetRenderDrawColor(app->renderer, 0, 0, 50, 255);
SDL_RenderClear(app->renderer);
if (arr->size < app->win_height * app->win_width * (int)sizeof(Color)){
arr->pointer = realloc(arr->pointer, app->win_height * app->win_width * sizeof(Color));
arr->size = app->win_height * app->win_width * sizeof(Color);
}
if (app->needs_recalc){
calculate_set_func(arr, app->win_height, app->win_width, view, use_avx);
app->needs_recalc = 0;
}
update_texture(app, arr);
SDL_RenderCopy(app->renderer, app->texture, NULL, NULL);
}
struct arguments struct arguments
{ {
...@@ -320,6 +279,69 @@ parse_opt (int key, char *arg, struct argp_state *state) ...@@ -320,6 +279,69 @@ parse_opt (int key, char *arg, struct argp_state *state)
static struct argp argp = {options, parse_opt, NULL, NULL}; static struct argp argp = {options, parse_opt, NULL, NULL};
void
render_cl(Array *arr, SDL_Texture *texture, SDL_Renderer *renderer, int width, int height, ViewInfo *view, short unsigned use_avx, short unsigned use_omp, short unsigned *needs_recalc) {
void (*calculate_set_func)(Array *arr, int height, int width,
ViewInfo view, short unsigned use_avx);
calculate_set_func = use_omp ? calculate_set_omp : calculate_set;
SDL_SetRenderDrawColor(renderer, 0, 0, 50, 255);
SDL_RenderClear(renderer);
if (arr->size < height * width * (int)sizeof(Color)){
arr->pointer = realloc(arr->pointer, height * width * sizeof(Color));
arr->size = height * width * sizeof(Color);
}
if (needs_recalc){
calculate_set_func(arr, height, width, *view, use_avx);
*needs_recalc = 0;
}
update_texture(width, height, texture, arr);
SDL_RenderCopy(renderer, texture, NULL, NULL);
}
short unsigned
render_headless(ViewInfo view, const char* filename,
int width, int height,
short unsigned use_avx, short unsigned use_omp) {
SDL_Surface* surface = SDL_CreateRGBSurface(0,
width, height,
32,
0, 0, 0, 0);
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(surface);
SDL_Texture* texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
width, height);
if (!surface) {
fprintf(stderr, "Failed to create surface: %s\n", SDL_GetError());
return 1;
}
Array arr = {
malloc(width * height * sizeof(Color)),
width * height * sizeof(Color)
};
void (*calculate_set_func)(Array*, int, int, ViewInfo, short unsigned);
calculate_set_func = use_omp ? calculate_set_omp : calculate_set;
short unsigned int plug;
render_cl(&arr, texture, renderer, width, height, &view, use_avx, use_omp, &plug);
IMG_SavePNG(surface, filename);
free(arr.pointer);
SDL_FreeSurface(surface);
return 0;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
...@@ -345,6 +367,14 @@ main(int argc, char **argv) ...@@ -345,6 +367,14 @@ main(int argc, char **argv)
.zoom = args.zoom .zoom = args.zoom
}; };
if (args.output_file) {
render_headless(view, args.output_file,
args.width, args.height,
args.use_avx, args.use_omp);
SDL_Quit();
return 0;
}
App app = { App app = {
SDL_CreateWindow("Mandelbrot set exploer", SDL_CreateWindow("Mandelbrot set exploer",
0, 0, 0, 0,
...@@ -458,13 +488,8 @@ main(int argc, char **argv) ...@@ -458,13 +488,8 @@ main(int argc, char **argv)
//drawing happens here //drawing happens here
SDL_SetRenderDrawColor(app.renderer, 0xff, 0xff, 0xff, 0xff); SDL_SetRenderDrawColor(app.renderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderClear(app.renderer); SDL_RenderClear(app.renderer);
render_cl(&screen, &app, view, args.use_avx, args.use_omp);
// if outputfile arg given bail after 1 frame rendered and dumb to png render_cl(&screen, app.texture, app.renderer, app.win_width, app.win_height, &view, args.use_avx, args.use_omp, &app.needs_recalc);
if (args.output_file != NULL){
save_texture(args.output_file, app.renderer, app.texture);
running = 0;
}
int msec = SDL_GetTicks() - frame_start; int msec = SDL_GetTicks() - frame_start;
float fps = 0; float fps = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment