From 78dc71bda71fc8eb7accfcf68d010a0fea1a7921 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sat, 7 Mar 2015 11:21:07 +0100 Subject: Add bucket cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stores accumulation buckets. Use case: Run n seconds, check image noise levels, run again (with cache and thus previous results), check image agin, … --- rect.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'rect.c') diff --git a/rect.c b/rect.c index d988ed1..34bbc7e 100644 --- a/rect.c +++ b/rect.c @@ -194,9 +194,72 @@ void bucket_init (bucket * const b, const uint2 dim) { int ret = posix_memalign ((void **) &b->data, sizeof (*b->data), size); assert (ret == 0); assert (b->data != NULL); + memset (b->data, 0, size); } +/* just a random 32 bit value */ +#define BUCKET_CACHE_IDENT 0x252007d2 + +/* Read bucket from file + */ +bool bucket_deserialize (bucket * const b, const char *file) { + FILE *fd = fopen (file, "r"); + if (fd == NULL) { + return false; + } + + uint32_t ident; + size_t ret = fread (&ident, sizeof (ident), 1, fd); + assert (ret == 1); + assert (ident == BUCKET_CACHE_IDENT); + + uint32_t w, h; + ret = fread (&w, sizeof (w), 1, fd); + assert (ret == 1); + ret = fread (&h, sizeof (h), 1, fd); + assert (ret == 1); + assert (b->dim[0] == w && b->dim[1] == h); + + uint64_t samples, badvals; + ret = fread (&samples, sizeof (samples), 1, fd); + assert (ret == 1); + ret = fread (&badvals, sizeof (badvals), 1, fd); + assert (ret == 1); + b->samples = samples; + b->badvals = badvals; + + ret = fread (b->data, sizeof (*b->data), w*h, fd); + assert (ret == w*h); + + fclose (fd); + + return true; +} + +/* Write bucket into a file + */ +void bucket_serialize (bucket * const b, const char *file) { + FILE *fd = fopen (file, "w"); + assert (fd != NULL); + + uint32_t ident = BUCKET_CACHE_IDENT; + fwrite (&ident, sizeof (ident), 1, fd); + + assert (sizeof (b->dim[0]) >= sizeof (uint32_t)); + fwrite (&b->dim[0], sizeof (uint32_t), 1, fd); + fwrite (&b->dim[1], sizeof (uint32_t), 1, fd); + + assert (sizeof (b->samples) >= sizeof (uint64_t)); + assert (sizeof (b->badvals) >= sizeof (uint64_t)); + fwrite (&b->samples, sizeof (uint64_t), 1, fd); + fwrite (&b->badvals, sizeof (uint64_t), 1, fd); + + fwrite (b->data, sizeof (*b->data), b->dim[0]*b->dim[1], fd); + + fclose (fd); +} + static void compute_camera (const flam3_genome * const genome, const bucket * const bucket, render_constants * const c) { assert (genome != NULL); -- cgit v1.2.3