1/* 2 * Copyright © Microsoft Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#include "dxil_buffer.h" 25#include <assert.h> 26 27void 28dxil_buffer_init(struct dxil_buffer *b, unsigned abbrev_width) 29{ 30 blob_init(&b->blob); 31 b->buf = 0; 32 b->buf_bits = 0; 33 34 b->abbrev_width = abbrev_width; 35} 36 37void 38dxil_buffer_finish(struct dxil_buffer *b) 39{ 40 blob_finish(&b->blob); 41} 42 43static bool 44flush_dword(struct dxil_buffer *b) 45{ 46 assert(b->buf_bits >= 32 && b->buf_bits < 64); 47 48 uint32_t lower_bits = b->buf & UINT32_MAX; 49 if (!blob_write_bytes(&b->blob, &lower_bits, sizeof(lower_bits))) 50 return false; 51 52 b->buf >>= 32; 53 b->buf_bits -= 32; 54 55 return true; 56} 57 58bool 59dxil_buffer_emit_bits(struct dxil_buffer *b, uint32_t data, unsigned width) 60{ 61 assert(b->buf_bits < 32); 62 assert(width > 0 && width <= 32); 63 assert((data & ~((UINT64_C(1) << width) - 1)) == 0); 64 65 b->buf |= ((uint64_t)data) << b->buf_bits; 66 b->buf_bits += width; 67 68 if (b->buf_bits >= 32) 69 return flush_dword(b); 70 71 return true; 72} 73 74bool 75dxil_buffer_emit_vbr_bits(struct dxil_buffer *b, uint64_t data, 76 unsigned width) 77{ 78 assert(width > 1 && width <= 32); 79 80 uint32_t tag = UINT32_C(1) << (width - 1); 81 uint32_t max = tag - 1; 82 while (data > max) { 83 uint32_t value = (data & max) | tag; 84 data >>= width - 1; 85 86 if (!dxil_buffer_emit_bits(b, value, width)) 87 return false; 88 } 89 90 return dxil_buffer_emit_bits(b, data, width); 91} 92 93bool 94dxil_buffer_align(struct dxil_buffer *b) 95{ 96 assert(b->buf_bits < 32); 97 98 if (b->buf_bits) { 99 b->buf_bits = 32; 100 return flush_dword(b); 101 } 102 103 return true; 104} 105