我一直在尝试使用texture2d_array来应用金属中的实时滤镜。但是我没有得到正确的结果。
我像这样创建纹理数组,
代码:Class MetalTextureArray
。
class MetalTextureArray {
private(set) var arrayTexture: MTLTexture
private var width: Int
private var height: Int
init(_ width: Int, _ height: Int, _ arrayLength: Int, _ device: MTLDevice) {
self.width = width
self.height = height
let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.textureType = .type2DArray
textureDescriptor.pixelFormat = .bgra8Unorm
textureDescriptor.width = width
textureDescriptor.height = height
textureDescriptor.arrayLength = arrayLength
arrayTexture = device.makeTexture(descriptor: textureDescriptor)
}
func append(_ texture: MTLTexture) -> Bool {
if let bytes = texture.buffer?.contents() {
let region = MTLRegion(origin: MTLOrigin(x: 0, y: 0, z: 0), size: MTLSize(width: width, height: height, depth: 1))
arrayTexture.replace(region: region, mipmapLevel: 0, withBytes: bytes, bytesPerRow: texture.bufferBytesPerRow)
return true
}
return false
}
}
我将这个纹理编码到这样的renderEncoder中,
代码:
let textureArray = MetalTextureArray.init(firstTexture!.width, firstTexture!.height, colorTextures.count, device)
_ = textureArray.append(colorTextures[0].texture)
_ = textureArray.append(colorTextures[1].texture)
_ = textureArray.append(colorTextures[2].texture)
_ = textureArray.append(colorTextures[3].texture)
_ = textureArray.append(colorTextures[4].texture)
renderEncoder.setFragmentTexture(textureArray.arrayTexture, at: 1)
最后我正在片段着色器中访问texture2d_array,就像这样,
代码:
struct RasterizerData {
float4 clipSpacePosition [[position]];
float2 textureCoordinate;
};
multipleShader(RasterizerData in [[stage_in]],
texture2d<half> colorTexture [[ texture(0) ]],
texture2d_array<half> texture2D [[ texture(1) ]])
{
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear,
s_address::repeat,
t_address::repeat,
r_address::repeat);
// Sample the texture and return the color to colorSample
half4 colorSample = colorTexture.sample (textureSampler, in.textureCoordinate);
float4 outputColor;
half red = texture2D.sample(textureSampler, in.textureCoordinate, 2).r;
half green = texture2D.sample(textureSampler, in.textureCoordinate, 2).g;
half blue = texture2D.sample(textureSampler, in.textureCoordinate, 2).b;
outputColor = float4(colorSample.r * red, colorSample.g * green, colorSample.b * blue, colorSample.a);
// We return the color of the texture
return outputColor;
}
我附加到纹理数组的纹理是从acv曲线文件中提取的纹理,其大小为256 * 1.
在这段代码half red = texture2D.sample(textureSampler, in.textureCoordinate, 2).r;
中,我将最后一个参数设为2,因为我认为它是要访问的纹理的索引。但我不知道这意味着什么。
但是在完成所有这些之后我才得到黑屏。即使我有其他片段着色器,所有这些都工作正常。但对于这个片段着色器,我会变黑屏幕。我想对于这段代码half blue = texture2D.sample(textureSampler, in.textureCoordinate, 2).b
我得到0
所有红色,绿色和蓝色值。
修改1:
正如所建议的那样,我使用blitcommandEncoder来复制纹理但仍然没有结果。
我的代码就在这里,
我的MetalTextureArray类已经进行了修改。
方法追加就是这样。
func append(_ texture: MTLTexture) -> Bool {
self.blitCommandEncoder.copy(from: texture, sourceSlice: 0, sourceLevel: 0, sourceOrigin: MTLOrigin(x: 0, y: 0, z: 0), sourceSize: MTLSize(width: texture.width, height: texture.height, depth: 1), to: self.arrayTexture, destinationSlice: count, destinationLevel: 0, destinationOrigin: MTLOrigin(x: 0, y: 0, z: 0))
count += 1
return true
}
我正在添加这样的纹理
let textureArray = MetalTextureArray.init(256, 1, colorTextures.count, device, blitCommandEncoder: blitcommandEncoder)
for (index, filter) in colorTextures!.enumerated() {
_ = textureArray.append(colorTextures[index].texture)
}
renderEncoder.setFragmentTexture(textureArray.arrayTexture, at: 1)
我的着色器代码就像这样
multipleShader(RasterizerData in [[stage_in]],
texture2d<half> colorTexture [[ texture(0) ]],
texture2d_array<float> textureArray [[texture(1)]],
const device struct SliceDataSource &sliceData [[ buffer(2) ]])
{
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
// Sample the texture and return the color to colorSample
half4 colorSample = colorTexture.sample (textureSampler, in.textureCoordinate);
float4 outputColor = float4(0,0,0,0);
int slice = 1;
float red = textureArray.sample(textureSampler, in.textureCoordinate, slice).r;
float blue = textureArray.sample(textureSampler, in.textureCoordinate, slice).b;
float green = textureArray.sample(textureSampler, in.textureCoordinate, slice).g;
outputColor = float4(colorSample.r * red, colorSample.g * green, colorSample.b * blue, colorSample.a);
// We return the color of the texture
return outputColor;
}
我仍然得到黑屏。
在方法textureArray.sample(textureSampler, in.textureCoordinate, slice);
中,第三个参数是什么。我虽然它作为一个索引,我给了一些随机索引来获取随机纹理。这是对的吗?
编辑2:
我终于能够实现这个建议了,我在实现另一个编码器之前使用endEncoding
方法获得了结果,并且我得到了带有ACV
负过滤器的以下屏幕。
有人可以建议我吗。
感谢。
答案 0 :(得分:2)
纹理数组和数组纹理之间存在差异。听起来像你只是想要一组纹理。在这种情况下,您不应该使用texture2d_array
;你应该使用array<texture2d<half>, 5> texture_array [[texture(1)]]
。
在应用程序代码中,您可以使用对setFragmentTexture()
的多次调用来为顺序索引分配纹理,也可以使用setFragmentTextures()
一次将一组纹理分配给一系列索引。
在着色器代码中,您将使用数组下标语法来引用数组中的各个纹理(例如texture_array[2]
)。
如果您确实想要使用数组纹理,则可能需要更改append()
方法。首先,如果texture
参数未使用makeTexture(descriptor:offset:bytesPerRow:)
MTLBuffer
方法创建,则texture.buffer
始终为nil
。也就是说,如果纹理最初是从缓冲区创建的,那么纹理只有关联的缓冲区。要从纹理复制到纹理,您应该使用blit命令编码器及其copy(from:sourceSlice:sourceLevel:sourceOrigin:sourceSize:to:destinationSlice:destinationLevel:destinationOrigin:)
方法。
其次,如果要替换数组纹理的特定切片(数组元素)的纹理数据,则需要将该切片索引作为参数传递给replace()
方法。为此,您需要使用replace(region:mipmapLevel:slice:withBytes:bytesPerRow:bytesPerImage:)
方法,而不是目前正在使用的replace(region:mipmapLevel:withBytes:bytesPerRow:)
方法。您当前的代码只是反复替换第一个切片(假设源纹理确实与缓冲区相关联。)