HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Android 10
|
10.0.0_r6
下载
查看原文件
收藏
根目录
external
swiftshader
src
Device
Blitter.cpp
// Copyright 2016 The SwiftShader Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "Blitter.hpp" #include "Pipeline/ShaderCore.hpp" #include "Reactor/Reactor.hpp" #include "System/Memory.hpp" #include "Vulkan/VkDebug.hpp" namespace sw { Blitter::Blitter() { blitCache = new RoutineCache
(1024); } Blitter::~Blitter() { delete blitCache; } void Blitter::clear(void *pixel, VkFormat format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask) { if(fastClear(pixel, format, dest, dRect, rgbaMask)) { return; } sw::Surface *color = sw::Surface::create(1, 1, 1, format, pixel, sw::Surface::bytes(format), sw::Surface::bytes(format)); SliceRectF sRect(0.5f, 0.5f, 0.5f, 0.5f, 0); // Sample from the middle. blit(color, sRect, dest, dRect, {rgbaMask}); delete color; } bool Blitter::fastClear(void *pixel, VkFormat format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask) { if(format != VK_FORMAT_R32G32B32A32_SFLOAT) { return false; } float *color = (float*)pixel; float r = color[0]; float g = color[1]; float b = color[2]; float a = color[3]; uint32_t packed; switch(dest->getFormat()) { case VK_FORMAT_R5G6B5_UNORM_PACK16: if((rgbaMask & 0x7) != 0x7) return false; packed = ((uint16_t)(31 * b + 0.5f) << 0) | ((uint16_t)(63 * g + 0.5f) << 5) | ((uint16_t)(31 * r + 0.5f) << 11); break; case VK_FORMAT_B5G6R5_UNORM_PACK16: if((rgbaMask & 0x7) != 0x7) return false; packed = ((uint16_t)(31 * r + 0.5f) << 0) | ((uint16_t)(63 * g + 0.5f) << 5) | ((uint16_t)(31 * b + 0.5f) << 11); break; case VK_FORMAT_A8B8G8R8_UINT_PACK32: case VK_FORMAT_A8B8G8R8_UNORM_PACK32: case VK_FORMAT_R8G8B8A8_UNORM: if((rgbaMask & 0xF) != 0xF) return false; packed = ((uint32_t)(255 * a + 0.5f) << 24) | ((uint32_t)(255 * b + 0.5f) << 16) | ((uint32_t)(255 * g + 0.5f) << 8) | ((uint32_t)(255 * r + 0.5f) << 0); break; case VK_FORMAT_B8G8R8A8_UNORM: if((rgbaMask & 0xF) != 0xF) return false; packed = ((uint32_t)(255 * a + 0.5f) << 24) | ((uint32_t)(255 * r + 0.5f) << 16) | ((uint32_t)(255 * g + 0.5f) << 8) | ((uint32_t)(255 * b + 0.5f) << 0); break; case VK_FORMAT_B10G11R11_UFLOAT_PACK32: if((rgbaMask & 0x7) != 0x7) return false; packed = R11G11B10F(color); break; case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: if((rgbaMask & 0x7) != 0x7) return false; packed = RGB9E5(color); break; default: return false; } bool useDestInternal = !dest->isExternalDirty(); uint8_t *slice = (uint8_t*)dest->lock(dRect.x0, dRect.y0, dRect.slice, sw::LOCK_WRITEONLY, sw::PUBLIC, useDestInternal); for(int j = 0; j < dest->getSamples(); j++) { uint8_t *d = slice; switch(Surface::bytes(dest->getFormat())) { case 2: for(int i = dRect.y0; i < dRect.y1; i++) { sw::clear((uint16_t*)d, packed, dRect.x1 - dRect.x0); d += dest->getPitchB(useDestInternal); } break; case 4: for(int i = dRect.y0; i < dRect.y1; i++) { sw::clear((uint32_t*)d, packed, dRect.x1 - dRect.x0); d += dest->getPitchB(useDestInternal); } break; default: assert(false); } slice += dest->getSliceB(useDestInternal); } dest->unlock(useDestInternal); return true; } void Blitter::blit(Surface *source, const SliceRectF &sourceRect, Surface *dest, const SliceRect &destRect, const Blitter::Options& options) { if(dest->getInternalFormat() == VK_FORMAT_UNDEFINED) { return; } if(blitReactor(source, sourceRect, dest, destRect, options)) { return; } SliceRectF sRect = sourceRect; SliceRect dRect = destRect; bool flipX = destRect.x0 > destRect.x1; bool flipY = destRect.y0 > destRect.y1; if(flipX) { swap(dRect.x0, dRect.x1); swap(sRect.x0, sRect.x1); } if(flipY) { swap(dRect.y0, dRect.y1); swap(sRect.y0, sRect.y1); } source->lockInternal(0, 0, sRect.slice, sw::LOCK_READONLY, sw::PUBLIC); dest->lockInternal(0, 0, dRect.slice, sw::LOCK_WRITEONLY, sw::PUBLIC); float w = sRect.width() / dRect.width(); float h = sRect.height() / dRect.height(); float xStart = sRect.x0 + (0.5f - dRect.x0) * w; float yStart = sRect.y0 + (0.5f - dRect.y0) * h; for(int j = dRect.y0; j < dRect.y1; j++) { float y = yStart + j * h; for(int i = dRect.x0; i < dRect.x1; i++) { float x = xStart + i * w; // FIXME: Support RGBA mask dest->copyInternal(source, i, j, x, y, options.filter); } } source->unlockInternal(); dest->unlockInternal(); } void Blitter::blit3D(Surface *source, Surface *dest) { source->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC); dest->lockInternal(0, 0, 0, sw::LOCK_WRITEONLY, sw::PUBLIC); float w = static_cast
(source->getWidth()) / static_cast
(dest->getWidth()); float h = static_cast
(source->getHeight()) / static_cast
(dest->getHeight()); float d = static_cast
(source->getDepth()) / static_cast
(dest->getDepth()); for(int k = 0; k < dest->getDepth(); k++) { float z = (k + 0.5f) * d; for(int j = 0; j < dest->getHeight(); j++) { float y = (j + 0.5f) * h; for(int i = 0; i < dest->getWidth(); i++) { float x = (i + 0.5f) * w; dest->copyInternal(source, i, j, k, x, y, z, true); } } } source->unlockInternal(); dest->unlockInternal(); } bool Blitter::read(Float4 &c, Pointer
element, const State &state) { c = Float4(0.0f, 0.0f, 0.0f, 1.0f); switch(state.sourceFormat) { case VK_FORMAT_B4G4R4A4_UNORM_PACK16: c.w = Float(Int(*Pointer
(element)) & Int(0xF)); c.x = Float((Int(*Pointer
(element)) >> 4) & Int(0xF)); c.y = Float(Int(*Pointer
(element + 1)) & Int(0xF)); c.z = Float((Int(*Pointer
(element + 1)) >> 4) & Int(0xF)); break; case VK_FORMAT_R8_SINT: case VK_FORMAT_R8_SNORM: c.x = Float(Int(*Pointer
(element))); c.w = float(0x7F); break; case VK_FORMAT_R8_UNORM: case VK_FORMAT_R8_UINT: c.x = Float(Int(*Pointer
(element))); c.w = float(0xFF); break; case VK_FORMAT_R16_SINT: c.x = Float(Int(*Pointer
(element))); c.w = float(0x7FFF); break; case VK_FORMAT_R16_UINT: c.x = Float(Int(*Pointer
(element))); c.w = float(0xFFFF); break; case VK_FORMAT_R32_SINT: c.x = Float(*Pointer
(element)); c.w = float(0x7FFFFFFF); break; case VK_FORMAT_R32_UINT: c.x = Float(*Pointer
(element)); c.w = float(0xFFFFFFFF); break; case VK_FORMAT_B8G8R8A8_SRGB: case VK_FORMAT_B8G8R8A8_UNORM: c = Float4(*Pointer
(element)).zyxw; break; case VK_FORMAT_A8B8G8R8_SINT_PACK32: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_A8B8G8R8_SNORM_PACK32: case VK_FORMAT_R8G8B8A8_SNORM: c = Float4(*Pointer
(element)); break; case VK_FORMAT_A8B8G8R8_UINT_PACK32: case VK_FORMAT_A8B8G8R8_UNORM_PACK32: case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_A8B8G8R8_SRGB_PACK32: case VK_FORMAT_R8G8B8A8_SRGB: c = Float4(*Pointer
(element)); break; case VK_FORMAT_R16G16B16A16_SINT: c = Float4(*Pointer
(element)); break; case VK_FORMAT_R16G16B16A16_UNORM: case VK_FORMAT_R16G16B16A16_UINT: c = Float4(*Pointer
(element)); break; case VK_FORMAT_R32G32B32A32_SINT: c = Float4(*Pointer
(element)); break; case VK_FORMAT_R32G32B32A32_UINT: c = Float4(*Pointer
(element)); break; case VK_FORMAT_R8G8_SINT: case VK_FORMAT_R8G8_SNORM: c.x = Float(Int(*Pointer
(element + 0))); c.y = Float(Int(*Pointer
(element + 1))); c.w = float(0x7F); break; case VK_FORMAT_R8G8_UNORM: case VK_FORMAT_R8G8_UINT: c.x = Float(Int(*Pointer
(element + 0))); c.y = Float(Int(*Pointer
(element + 1))); c.w = float(0xFF); break; case VK_FORMAT_R16G16_SINT: c.x = Float(Int(*Pointer
(element + 0))); c.y = Float(Int(*Pointer
(element + 2))); c.w = float(0x7FFF); break; case VK_FORMAT_R16G16_UNORM: case VK_FORMAT_R16G16_UINT: c.x = Float(Int(*Pointer
(element + 0))); c.y = Float(Int(*Pointer
(element + 2))); c.w = float(0xFFFF); break; case VK_FORMAT_R32G32_SINT: c.x = Float(*Pointer
(element + 0)); c.y = Float(*Pointer
(element + 4)); c.w = float(0x7FFFFFFF); break; case VK_FORMAT_R32G32_UINT: c.x = Float(*Pointer
(element + 0)); c.y = Float(*Pointer
(element + 4)); c.w = float(0xFFFFFFFF); break; case VK_FORMAT_R32G32B32A32_SFLOAT: c = *Pointer
(element); break; case VK_FORMAT_R32G32_SFLOAT: c.x = *Pointer
(element + 0); c.y = *Pointer
(element + 4); break; case VK_FORMAT_R32_SFLOAT: c.x = *Pointer
(element); break; case VK_FORMAT_R16G16B16A16_SFLOAT: c.w = Float(*Pointer
(element + 6)); case VK_FORMAT_R16G16B16_SFLOAT: c.z = Float(*Pointer
(element + 4)); case VK_FORMAT_R16G16_SFLOAT: c.y = Float(*Pointer
(element + 2)); case VK_FORMAT_R16_SFLOAT: c.x = Float(*Pointer
(element)); break; case VK_FORMAT_B10G11R11_UFLOAT_PACK32: // 10 (or 11) bit float formats are unsigned formats with a 5 bit exponent and a 5 (or 6) bit mantissa. // Since the Half float format also has a 5 bit exponent, we can convert these formats to half by // copy/pasting the bits so the the exponent bits and top mantissa bits are aligned to the half format. // In this case, we have: // B B B B B B B B B B G G G G G G G G G G G R R R R R R R R R R R // 1st Short: |xxxxxxxxxx---------------------| // 2nd Short: |xxxx---------------------xxxxxx| // 3rd Short: |--------------------xxxxxxxxxxxx| // These memory reads overlap, but each of them contains an entire channel, so we can read this without // any int -> short conversion. c.x = Float(As
((*Pointer
(element + 0) & UShort(0x07FF)) << UShort(4))); c.y = Float(As
((*Pointer
(element + 1) & UShort(0x3FF8)) << UShort(1))); c.z = Float(As
((*Pointer
(element + 2) & UShort(0xFFC0)) >> UShort(1))); break; case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: // This type contains a common 5 bit exponent (E) and a 9 bit the mantissa for R, G and B. c.x = Float(*Pointer
(element) & UInt(0x000001FF)); // R's mantissa (bits 0-8) c.y = Float((*Pointer
(element) & UInt(0x0003FE00)) >> 9); // G's mantissa (bits 9-17) c.z = Float((*Pointer
(element) & UInt(0x07FC0000)) >> 18); // B's mantissa (bits 18-26) c *= Float4( // 2^E, using the exponent (bits 27-31) and treating it as an unsigned integer value Float(UInt(1) << ((*Pointer
(element) & UInt(0xF8000000)) >> 27)) * // Since the 9 bit mantissa values currently stored in RGB were converted straight // from int to float (in the [0, 1<<9] range instead of the [0, 1] range), they // are (1 << 9) times too high. // Also, the exponent has 5 bits and we compute the exponent bias of floating point // formats using "2^(k-1) - 1", so, in this case, the exponent bias is 2^(5-1)-1 = 15 // Exponent bias (15) + number of mantissa bits per component (9) = 24 Float(1.0f / (1 << 24))); c.w = 1.0f; break; case VK_FORMAT_R5G6B5_UNORM_PACK16: c.x = Float(Int((*Pointer
(element) & UShort(0xF800)) >> UShort(11))); c.y = Float(Int((*Pointer
(element) & UShort(0x07E0)) >> UShort(5))); c.z = Float(Int(*Pointer
(element) & UShort(0x001F))); break; case VK_FORMAT_A1R5G5B5_UNORM_PACK16: c.w = Float(Int((*Pointer
(element) & UShort(0x8000)) >> UShort(15))); c.x = Float(Int((*Pointer
(element) & UShort(0x7C00)) >> UShort(10))); c.y = Float(Int((*Pointer
(element) & UShort(0x03E0)) >> UShort(5))); c.z = Float(Int(*Pointer
(element) & UShort(0x001F))); break; case VK_FORMAT_A2B10G10R10_UNORM_PACK32: case VK_FORMAT_A2B10G10R10_UINT_PACK32: c.x = Float(Int((*Pointer
(element) & UInt(0x000003FF)))); c.y = Float(Int((*Pointer
(element) & UInt(0x000FFC00)) >> 10)); c.z = Float(Int((*Pointer
(element) & UInt(0x3FF00000)) >> 20)); c.w = Float(Int((*Pointer
(element) & UInt(0xC0000000)) >> 30)); break; case VK_FORMAT_D16_UNORM: c.x = Float(Int((*Pointer
(element)))); break; case VK_FORMAT_D24_UNORM_S8_UINT: case VK_FORMAT_X8_D24_UNORM_PACK32: c.x = Float(Int((*Pointer
(element) & UInt(0xFFFFFF00)) >> 8)); break; case VK_FORMAT_D32_SFLOAT: case VK_FORMAT_D32_SFLOAT_S8_UINT: c.x = *Pointer
(element); break; case VK_FORMAT_S8_UINT: c.x = Float(Int(*Pointer
(element))); break; default: return false; } return true; } bool Blitter::write(Float4 &c, Pointer
element, const State &state) { bool writeR = state.writeRed; bool writeG = state.writeGreen; bool writeB = state.writeBlue; bool writeA = state.writeAlpha; bool writeRGBA = writeR && writeG && writeB && writeA; switch(state.destFormat) { case VK_FORMAT_R4G4_UNORM_PACK8: if(writeR | writeG) { if(!writeR) { *Pointer
(element) = (Byte(RoundInt(Float(c.y))) & Byte(0xF)) | (*Pointer
(element) & Byte(0xF0)); } else if(!writeG) { *Pointer
(element) = (*Pointer
(element) & Byte(0xF)) | (Byte(RoundInt(Float(c.x))) << Byte(4)); } else { *Pointer
(element) = (Byte(RoundInt(Float(c.y))) & Byte(0xF)) | (Byte(RoundInt(Float(c.x))) << Byte(4)); } } break; case VK_FORMAT_R4G4B4A4_UNORM_PACK16: if(writeR || writeG || writeB || writeA) { *Pointer
(element) = (writeR ? ((UShort(RoundInt(Float(c.x))) & UShort(0xF)) << UShort(12)) : (*Pointer
(element) & UShort(0x000F))) | (writeG ? ((UShort(RoundInt(Float(c.y))) & UShort(0xF)) << UShort(8)) : (*Pointer
(element) & UShort(0x00F0))) | (writeB ? ((UShort(RoundInt(Float(c.z))) & UShort(0xF)) << UShort(4)) : (*Pointer
(element) & UShort(0x0F00))) | (writeA ? (UShort(RoundInt(Float(c.w))) & UShort(0xF)) : (*Pointer
(element) & UShort(0xF000))); } break; case VK_FORMAT_B4G4R4A4_UNORM_PACK16: if(writeRGBA) { *Pointer
(element) = UShort(RoundInt(Float(c.w)) & Int(0xF)) | UShort((RoundInt(Float(c.x)) & Int(0xF)) << 4) | UShort((RoundInt(Float(c.y)) & Int(0xF)) << 8) | UShort((RoundInt(Float(c.z)) & Int(0xF)) << 12); } else { unsigned short mask = (writeA ? 0x000F : 0x0000) | (writeR ? 0x00F0 : 0x0000) | (writeG ? 0x0F00 : 0x0000) | (writeB ? 0xF000 : 0x0000); unsigned short unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UShort(unmask)) | ((UShort(RoundInt(Float(c.w)) & Int(0xF)) | UShort((RoundInt(Float(c.x)) & Int(0xF)) << 4) | UShort((RoundInt(Float(c.y)) & Int(0xF)) << 8) | UShort((RoundInt(Float(c.z)) & Int(0xF)) << 12)) & UShort(mask)); } break; case VK_FORMAT_B8G8R8A8_SRGB: case VK_FORMAT_B8G8R8A8_UNORM: if(writeRGBA) { Short4 c0 = RoundShort4(c.zyxw); *Pointer
(element) = Byte4(PackUnsigned(c0, c0)); } else { if(writeB) { *Pointer
(element + 0) = Byte(RoundInt(Float(c.z))); } if(writeG) { *Pointer
(element + 1) = Byte(RoundInt(Float(c.y))); } if(writeR) { *Pointer
(element + 2) = Byte(RoundInt(Float(c.x))); } if(writeA) { *Pointer
(element + 3) = Byte(RoundInt(Float(c.w))); } } break; case VK_FORMAT_B8G8R8_SNORM: if(writeB) { *Pointer
(element + 0) = SByte(RoundInt(Float(c.z))); } if(writeG) { *Pointer
(element + 1) = SByte(RoundInt(Float(c.y))); } if(writeR) { *Pointer
(element + 2) = SByte(RoundInt(Float(c.x))); } break; case VK_FORMAT_B8G8R8_UNORM: case VK_FORMAT_B8G8R8_SRGB: if(writeB) { *Pointer
(element + 0) = Byte(RoundInt(Float(c.z))); } if(writeG) { *Pointer
(element + 1) = Byte(RoundInt(Float(c.y))); } if(writeR) { *Pointer
(element + 2) = Byte(RoundInt(Float(c.x))); } break; case VK_FORMAT_A8B8G8R8_UNORM_PACK32: case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_A8B8G8R8_SRGB_PACK32: case VK_FORMAT_R8G8B8A8_SRGB: case VK_FORMAT_A8B8G8R8_UINT_PACK32: case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R8G8B8A8_USCALED: case VK_FORMAT_A8B8G8R8_USCALED_PACK32: if(writeRGBA) { Short4 c0 = RoundShort4(c); *Pointer
(element) = Byte4(PackUnsigned(c0, c0)); } else { if(writeR) { *Pointer
(element + 0) = Byte(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 1) = Byte(RoundInt(Float(c.y))); } if(writeB) { *Pointer
(element + 2) = Byte(RoundInt(Float(c.z))); } if(writeA) { *Pointer
(element + 3) = Byte(RoundInt(Float(c.w))); } } break; case VK_FORMAT_R32G32B32A32_SFLOAT: if(writeRGBA) { *Pointer
(element) = c; } else { if(writeR) { *Pointer
(element) = c.x; } if(writeG) { *Pointer
(element + 4) = c.y; } if(writeB) { *Pointer
(element + 8) = c.z; } if(writeA) { *Pointer
(element + 12) = c.w; } } break; case VK_FORMAT_R32G32B32_SFLOAT: if(writeR) { *Pointer
(element) = c.x; } if(writeG) { *Pointer
(element + 4) = c.y; } if(writeB) { *Pointer
(element + 8) = c.z; } break; case VK_FORMAT_R32G32_SFLOAT: if(writeR && writeG) { *Pointer
(element) = Float2(c); } else { if(writeR) { *Pointer
(element) = c.x; } if(writeG) { *Pointer
(element + 4) = c.y; } } break; case VK_FORMAT_R32_SFLOAT: if(writeR) { *Pointer
(element) = c.x; } break; case VK_FORMAT_R16G16B16A16_SFLOAT: if(writeA) { *Pointer
(element + 6) = Half(c.w); } case VK_FORMAT_R16G16B16_SFLOAT: if(writeB) { *Pointer
(element + 4) = Half(c.z); } case VK_FORMAT_R16G16_SFLOAT: if(writeG) { *Pointer
(element + 2) = Half(c.y); } case VK_FORMAT_R16_SFLOAT: if(writeR) { *Pointer
(element) = Half(c.x); } break; case VK_FORMAT_B8G8R8A8_SNORM: if(writeB) { *Pointer
(element) = SByte(RoundInt(Float(c.z))); } if(writeG) { *Pointer
(element + 1) = SByte(RoundInt(Float(c.y))); } if(writeR) { *Pointer
(element + 2) = SByte(RoundInt(Float(c.x))); } if(writeA) { *Pointer
(element + 3) = SByte(RoundInt(Float(c.w))); } break; case VK_FORMAT_A8B8G8R8_SINT_PACK32: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_A8B8G8R8_SNORM_PACK32: case VK_FORMAT_R8G8B8A8_SNORM: case VK_FORMAT_R8G8B8A8_SSCALED: case VK_FORMAT_A8B8G8R8_SSCALED_PACK32: if(writeA) { *Pointer
(element + 3) = SByte(RoundInt(Float(c.w))); } case VK_FORMAT_R8G8B8_SINT: case VK_FORMAT_R8G8B8_SNORM: case VK_FORMAT_R8G8B8_SSCALED: case VK_FORMAT_R8G8B8_SRGB: if(writeB) { *Pointer
(element + 2) = SByte(RoundInt(Float(c.z))); } case VK_FORMAT_R8G8_SINT: case VK_FORMAT_R8G8_SNORM: case VK_FORMAT_R8G8_SSCALED: case VK_FORMAT_R8G8_SRGB: if(writeG) { *Pointer
(element + 1) = SByte(RoundInt(Float(c.y))); } case VK_FORMAT_R8_SINT: case VK_FORMAT_R8_SNORM: case VK_FORMAT_R8_SSCALED: case VK_FORMAT_R8_SRGB: if(writeR) { *Pointer
(element) = SByte(RoundInt(Float(c.x))); } break; case VK_FORMAT_R8G8B8_UINT: case VK_FORMAT_R8G8B8_UNORM: case VK_FORMAT_R8G8B8_USCALED: if(writeB) { *Pointer
(element + 2) = Byte(RoundInt(Float(c.z))); } case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8G8_UNORM: case VK_FORMAT_R8G8_USCALED: if(writeG) { *Pointer
(element + 1) = Byte(RoundInt(Float(c.y))); } case VK_FORMAT_R8_UINT: case VK_FORMAT_R8_UNORM: case VK_FORMAT_R8_USCALED: if(writeR) { *Pointer
(element) = Byte(RoundInt(Float(c.x))); } break; case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R16G16B16A16_SNORM: case VK_FORMAT_R16G16B16A16_SSCALED: if(writeRGBA) { *Pointer
(element) = Short4(RoundInt(c)); } else { if(writeR) { *Pointer
(element) = Short(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 2) = Short(RoundInt(Float(c.y))); } if(writeB) { *Pointer
(element + 4) = Short(RoundInt(Float(c.z))); } if(writeA) { *Pointer
(element + 6) = Short(RoundInt(Float(c.w))); } } break; case VK_FORMAT_R16G16B16_SINT: case VK_FORMAT_R16G16B16_SNORM: case VK_FORMAT_R16G16B16_SSCALED: if(writeR) { *Pointer
(element) = Short(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 2) = Short(RoundInt(Float(c.y))); } if(writeB) { *Pointer
(element + 4) = Short(RoundInt(Float(c.z))); } break; case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16G16_SNORM: case VK_FORMAT_R16G16_SSCALED: if(writeR && writeG) { *Pointer
(element) = Short2(Short4(RoundInt(c))); } else { if(writeR) { *Pointer
(element) = Short(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 2) = Short(RoundInt(Float(c.y))); } } break; case VK_FORMAT_R16_SINT: case VK_FORMAT_R16_SNORM: case VK_FORMAT_R16_SSCALED: if(writeR) { *Pointer
(element) = Short(RoundInt(Float(c.x))); } break; case VK_FORMAT_R16G16B16A16_UINT: case VK_FORMAT_R16G16B16A16_UNORM: case VK_FORMAT_R16G16B16A16_USCALED: if(writeRGBA) { *Pointer
(element) = UShort4(RoundInt(c)); } else { if(writeR) { *Pointer
(element) = UShort(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 2) = UShort(RoundInt(Float(c.y))); } if(writeB) { *Pointer
(element + 4) = UShort(RoundInt(Float(c.z))); } if(writeA) { *Pointer
(element + 6) = UShort(RoundInt(Float(c.w))); } } break; case VK_FORMAT_R16G16B16_UINT: case VK_FORMAT_R16G16B16_UNORM: case VK_FORMAT_R16G16B16_USCALED: if(writeR) { *Pointer
(element) = UShort(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 2) = UShort(RoundInt(Float(c.y))); } if(writeB) { *Pointer
(element + 4) = UShort(RoundInt(Float(c.z))); } break; case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16G16_UNORM: case VK_FORMAT_R16G16_USCALED: if(writeR && writeG) { *Pointer
(element) = UShort2(UShort4(RoundInt(c))); } else { if(writeR) { *Pointer
(element) = UShort(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 2) = UShort(RoundInt(Float(c.y))); } } break; case VK_FORMAT_R16_UINT: case VK_FORMAT_R16_UNORM: case VK_FORMAT_R16_USCALED: if(writeR) { *Pointer
(element) = UShort(RoundInt(Float(c.x))); } break; case VK_FORMAT_R32G32B32A32_SINT: if(writeRGBA) { *Pointer
(element) = RoundInt(c); } else { if(writeR) { *Pointer
(element) = RoundInt(Float(c.x)); } if(writeG) { *Pointer
(element + 4) = RoundInt(Float(c.y)); } if(writeB) { *Pointer
(element + 8) = RoundInt(Float(c.z)); } if(writeA) { *Pointer
(element + 12) = RoundInt(Float(c.w)); } } break; case VK_FORMAT_R32G32B32_SINT: if(writeB) { *Pointer
(element + 8) = RoundInt(Float(c.z)); } case VK_FORMAT_R32G32_SINT: if(writeG) { *Pointer
(element + 4) = RoundInt(Float(c.y)); } case VK_FORMAT_R32_SINT: if(writeR) { *Pointer
(element) = RoundInt(Float(c.x)); } break; case VK_FORMAT_R32G32B32A32_UINT: if(writeRGBA) { *Pointer
(element) = UInt4(RoundInt(c)); } else { if(writeR) { *Pointer
(element) = As
(RoundInt(Float(c.x))); } if(writeG) { *Pointer
(element + 4) = As
(RoundInt(Float(c.y))); } if(writeB) { *Pointer
(element + 8) = As
(RoundInt(Float(c.z))); } if(writeA) { *Pointer
(element + 12) = As
(RoundInt(Float(c.w))); } } break; case VK_FORMAT_R32G32B32_UINT: if(writeB) { *Pointer
(element + 8) = As
(RoundInt(Float(c.z))); } case VK_FORMAT_R32G32_UINT: if(writeG) { *Pointer
(element + 4) = As
(RoundInt(Float(c.y))); } case VK_FORMAT_R32_UINT: if(writeR) { *Pointer
(element) = As
(RoundInt(Float(c.x))); } break; case VK_FORMAT_R5G6B5_UNORM_PACK16: if(writeR && writeG && writeB) { *Pointer
(element) = UShort(RoundInt(Float(c.z)) | (RoundInt(Float(c.y)) << Int(5)) | (RoundInt(Float(c.x)) << Int(11))); } else { unsigned short mask = (writeB ? 0x001F : 0x0000) | (writeG ? 0x07E0 : 0x0000) | (writeR ? 0xF800 : 0x0000); unsigned short unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UShort(unmask)) | (UShort(RoundInt(Float(c.z)) | (RoundInt(Float(c.y)) << Int(5)) | (RoundInt(Float(c.x)) << Int(11))) & UShort(mask)); } break; case VK_FORMAT_R5G5B5A1_UNORM_PACK16: if(writeRGBA) { *Pointer
(element) = UShort(RoundInt(Float(c.w)) | (RoundInt(Float(c.z)) << Int(1)) | (RoundInt(Float(c.y)) << Int(6)) | (RoundInt(Float(c.x)) << Int(11))); } else { unsigned short mask = (writeA ? 0x8000 : 0x0000) | (writeR ? 0x7C00 : 0x0000) | (writeG ? 0x03E0 : 0x0000) | (writeB ? 0x001F : 0x0000); unsigned short unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UShort(unmask)) | (UShort(RoundInt(Float(c.w)) | (RoundInt(Float(c.z)) << Int(1)) | (RoundInt(Float(c.y)) << Int(6)) | (RoundInt(Float(c.x)) << Int(11))) & UShort(mask)); } break; case VK_FORMAT_B5G5R5A1_UNORM_PACK16: if(writeRGBA) { *Pointer
(element) = UShort(RoundInt(Float(c.w)) | (RoundInt(Float(c.x)) << Int(1)) | (RoundInt(Float(c.y)) << Int(6)) | (RoundInt(Float(c.z)) << Int(11))); } else { unsigned short mask = (writeA ? 0x8000 : 0x0000) | (writeR ? 0x7C00 : 0x0000) | (writeG ? 0x03E0 : 0x0000) | (writeB ? 0x001F : 0x0000); unsigned short unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UShort(unmask)) | (UShort(RoundInt(Float(c.w)) | (RoundInt(Float(c.x)) << Int(1)) | (RoundInt(Float(c.y)) << Int(6)) | (RoundInt(Float(c.z)) << Int(11))) & UShort(mask)); } break; case VK_FORMAT_A1R5G5B5_UNORM_PACK16: if(writeRGBA) { *Pointer
(element) = UShort(RoundInt(Float(c.z)) | (RoundInt(Float(c.y)) << Int(5)) | (RoundInt(Float(c.x)) << Int(10)) | (RoundInt(Float(c.w)) << Int(15))); } else { unsigned short mask = (writeA ? 0x8000 : 0x0000) | (writeR ? 0x7C00 : 0x0000) | (writeG ? 0x03E0 : 0x0000) | (writeB ? 0x001F : 0x0000); unsigned short unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UShort(unmask)) | (UShort(RoundInt(Float(c.z)) | (RoundInt(Float(c.y)) << Int(5)) | (RoundInt(Float(c.x)) << Int(10)) | (RoundInt(Float(c.w)) << Int(15))) & UShort(mask)); } break; case VK_FORMAT_A2B10G10R10_UNORM_PACK32: case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_A2B10G10R10_SNORM_PACK32: if(writeRGBA) { *Pointer
(element) = UInt(RoundInt(Float(c.x)) | (RoundInt(Float(c.y)) << 10) | (RoundInt(Float(c.z)) << 20) | (RoundInt(Float(c.w)) << 30)); } else { unsigned int mask = (writeA ? 0xC0000000 : 0x0000) | (writeB ? 0x3FF00000 : 0x0000) | (writeG ? 0x000FFC00 : 0x0000) | (writeR ? 0x000003FF : 0x0000); unsigned int unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UInt(unmask)) | (UInt(RoundInt(Float(c.x)) | (RoundInt(Float(c.y)) << 10) | (RoundInt(Float(c.z)) << 20) | (RoundInt(Float(c.w)) << 30)) & UInt(mask)); } break; case VK_FORMAT_A2R10G10B10_UNORM_PACK32: case VK_FORMAT_A2R10G10B10_UINT_PACK32: case VK_FORMAT_A2R10G10B10_SNORM_PACK32: if(writeRGBA) { *Pointer
(element) = UInt(RoundInt(Float(c.z)) | (RoundInt(Float(c.y)) << 10) | (RoundInt(Float(c.x)) << 20) | (RoundInt(Float(c.w)) << 30)); } else { unsigned int mask = (writeA ? 0xC0000000 : 0x0000) | (writeR ? 0x3FF00000 : 0x0000) | (writeG ? 0x000FFC00 : 0x0000) | (writeB ? 0x000003FF : 0x0000); unsigned int unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UInt(unmask)) | (UInt(RoundInt(Float(c.z)) | (RoundInt(Float(c.y)) << 10) | (RoundInt(Float(c.x)) << 20) | (RoundInt(Float(c.w)) << 30)) & UInt(mask)); } break; case VK_FORMAT_D16_UNORM: *Pointer
(element) = UShort(RoundInt(Float(c.x))); break; case VK_FORMAT_D24_UNORM_S8_UINT: case VK_FORMAT_X8_D24_UNORM_PACK32: *Pointer
(element) = UInt(RoundInt(Float(c.x)) << 8); break; case VK_FORMAT_D32_SFLOAT: case VK_FORMAT_D32_SFLOAT_S8_UINT: *Pointer
(element) = c.x; break; case VK_FORMAT_S8_UINT: *Pointer
(element) = Byte(RoundInt(Float(c.x))); break; default: return false; } return true; } bool Blitter::read(Int4 &c, Pointer
element, const State &state) { c = Int4(0, 0, 0, 1); switch(state.sourceFormat) { case VK_FORMAT_A8B8G8R8_SINT_PACK32: case VK_FORMAT_R8G8B8A8_SINT: c = Insert(c, Int(*Pointer
(element + 3)), 3); c = Insert(c, Int(*Pointer
(element + 2)), 2); case VK_FORMAT_R8G8_SINT: c = Insert(c, Int(*Pointer
(element + 1)), 1); case VK_FORMAT_R8_SINT: c = Insert(c, Int(*Pointer
(element)), 0); break; case VK_FORMAT_A2B10G10R10_UINT_PACK32: c = Insert(c, Int((*Pointer
(element) & UInt(0x000003FF))), 0); c = Insert(c, Int((*Pointer
(element) & UInt(0x000FFC00)) >> 10), 1); c = Insert(c, Int((*Pointer
(element) & UInt(0x3FF00000)) >> 20), 2); c = Insert(c, Int((*Pointer
(element) & UInt(0xC0000000)) >> 30), 3); break; case VK_FORMAT_A8B8G8R8_UINT_PACK32: case VK_FORMAT_R8G8B8A8_UINT: c = Insert(c, Int(*Pointer
(element + 3)), 3); c = Insert(c, Int(*Pointer
(element + 2)), 2); case VK_FORMAT_R8G8_UINT: c = Insert(c, Int(*Pointer
(element + 1)), 1); case VK_FORMAT_R8_UINT: c = Insert(c, Int(*Pointer
(element)), 0); break; case VK_FORMAT_R16G16B16A16_SINT: c = Insert(c, Int(*Pointer
(element + 6)), 3); c = Insert(c, Int(*Pointer
(element + 4)), 2); case VK_FORMAT_R16G16_SINT: c = Insert(c, Int(*Pointer
(element + 2)), 1); case VK_FORMAT_R16_SINT: c = Insert(c, Int(*Pointer
(element)), 0); break; case VK_FORMAT_R16G16B16A16_UINT: c = Insert(c, Int(*Pointer
(element + 6)), 3); c = Insert(c, Int(*Pointer
(element + 4)), 2); case VK_FORMAT_R16G16_UINT: c = Insert(c, Int(*Pointer
(element + 2)), 1); case VK_FORMAT_R16_UINT: c = Insert(c, Int(*Pointer
(element)), 0); break; case VK_FORMAT_R32G32B32A32_SINT: case VK_FORMAT_R32G32B32A32_UINT: c = *Pointer
(element); break; case VK_FORMAT_R32G32_SINT: case VK_FORMAT_R32G32_UINT: c = Insert(c, *Pointer
(element + 4), 1); case VK_FORMAT_R32_SINT: case VK_FORMAT_R32_UINT: c = Insert(c, *Pointer
(element), 0); break; default: return false; } return true; } bool Blitter::write(Int4 &c, Pointer
element, const State &state) { bool writeR = state.writeRed; bool writeG = state.writeGreen; bool writeB = state.writeBlue; bool writeA = state.writeAlpha; bool writeRGBA = writeR && writeG && writeB && writeA; switch(state.destFormat) { case VK_FORMAT_A2B10G10R10_UINT_PACK32: c = Min(As
(c), UInt4(0x03FF, 0x03FF, 0x03FF, 0x0003)); break; case VK_FORMAT_A8B8G8R8_UINT_PACK32: case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R8G8B8_UINT: case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8_UINT: case VK_FORMAT_R8G8B8A8_USCALED: case VK_FORMAT_R8G8B8_USCALED: case VK_FORMAT_R8G8_USCALED: case VK_FORMAT_R8_USCALED: c = Min(As
(c), UInt4(0xFF)); break; case VK_FORMAT_R16G16B16A16_UINT: case VK_FORMAT_R16G16B16_UINT: case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16_UINT: case VK_FORMAT_R16G16B16A16_USCALED: case VK_FORMAT_R16G16B16_USCALED: case VK_FORMAT_R16G16_USCALED: case VK_FORMAT_R16_USCALED: c = Min(As
(c), UInt4(0xFFFF)); break; case VK_FORMAT_A8B8G8R8_SINT_PACK32: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_R8G8_SINT: case VK_FORMAT_R8_SINT: case VK_FORMAT_R8G8B8A8_SSCALED: case VK_FORMAT_R8G8B8_SSCALED: case VK_FORMAT_R8G8_SSCALED: case VK_FORMAT_R8_SSCALED: c = Min(Max(c, Int4(-0x80)), Int4(0x7F)); break; case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R16G16B16_SINT: case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16_SINT: case VK_FORMAT_R16G16B16A16_SSCALED: case VK_FORMAT_R16G16B16_SSCALED: case VK_FORMAT_R16G16_SSCALED: case VK_FORMAT_R16_SSCALED: c = Min(Max(c, Int4(-0x8000)), Int4(0x7FFF)); break; default: break; } switch(state.destFormat) { case VK_FORMAT_B8G8R8A8_SINT: case VK_FORMAT_B8G8R8A8_SSCALED: if(writeA) { *Pointer
(element + 3) = SByte(Extract(c, 3)); } case VK_FORMAT_B8G8R8_SINT: case VK_FORMAT_B8G8R8_SRGB: case VK_FORMAT_B8G8R8_SSCALED: if(writeB) { *Pointer
(element) = SByte(Extract(c, 2)); } if(writeG) { *Pointer
(element + 1) = SByte(Extract(c, 1)); } if(writeR) { *Pointer
(element + 2) = SByte(Extract(c, 0)); } break; case VK_FORMAT_A8B8G8R8_SINT_PACK32: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_R8G8B8A8_SSCALED: case VK_FORMAT_A8B8G8R8_SSCALED_PACK32: if(writeA) { *Pointer
(element + 3) = SByte(Extract(c, 3)); } case VK_FORMAT_R8G8B8_SINT: case VK_FORMAT_R8G8B8_SSCALED: if(writeB) { *Pointer
(element + 2) = SByte(Extract(c, 2)); } case VK_FORMAT_R8G8_SINT: case VK_FORMAT_R8G8_SSCALED: if(writeG) { *Pointer
(element + 1) = SByte(Extract(c, 1)); } case VK_FORMAT_R8_SINT: case VK_FORMAT_R8_SSCALED: if(writeR) { *Pointer
(element) = SByte(Extract(c, 0)); } break; case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_A2B10G10R10_SINT_PACK32: case VK_FORMAT_A2B10G10R10_USCALED_PACK32: case VK_FORMAT_A2B10G10R10_SSCALED_PACK32: if(writeRGBA) { *Pointer
(element) = UInt((Extract(c, 0)) | (Extract(c, 1) << 10) | (Extract(c, 2) << 20) | (Extract(c, 3) << 30)); } else { unsigned int mask = (writeA ? 0xC0000000 : 0x0000) | (writeB ? 0x3FF00000 : 0x0000) | (writeG ? 0x000FFC00 : 0x0000) | (writeR ? 0x000003FF : 0x0000); unsigned int unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UInt(unmask)) | (UInt(Extract(c, 0) | (Extract(c, 1) << 10) | (Extract(c, 2) << 20) | (Extract(c, 3) << 30)) & UInt(mask)); } break; case VK_FORMAT_A2R10G10B10_UINT_PACK32: case VK_FORMAT_A2R10G10B10_SINT_PACK32: case VK_FORMAT_A2R10G10B10_USCALED_PACK32: case VK_FORMAT_A2R10G10B10_SSCALED_PACK32: if(writeRGBA) { *Pointer
(element) = UInt((Extract(c, 2)) | (Extract(c, 1) << 10) | (Extract(c, 0) << 20) | (Extract(c, 3) << 30)); } else { unsigned int mask = (writeA ? 0xC0000000 : 0x0000) | (writeR ? 0x3FF00000 : 0x0000) | (writeG ? 0x000FFC00 : 0x0000) | (writeB ? 0x000003FF : 0x0000); unsigned int unmask = ~mask; *Pointer
(element) = (*Pointer
(element) & UInt(unmask)) | (UInt(Extract(c, 2) | (Extract(c, 1) << 10) | (Extract(c, 0) << 20) | (Extract(c, 3) << 30)) & UInt(mask)); } break; case VK_FORMAT_B8G8R8A8_UINT: case VK_FORMAT_B8G8R8A8_USCALED: if(writeA) { *Pointer
(element + 3) = Byte(Extract(c, 3)); } case VK_FORMAT_B8G8R8_UINT: case VK_FORMAT_B8G8R8_USCALED: if(writeB) { *Pointer
(element) = Byte(Extract(c, 2)); } if(writeG) { *Pointer
(element + 1) = Byte(Extract(c, 1)); } if(writeR) { *Pointer
(element + 2) = Byte(Extract(c, 0)); } break; case VK_FORMAT_A8B8G8R8_UINT_PACK32: case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R8G8B8A8_USCALED: case VK_FORMAT_A8B8G8R8_USCALED_PACK32: if(writeA) { *Pointer
(element + 3) = Byte(Extract(c, 3)); } case VK_FORMAT_R8G8B8_UINT: case VK_FORMAT_R8G8B8_USCALED: if(writeB) { *Pointer
(element + 2) = Byte(Extract(c, 2)); } case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8G8_USCALED: if(writeG) { *Pointer
(element + 1) = Byte(Extract(c, 1)); } case VK_FORMAT_R8_UINT: case VK_FORMAT_R8_USCALED: if(writeR) { *Pointer
(element) = Byte(Extract(c, 0)); } break; case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R16G16B16A16_SSCALED: if(writeA) { *Pointer
(element + 6) = Short(Extract(c, 3)); } case VK_FORMAT_R16G16B16_SINT: case VK_FORMAT_R16G16B16_SSCALED: if(writeB) { *Pointer
(element + 4) = Short(Extract(c, 2)); } case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16G16_SSCALED: if(writeG) { *Pointer
(element + 2) = Short(Extract(c, 1)); } case VK_FORMAT_R16_SINT: case VK_FORMAT_R16_SSCALED: if(writeR) { *Pointer
(element) = Short(Extract(c, 0)); } break; case VK_FORMAT_R16G16B16A16_UINT: case VK_FORMAT_R16G16B16A16_USCALED: if(writeA) { *Pointer
(element + 6) = UShort(Extract(c, 3)); } case VK_FORMAT_R16G16B16_UINT: case VK_FORMAT_R16G16B16_USCALED: if(writeB) { *Pointer
(element + 4) = UShort(Extract(c, 2)); } case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16G16_USCALED: if(writeG) { *Pointer
(element + 2) = UShort(Extract(c, 1)); } case VK_FORMAT_R16_UINT: case VK_FORMAT_R16_USCALED: if(writeR) { *Pointer