/* Test that data values are written and read with proper alignment. */ static void test_alignment(void) { struct blob blob; struct blob_reader reader; uint8_t bytes[] = "ABCDEFGHIJKLMNOP"; size_t delta, last, num_bytes; blob_init(&blob); /* First, write an intptr value to the blob and capture that size. This is * the expected offset between any pair of intptr values (if written with * alignment). */ blob_write_intptr(&blob, (intptr_t) &blob); delta = blob.size; last = blob.size; /* Then loop doing the following: * * 1. Write an unaligned number of bytes * 2. Verify that write results in an unaligned size * 3. Write an intptr_t value * 2. Verify that that write results in an aligned size */ for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) { blob_write_bytes(&blob, bytes, num_bytes); expect_unequal(delta, blob.size - last, "unaligned write of bytes"); blob_write_intptr(&blob, (intptr_t) &blob); expect_equal(2 * delta, blob.size - last, "aligned write of intptr"); last = blob.size; } /* Finally, test that reading also does proper alignment. Since we know * that values were written with all the right alignment, all we have to do * here is verify that correct values are read. */ blob_reader_init(&reader, blob.data, blob.size); expect_equal((intptr_t) &blob, blob_read_intptr(&reader), "read of initial, aligned intptr_t"); for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) { expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes), num_bytes, "unaligned read of bytes"); expect_equal((intptr_t) &blob, blob_read_intptr(&reader), "aligned read of intptr_t"); } blob_finish(&blob); }
void shader_cache_write_program_metadata(struct gl_context *ctx, struct gl_shader_program *prog) { struct disk_cache *cache = ctx->Cache; if (!cache) return; /* Exit early when we are dealing with a ff shader with no source file to * generate a source from. * * TODO: In future we should use another method to generate a key for ff * programs. */ static const char zero[sizeof(prog->data->sha1)] = {0}; if (memcmp(prog->data->sha1, zero, sizeof(prog->data->sha1)) == 0) return; struct blob metadata; blob_init(&metadata); serialize_glsl_program(&metadata, ctx, prog); struct cache_item_metadata cache_item_metadata; cache_item_metadata.type = CACHE_ITEM_TYPE_GLSL; cache_item_metadata.keys = (cache_key *) malloc(prog->NumShaders * sizeof(cache_key)); cache_item_metadata.num_keys = prog->NumShaders; if (!cache_item_metadata.keys) goto fail; char sha1_buf[41]; for (unsigned i = 0; i < prog->NumShaders; i++) { disk_cache_put_key(cache, prog->Shaders[i]->sha1); memcpy(cache_item_metadata.keys[i], prog->Shaders[i]->sha1, sizeof(cache_key)); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1); fprintf(stderr, "marking shader: %s\n", sha1_buf); } } disk_cache_put(cache, prog->data->sha1, metadata.data, metadata.size, &cache_item_metadata); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1_buf, prog->data->sha1); fprintf(stderr, "putting program metadata in cache: %s\n", sha1_buf); } fail: free(cache_item_metadata.keys); blob_finish(&metadata); }
/* Test that we detect overrun. */ static void test_overrun(void) { struct blob blob; struct blob_reader reader; uint32_t value = 0xdeadbeef; blob_init(&blob); blob_write_uint32(&blob, value); blob_reader_init(&reader, blob.data, blob.size); expect_equal(value, blob_read_uint32(&reader), "read before overrun"); expect_equal(false, reader.overrun, "overrun flag not set"); expect_equal(0, blob_read_uint32(&reader), "read at overrun"); expect_equal(true, reader.overrun, "overrun flag set"); blob_finish(&blob); }
/* Test that we can read and write some large objects, (exercising the code in * the blob_write functions to realloc blob->data. */ static void test_big_objects(void) { void *ctx = ralloc_context(NULL); struct blob blob; struct blob_reader reader; int size = 1000; int count = 1000; size_t i; char *buf; blob_init(&blob); /* Initialize our buffer. */ buf = ralloc_size(ctx, size); for (i = 0; i < size; i++) { buf[i] = i % 256; } /* Write it many times. */ for (i = 0; i < count; i++) { blob_write_bytes(&blob, buf, size); } blob_reader_init(&reader, blob.data, blob.size); /* Read and verify it many times. */ for (i = 0; i < count; i++) { expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size, "read of large objects"); } expect_equal(reader.end - reader.data, reader.current - reader.data, "number of bytes read reading large objects"); expect_equal(false, reader.overrun, "overrun flag not set reading large objects"); blob_finish(&blob); ralloc_free(ctx); }
/* Test at least one call of each blob_write_foo and blob_read_foo function, * verifying that we read out everything we wrote, that every bytes is * consumed, and that the overrun bit is not set. */ static void test_write_and_read_functions (void) { struct blob blob; struct blob_reader reader; ssize_t reserved; size_t str_offset, uint_offset; uint8_t reserve_buf[sizeof(reserve_test_str)]; blob_init(&blob); /*** Test blob by writing one of every possible kind of value. */ blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str)); reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str)); blob_overwrite_bytes(&blob, reserved, reserve_test_str, sizeof(reserve_test_str)); /* Write a placeholder, (to be replaced later via overwrite_bytes) */ str_offset = blob.size; blob_write_bytes(&blob, placeholder_str, sizeof(placeholder_str)); blob_write_uint32(&blob, uint32_test); /* Write a placeholder, (to be replaced later via overwrite_uint32) */ uint_offset = blob.size; blob_write_uint32(&blob, uint32_placeholder); blob_write_uint64(&blob, uint64_test); blob_write_intptr(&blob, (intptr_t) &blob); blob_write_string(&blob, string_test_str); /* Finally, overwrite our placeholders. */ blob_overwrite_bytes(&blob, str_offset, overwrite_test_str, sizeof(overwrite_test_str)); blob_overwrite_uint32(&blob, uint_offset, uint32_overwrite); /*** Now read each value and verify. */ blob_reader_init(&reader, blob.data, blob.size); expect_equal_str(bytes_test_str, blob_read_bytes(&reader, sizeof(bytes_test_str)), "blob_write/read_bytes"); blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf)); expect_equal_str(reserve_test_str, (char *) reserve_buf, "blob_reserve_bytes/blob_copy_bytes"); expect_equal_str(overwrite_test_str, blob_read_bytes(&reader, sizeof(overwrite_test_str)), "blob_overwrite_bytes"); expect_equal(uint32_test, blob_read_uint32(&reader), "blob_write/read_uint32"); expect_equal(uint32_overwrite, blob_read_uint32(&reader), "blob_overwrite_uint32"); expect_equal(uint64_test, blob_read_uint64(&reader), "blob_write/read_uint64"); expect_equal((intptr_t) &blob, blob_read_intptr(&reader), "blob_write/read_intptr"); expect_equal_str(string_test_str, blob_read_string(&reader), "blob_write/read_string"); expect_equal(reader.end - reader.data, reader.current - reader.data, "read_consumes_all_bytes"); expect_equal(false, reader.overrun, "read_does_not_overrun"); blob_finish(&blob); }
static void st_serialise_ir_program(struct gl_context *ctx, struct gl_program *prog, bool nir) { if (prog->driver_cache_blob) return; struct blob blob; blob_init(&blob); switch (prog->info.stage) { case MESA_SHADER_VERTEX: { struct st_vertex_program *stvp = (struct st_vertex_program *) prog; blob_write_uint32(&blob, stvp->num_inputs); blob_write_bytes(&blob, stvp->index_to_input, sizeof(stvp->index_to_input)); blob_write_bytes(&blob, stvp->input_to_index, sizeof(stvp->input_to_index)); blob_write_bytes(&blob, stvp->result_to_output, sizeof(stvp->result_to_output)); write_stream_out_to_cache(&blob, &stvp->tgsi); if (nir) write_nir_to_cache(&blob, prog); else write_tgsi_to_cache(&blob, stvp->tgsi.tokens, prog, stvp->num_tgsi_tokens); break; } case MESA_SHADER_TESS_CTRL: case MESA_SHADER_TESS_EVAL: case MESA_SHADER_GEOMETRY: { struct st_common_program *stcp = (struct st_common_program *) prog; write_stream_out_to_cache(&blob, &stcp->tgsi); if (nir) write_nir_to_cache(&blob, prog); else write_tgsi_to_cache(&blob, stcp->tgsi.tokens, prog, stcp->num_tgsi_tokens); break; } case MESA_SHADER_FRAGMENT: { struct st_fragment_program *stfp = (struct st_fragment_program *) prog; if (nir) write_nir_to_cache(&blob, prog); else write_tgsi_to_cache(&blob, stfp->tgsi.tokens, prog, stfp->num_tgsi_tokens); break; } case MESA_SHADER_COMPUTE: { struct st_compute_program *stcp = (struct st_compute_program *) prog; if (nir) write_nir_to_cache(&blob, prog); else write_tgsi_to_cache(&blob, stcp->tgsi.prog, prog, stcp->num_tgsi_tokens); break; } default: unreachable("Unsupported stage"); } blob_finish(&blob); }