1 /* 2 * Copyright 2008 Corbin Simpson <[email protected]> 3 * Copyright 2010 Marek Olšák <[email protected]> 4 * SPDX-License-Identifier: MIT 5 */ 6 7 /** 8 * This file contains macros for building command buffers in memory. 9 * 10 * Use NEW_CB for buffers with a varying size and it will also allocate 11 * the buffer. 12 * Use BEGIN_CB for arrays with a static size. 13 * 14 * Example: 15 * 16 * uint32_t cb[3]; 17 * CB_LOCALS; 18 * 19 * BEGIN_CB(cb, 3); 20 * OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2); 21 * OUT_CB(blend_color_red_alpha); 22 * OUT_CB(blend_color_green_blue); 23 * END_CB; 24 * 25 * And later: 26 * 27 * CS_LOCALS; 28 * WRITE_CS_TABLE(cb, 3); 29 * 30 * Or using a little slower variant: 31 * 32 * CS_LOCALS; 33 * BEGIN_CS(cb, 3); 34 * OUT_CS_TABLE(cb, 3); 35 * END_CS; 36 */ 37 38 #ifndef R300_CB_H 39 #define R300_CB_H 40 41 #include "r300_reg.h" 42 43 /* Yes, I know macros are ugly. However, they are much prettier than the code 44 * that they neatly hide away, and don't have the cost of function setup, so 45 * we're going to use them. */ 46 47 /** 48 * Command buffer setup. 49 */ 50 51 #if MESA_DEBUG 52 53 #define CB_LOCALS \ 54 int cs_count = 0; \ 55 uint32_t *cs_ptr = NULL; \ 56 (void) cs_count; (void) cs_ptr 57 58 #define BEGIN_CB(ptr, size) do { \ 59 assert(sizeof(*(ptr)) == sizeof(uint32_t)); \ 60 cs_count = (size); \ 61 cs_ptr = (ptr); \ 62 } while (0) 63 64 #define NEW_CB(ptr, size) \ 65 do { \ 66 assert(sizeof(*(ptr)) == sizeof(uint32_t)); \ 67 cs_count = (size); \ 68 cs_ptr = (ptr) = malloc((size) * sizeof(uint32_t)); \ 69 } while (0) 70 71 #define END_CB do { \ 72 if (cs_count != 0) \ 73 debug_printf("r300: Warning: cs_count off by %d at (%s, %s:%i)\n", \ 74 cs_count, __func__, __FILE__, __LINE__); \ 75 } while (0) 76 77 #define CB_USED_DW(x) cs_count -= x 78 79 #else 80 81 #define CB_LOCALS \ 82 uint32_t *cs_ptr = NULL; (void) cs_ptr 83 84 #define NEW_CB(ptr, size) \ 85 cs_ptr = (ptr) = malloc((size) * sizeof(uint32_t)) 86 87 #define BEGIN_CB(ptr, size) cs_ptr = (ptr) 88 #define END_CB 89 #define CB_USED_DW(x) 90 91 #endif 92 93 94 /** 95 * Storing pure DWORDs. 96 */ 97 98 #define OUT_CB(value) do { \ 99 *cs_ptr = (value); \ 100 cs_ptr++; \ 101 CB_USED_DW(1); \ 102 } while (0) 103 104 #define OUT_CB_TABLE(values, count) do { \ 105 memcpy(cs_ptr, values, count * sizeof(uint32_t)); \ 106 cs_ptr += count; \ 107 CB_USED_DW(count); \ 108 } while (0) 109 110 #define OUT_CB_32F(value) \ 111 OUT_CB(fui(value)); 112 113 #define OUT_CB_REG(register, value) do { \ 114 assert(register); \ 115 OUT_CB(CP_PACKET0(register, 0)); \ 116 OUT_CB(value); \ 117 } while (0) 118 119 /* Note: This expects count to be the number of registers, 120 * not the actual packet0 count! */ 121 #define OUT_CB_REG_SEQ(register, count) do { \ 122 assert(register); \ 123 OUT_CB(CP_PACKET0(register, (count) - 1)); \ 124 } while (0) 125 126 #define OUT_CB_ONE_REG(register, count) do { \ 127 assert(register); \ 128 OUT_CB(CP_PACKET0(register, (count) - 1) | RADEON_ONE_REG_WR); \ 129 } while (0) 130 131 #define OUT_CB_PKT3(op, count) \ 132 OUT_CB(CP_PACKET3(op, count)) 133 134 #endif /* R300_CB_H */ 135