- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个以编程方式生成的图像,我想将此图像作为纹理发送到计算着色器。我生成此图像的方式是将每个 RGBA 分量计算为 UInt8
值,并将它们组合成一个 UInt32
并将其存储在图像的缓冲区中。我使用以下代码来执行此操作:
guard let cgContext = CGContext(data: nil,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: RGBA32.bitmapInfo) else {
print("Unable to create CGContext")
return
}
guard let buffer = cgContext.data else {
print("Unable to create textures")
return
}
let pixelBuffer = buffer.bindMemory(to: RGBA32.self, capacity: width * height)
let heightFloat = Float(height)
let widthFloat = Float(width)
for i in 0 ..< height {
let latitude = Float(i + 1) / heightFloat
for j in 0 ..< width {
let longitude = Float(j + 1) / widthFloat
let x = UInt8(((sin(longitude * Float.pi * 2) * cos(latitude * Float.pi) + 1) / 2) * 255)
let y = UInt8(((sin(longitude * Float.pi * 2) * sin(latitude * Float.pi) + 1) / 2) * 255)
let z = UInt8(((cos(latitude * Float.pi) + 1) / 2) * 255)
let offset = width * i + j
pixelBuffer[offset] = RGBA32(red: x, green: y, blue: z, alpha: 255)
}
}
let coordinateConversionImage = cgContext.makeImage()
哪里RGBA32
是一个小结构,它进行移位和创建 UInt32
值(value)。这个图像结果很好,因为我可以将它转换为 UIImage
并将其保存到我的照片库中。
当我尝试将此图像作为纹理发送到计算着色器时出现问题。下面是我的着色器代码:
kernel void updateEnvironmentMap(texture2d<uint, access::read> currentFrameTexture [[texture(0)]],
texture2d<uint, access::read> coordinateConversionTexture [[texture(1)]],
texture2d<uint, access::write> environmentMap [[texture(2)]]
uint2 gid [[thread_position_in_grid]])
{
const uint4 pixel = {255, 127, 63, 255};
environmentMap.write(pixel, gid);
}
此代码的问题是我的纹理类型是 uint
,它是 32 位的,我想通过附加 4 个 8 位值来生成 32 位像素,就像我在 CPU 上所做的那样。但是,我似乎无法在 Metal 上执行此操作,因为没有 byte
我可以将其附加在一起并组成一个 uint32
的类型.所以,我的问题是,在 Metal 计算着色器上处理 2D 纹理和设置 32 位像素的正确方法是什么?
奖励问题:另外,我已经看到带有 texture2d<float, access::read>
的示例着色器代码作为输入纹理类型。我假设它代表一个介于 0.0 和 1.0 之间的值,但与值介于 0 和 255 之间的无符号整数相比,它有什么优势?
编辑:澄清一下,着色器的输出纹理,environmentMap
, 具有与输入纹理完全相同的属性(宽度、高度、像素格式等)。为什么我认为这是违反直觉的,因为我们正在设置 uint4
作为一个像素,这意味着它由 4 个 32 位值组成,而每个像素应该是 32 位。使用当前代码,{255, 127, 63, 255}
结果与 {2550, 127, 63, 255}
完全相同,这意味着在写入输出纹理之前,这些值会以某种方式被限制在 0-255 之间。但这是非常违反直觉的。
最佳答案
其中的魔法比您似乎熟悉的要多一些,所以我会尝试阐明。
首先,根据设计,Metal 中纹理的存储格式与您读取/采样时获得的类型之间存在松散的联系。您可以在 .bgra8Unorm
中拥有纹理格式化,当通过绑定(bind)为 texture2d<float, access::sample>
的纹理采样时会给你一个float4
其组件按 RGBA 顺序排列。从这些打包字节到带有混合组件的浮点向量的转换遵循 Metal 着色语言规范中指定的详细记录的转换规则。
还有一种情况是,当写入存储为(例如)每个组件 8 位的纹理时,值将被限制以适合底层存储格式。这进一步受到纹理是否为 norm
的影响。类型:如果格式包含 norm
,这些值被解释为好像它们指定了 0 到 1 之间的值。否则,您读取的值不会被标准化。
一个例子:如果一个纹理是.bgra8Unorm
并且给定像素包含字节值 [0, 64, 128, 255]
,然后在请求 float
的着色器中读取时组件,你会得到 [0.5, 0.25, 0, 1.0]
当你取样时。相比之下,如果格式为 .rgba8Uint
, 你会得到 [0, 64, 128, 255]
.纹理的存储格式对其内容在采样时的解释方式具有重要影响。
我假设你的纹理的像素格式类似于 .rgba8Unorm
.如果是这种情况,您可以通过这样编写内核来实现您想要的:
kernel void updateEnvironmentMap(texture2d<float, access::read> currentFrameTexture [[texture(0)]],
texture2d<float, access::read> coordinateConversionTexture [[texture(1)]],
texture2d<float, access::write> environmentMap [[texture(2)]]
uint2 gid [[thread_position_in_grid]])
{
const float4 pixel(255, 127, 63, 255);
environmentMap.write(pixel * (1 / 255.0), gid);
}
相比之下,如果您的纹理格式为 .rgba8Uint
, 你会通过这样写得到同样的效果:
kernel void updateEnvironmentMap(texture2d<float, access::read> currentFrameTexture [[texture(0)]],
texture2d<float, access::read> coordinateConversionTexture [[texture(1)]],
texture2d<float, access::write> environmentMap [[texture(2)]]
uint2 gid [[thread_position_in_grid]])
{
const float4 pixel(255, 127, 63, 255);
environmentMap.write(pixel, gid);
}
我知道这是一个玩具示例,但我希望通过上述信息,您可以弄清楚如何正确存储和采样值以实现您想要的。
关于ios - 将具有 UInt8 组件类型的纹理传递给 Metal 计算着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738441/
我必须在文本区域上绘制哪些选项? 我认识一些所见即所得的编辑器。我不确定他们使用的方法是什么,并且会看看他们以了解,但我知道像 tinymce 这样的事情已经存在了一段时间,所以我猜现在可能有更现代的
我在我的 woo commerce 商店页面中创建了一个子类别排序菜单。我想将链接到我现在所在页面的链接设置为红色。我对 css 没有问题,只是用 js 脚本。我怎样才能让它突出当前?这是我创建的排序
所以我使用 TextWrangler4.5.3 并主要使用 Python 编写脚本。当我编写 Python 时,我经常喜欢发表类似这样的评论:# BEGINS foo 然后是 # ENDS foo 我
我需要你的帮助! 我有一个表格,里面有行(姓名等)现在,当位于该行上的对象具有特定值时,我想为特定的 tableCells 背景着色。但我只得到它来读取这个单元格的值。但我需要阅读对象(在我的代码中称
有人知道在 uikit 中给 uiactionsheet 着色吗? 最佳答案 是的,因为它是一个 UIView(如 kmit 所述),您可以使用以下命令:addSubview,因此您可以添加自己的背景
在 vba 中,我想从工作表中的单元格中读取颜色来为条形图中的特定条形着色。我的问题是我的颜色格式为 4F9F92,但要在 makro 中读取它,我需要使用单元格中的 excel 颜色代码 40762
我正在尝试绘制一个 geom_histogram,其中条形图由渐变着色。 这就是我想要做的: library(ggplot2) set.seed(1) df <- data.frame(id=past
我正在构建一个带有 shiny 的应用程序,并使用与 Shiny gallery ( http://shiny.rstudio.com/gallery/slider-bar-and-slider-ra
在 iOS 6 和 xcode 4 中,我有这个: https://dl.dropboxusercontent.com/u/60718318/photo.PNG使用代码可以轻松创建 [editButt
我正在向我的 iPhone UI 添加自定义按钮,并希望使它们具有 Apple 应用程序中的玻璃外观。我有一个很好的默认玻璃图像,但我不想为我想要的每种色调(红色、绿色、蓝色等)都有一个单独的图像。
我到处寻找但没有找到解决方案。我有图像 1。如何以编程方式使用渐变对它们进行着色以获得图像 2 和 3?以下是这些图像: 我通过 Photoshop 应用到它们的色调是简单的 2 色线性渐变。 我的问
有没有办法告诉 OS X 自动设置 NSToolbarItem 的样式/色调? 我通过 IB/Xcode 添加了一个“图像工具栏项”,并将图标设置为黑色 PDF as described in the
我试图区分列范围图表上的多种类型的数据。问题是我希望能够按多个标准对它们进行分组。 我可以轻松地为不同的列着色,从而解决一个问题,但我希望能够根据我传递到图表的数据,为 xAxis 上的类别(图表是倒
有人在编译模式 Emacs 中添加了对 ansi-color 的支持吗?如果是这样,颜色写入程序必须检查什么属性/属性才能确保其事件终端支持 ANSI 转义着色。 最佳答案 已经有一个函数可以将颜色应
我正在尝试实现 Phong 着色模型,但我不确定如何选择光源。更具体地说,我不明白光源应该是什么样子。它应该是vec3吗?或者也许是我在 Java 代码中定义的矩阵?另外,如果我们假设我希望相机作为光
我有 2 个 dhtmlxSidebars,就像示例中一样 here如何为嵌套背景设置不同的背景颜色?如果我添加CSS .dhxsidebar_side { background-color:
我有一个 JTable,每行都根据最后一列中的值着色。 但是,当我单击标题对行进行排序时,颜色不会跟随行。 我尝试在 JTable 鼠标退出事件上调用我的“colourTable”方法(我知道 Hac
geom_hline 似乎忽略了美观,我错过了什么还是这是一个错误? df session 信息: R version 3.4.4 (2018-03-15) Platform: x86_64-pc-
这个问题已经有答案了: How to draw a BufferedImage with a color tint (1 个回答) 已关闭 8 年前。 如何将通过此处的图标着色为不同的颜色?假设我想拍
我想为除我点击的那个之外的所有 div 着色。在我的代码中它有效,但只有一次。如果我点击另一个 div,它不起作用。 http://jsfiddle.net/6VhK8/353/ id="1
我是一名优秀的程序员,十分优秀!