| 126 | |
| 127 | @dataclass(frozen=True, eq=False) |
| 128 | class ImageDType(PtrDType): |
| 129 | shape: tuple[int, ...] = () # shape of the Image |
| 130 | def ptr(self, size=-1, addrspace=AddrSpace.GLOBAL) -> PtrDType: |
| 131 | assert addrspace == AddrSpace.GLOBAL, "images can't be local" |
| 132 | return self |
| 133 | def __repr__(self): return f"dtypes.{self.name}({self.shape})" + (f'.vec({self.v})' if self.v != 1 else '') |
| 134 | |
| 135 | # for 1d images on macos, we need to round pitch up to 256 pixels to make CL happy |
| 136 | @property |
| 137 | def pitch(self): return (round_up(self.shape[1], 256) if OSX else self.shape[1]) * 4 * self.itemsize |
| 138 | |
| 139 | # get list of (height, width) that do not require pitch padding |
| 140 | @staticmethod |
| 141 | def valid_dims(ptr:PtrDType, arch:str) -> list[tuple[int,int]]: |
| 142 | if (ALIGN:=next((int(p.split('=')[1]) for p in arch.split(',') if p.startswith("IMAGE_PITCH_ALIGNMENT=")), 0)) == 0: return [] |
| 143 | MAXW, pxls = 16384, ptr.size // 4 |
| 144 | if ptr.base not in (dtypes.half, dtypes.float) or ptr.size > 4*MAXW*MAXW: return [] |
| 145 | # height=1 images just need to abide by alignment requirements in bytes, not pixels! |
| 146 | if ptr.size % (ALIGN * 4) != 0: return [] if ptr.nbytes() % (64 if OSX else ALIGN) != 0 or pxls > MAXW else [(1, pxls)] |
| 147 | return [(pxls//ALIGN//k, ALIGN*k) for k in range(ceildiv(pxls//ALIGN, MAXW), min(pxls//ALIGN, MAXW//ALIGN)+1) if (pxls//ALIGN)%k == 0] |
| 148 | |
| 149 | class dtypes: |
| 150 | @staticmethod |