~emersion/go-kdf

3a2857193ff286ea90e90443c8ccab8654361e4b — Simon Ser 1 year, 1 month ago 86b01e6
vulkan: add single-plane unpacked YUV formats
1 files changed, 51 insertions(+), 0 deletions(-)

M vulkan/vulkan.go
M vulkan/vulkan.go => vulkan/vulkan.go +51 -0
@@ 129,6 129,30 @@ func genUnpacked(bytesPerChannel int, channels []kdf.Channel, nf numericFormat, 
	})
}

func genYUVAUnpacked(bytesPerChannel int, channels []kdf.Channel, nf numericFormat, endianness kdf.Endianness) *kdf.Block {
	var sampleInfo []kdf.SampleInfo
	for _, sample := range internal.UnpackedSamples(channels, bytesPerChannel, endianness) {
		sampleInfo = append(sampleInfo, *genSample(sample.Channel, nf, sample.BitLength, sample.BitOffset))
	}

	numY := 0
	for i := len(sampleInfo) - 1; i >= 0; i-- {
		sample := &sampleInfo[i]
		if sample.Channel != kdf.ChannelYUVSDA_Y {
			continue
		}
		sample.SamplePosition[0] = uint8(numY * 0x80) // numY * 1.0
		numY++
	}

	return kdf.NewBlockBasic(&kdf.BlockBasic{
		ColorModel:          kdf.ColorModelYUVSDA,
		TexelBlockDimension: [4]int{2, 1, 1, 1},
		BytesPlane:          []uint8{uint8(bytesPerChannel * len(channels))},
		SampleInfo:          sampleInfo,
	})
}

func genPacked(channels []kdf.Channel, bitsPerChannel []int, nf numericFormat, endianness kdf.Endianness) *kdf.Block {
	totalBits := 0
	for _, n := range bitsPerChannel {


@@ 170,6 194,15 @@ var channelMap = map[string]kdf.Channel{
	"A": kdf.ChannelRGBSDA_A,
}

var ycbcrChannelMap = map[kdf.Channel]kdf.Channel{
	kdf.ChannelRGBSDA_R: kdf.ChannelYUVSDA_V,
	kdf.ChannelRGBSDA_G: kdf.ChannelYUVSDA_Y,
	kdf.ChannelRGBSDA_B: kdf.ChannelYUVSDA_U,
	kdf.ChannelRGBSDA_S: kdf.ChannelYUVSDA_S,
	kdf.ChannelRGBSDA_D: kdf.ChannelYUVSDA_D,
	kdf.ChannelRGBSDA_A: kdf.ChannelYUVSDA_A,
}

var unpackedBaseFormats = []string{
	"R8",
	"R8G8",


@@ 211,6 244,13 @@ var packedBaseFormats = []string{
	// TODO: more formats
}

var unpackedYUVAFormats = []Format{
	FormatG8B8G8R8_422_UNORM,
	FormatB8G8R8G8_422_UNORM,
	FormatG16B16G16R16_422_UNORM,
	FormatB16G16R16G16_422_UNORM,
}

func parseFormatBase(s string) (channels []kdf.Channel, bitsPerChannel []int) {
	for s != "" {
		ch, ok := channelMap[s[0:1]]


@@ 302,6 342,17 @@ func genTable(endianness kdf.Endianness) map[Format]*kdf.Block {
		}
	}

	for _, format := range unpackedYUVAFormats {
		base := strings.TrimRight(format.String(), "_422_UNORM")
		bytesPerChannel, channels := parseUnpackedBase(base)

		for i, ch := range channels {
			channels[i] = ycbcrChannelMap[ch]
		}

		blockTable[format] = genYUVAUnpacked(bytesPerChannel, channels, numericFormatUnorm, endianness)
	}

	return blockTable
}