WEAK bool CacheEntry::init(const uint8_t *cache_key, size_t cache_key_size, uint32_t key_hash, const buffer_t &computed_buf, int32_t tuples, buffer_t **tuple_buffers) { next = NULL; more_recent = NULL; less_recent = NULL; key_size = cache_key_size; hash = key_hash; in_use_count = 0; tuple_count = tuples; key = (uint8_t *)halide_malloc(NULL, key_size); if (key == NULL) { return false; } computed_bounds = computed_buf; computed_bounds.host = NULL; computed_bounds.dev = 0; for (size_t i = 0; i < key_size; i++) { key[i] = cache_key[i]; } for (int32_t i = 0; i < tuple_count; i++) { buffer(i) = *tuple_buffers[i]; } return true; }
WEAK buffer_t copy_of_buffer(void *user_context, const buffer_t &buf) { buffer_t result = buf; size_t buffer_size = full_extent(result); // TODO: ERROR RETURN result.host = (uint8_t *)halide_malloc(user_context, buffer_size * result.elem_size); copy_from_to(user_context, buf, result); return result; }
WEAK int halide_default_device_and_host_malloc(void *user_context, struct buffer_t *buf, const halide_device_interface *device_interface) { size_t size = buf_size(buf); buf->host = (uint8_t *)halide_malloc(user_context, size); if (buf->host == NULL) { return -1; } int result = halide_device_malloc(user_context, buf, device_interface); if (result != 0) { halide_free(user_context, buf->host); buf->host = NULL; } return result; }
WEAK void CacheEntry::init(const uint8_t *cache_key, size_t cache_key_size, uint32_t key_hash, const buffer_t &computed_buf, int32_t tuples, buffer_t **tuple_buffers) { next = NULL; more_recent = NULL; less_recent = NULL; key_size = cache_key_size; hash = key_hash; tuple_count = tuples; // TODO: ERROR RETURN key = (uint8_t *)halide_malloc(NULL, key_size); computed_bounds = computed_buf; computed_bounds.host = NULL; computed_bounds.dev = 0; for (size_t i = 0; i < key_size; i++) { key[i] = cache_key[i]; } for (int32_t i = 0; i < tuple_count; i++) { buffer_t *buf = tuple_buffers[i]; buffer(i) = copy_of_buffer(NULL, *buf); } }
// Copy image data from texture back to host memory. EXPORT int halide_opengl_copy_to_host(void *user_context, buffer_t *buf) { CHECK_INITIALIZED(1); if (!buf->dev_dirty) { return 0; } if (!buf->host || !buf->dev) { #ifdef DEBUG print_buffer(user_context, buf); #endif halide_error(user_context, "Invalid copy_to_host operation"); return 1; } GLuint tex = get_texture_id(buf); #ifdef DEBUG halide_printf(user_context, "halide_copy_to_host: %d\n", tex); #endif GLint format; GLint type; if (!get_texture_format(user_context, buf, &format, &type)) { halide_error(user_context, "Invalid texture format\n"); return 1; } GLint width = buf->extent[0]; GLint height = buf->extent[1]; ST.BindTexture(GL_TEXTURE_2D, tex); CHECK_GLERROR(1); bool is_interleaved = (buf->stride[2] == 1 && buf->stride[0] == buf->extent[2]); if (is_interleaved) { // TODO: GL_UNPACK_ROW_LENGTH ST.PixelStorei(GL_PACK_ROW_LENGTH, buf->extent[1]); ST.PixelStorei(GL_PACK_ALIGNMENT, 1); ST.GetTexImage(GL_TEXTURE_2D, 0, format, type, buf->host); CHECK_GLERROR(1); } else { #ifdef DEBUG halide_printf(user_context, "Warning: In copy_to_host, host buffer is not interleaved. Doing slow deinterleave.\n"); #endif size_t size = width * height * buf->extent[2] * buf->elem_size; uint8_t *tmp = (uint8_t*)halide_malloc(user_context, size); ST.PixelStorei(GL_PACK_ALIGNMENT, 1); ST.GetTexImage(GL_TEXTURE_2D, 0, format, type, tmp); CHECK_GLERROR(1); switch (type) { case GL_UNSIGNED_BYTE: interleaved_to_halide<uint8_t>(buf, (uint8_t*)tmp, width, height, buf->extent[2]); break; case GL_UNSIGNED_SHORT: interleaved_to_halide<uint16_t>(buf, (uint16_t*)tmp, width, height, buf->extent[2]); break; case GL_FLOAT: interleaved_to_halide<float>(buf, (float*)tmp, width, height, buf->extent[2]); break; } halide_free(user_context, tmp); } ST.BindTexture(GL_TEXTURE_2D, 0); buf->dev_dirty = false; return 0; }