- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将一个结构数组传递给我的 Metal 着色器顶点函数。该结构如下所示:
struct Vertex {
var x,y,z: Float // position data
var r,g,b,a: Float // color data
var s,t: Float // texture coordinates
var nX,nY,nZ: Float // normal
func floatBuffer() -> [Float] {
return [x,y,z,r,g,b,a,s,t,nX,nY,nZ]
}
};
floatBuffer 函数用于将顶点组装成一个大的 Floats 数组。我可以通过使用使用“打包”数据类型的结构定义将它传递到我的着色器函数中,如下所示:
struct VertexIn {
packed_float3 position;
packed_float4 color;
packed_float2 texCoord;
packed_float3 normal;
};
vertex VertexOut basic_vertex(
const device VertexIn* vertex_array [[ buffer(0) ]],
.
.
.
这行得通。但是,我想知道如何使用 MTLVertexAttributeDescriptors 和相关语法来做同样的事情。现在我得到了损坏的多边形,大概是因为 float3 和 packed_float3 的字节对齐差异?
这就是我现在尝试定义它并获取垃圾多边形的方式。我收到“packed_float3”对属性无效的错误,所以我想弄清楚如何使常规 float3、float4 等工作。
struct VertexIn {
float3 position [[attribute(RayVertexAttributePosition)]];
float4 color [[attribute(RayVertexAttributeColor)]];
float2 texCoord [[attribute(RayVertexAttributeTexCoord)]];
float3 normal [[attribute(RayVertexAttributeNormal)]];
};
class func buildMetalVertexDescriptor() -> MTLVertexDescriptor {
let mtlVertexDescriptor = MTLVertexDescriptor()
var offset = 0
mtlVertexDescriptor.attributes[RayVertexAttribute.position.rawValue].format = MTLVertexFormat.float3
mtlVertexDescriptor.attributes[RayVertexAttribute.position.rawValue].offset = offset
mtlVertexDescriptor.attributes[RayVertexAttribute.position.rawValue].bufferIndex = RayBufferIndex.positions.rawValue
offset += 3*MemoryLayout<Float>.stride
mtlVertexDescriptor.attributes[RayVertexAttribute.color.rawValue].format = MTLVertexFormat.float4
mtlVertexDescriptor.attributes[RayVertexAttribute.color.rawValue].offset = offset
mtlVertexDescriptor.attributes[RayVertexAttribute.color.rawValue].bufferIndex = RayBufferIndex.positions.rawValue
offset += MemoryLayout<float4>.stride
mtlVertexDescriptor.attributes[RayVertexAttribute.texCoord.rawValue].format = MTLVertexFormat.float2
mtlVertexDescriptor.attributes[RayVertexAttribute.texCoord.rawValue].offset = offset
mtlVertexDescriptor.attributes[RayVertexAttribute.texCoord.rawValue].bufferIndex = RayBufferIndex.positions.rawValue
offset += MemoryLayout<float2>.stride
mtlVertexDescriptor.attributes[RayVertexAttribute.normal.rawValue].format = MTLVertexFormat.float3
mtlVertexDescriptor.attributes[RayVertexAttribute.normal.rawValue].offset = offset
mtlVertexDescriptor.attributes[RayVertexAttribute.normal.rawValue].bufferIndex = RayBufferIndex.positions.rawValue
offset += 3*MemoryLayout<Float>.stride
print("stride \(offset)")
mtlVertexDescriptor.layouts[RayBufferIndex.positions.rawValue].stride = offset
mtlVertexDescriptor.layouts[RayBufferIndex.positions.rawValue].stepRate = 1
mtlVertexDescriptor.layouts[RayBufferIndex.positions.rawValue].stepFunction = MTLVertexStepFunction.perVertex
return mtlVertexDescriptor
}
请注意,我将第一个属性指定为 float3,但我指定了 3 个 float 的偏移量,而不是 float3 通常使用的 4 个 float 。但显然这还不够。我想知道如何设置 MTLVertexDescriptor 和带有属性的着色器结构,以便它处理来 self 的结构的“打包”数据?
非常感谢。
最佳答案
关键在于您问题的这一部分:“请注意,我将第一个属性指定为 float3,但我指定了 3 个 float 的偏移量,而不是 float3 通常使用的 4 个 float ”。
SIMD float3 类型占用 16 个字节,它与非打包的 Metal float3 类型具有相同的内存布局。因此,当您将偏移量设置为仅 3*MemoryLayout.stride 时,您会丢失仍然存在的最后 4 个字节,从而导致下一个字段从这些额外字节中提取并偏移其余数据。
要真正使用打包类型将数据传输到 Metal(或任何图形 API),您要么必须坚持之前所做的并在数组中的三个单独的 Floats 中指定 x、y、z,要么必须定义你自己的结构是这样的:
struct Vector3 {
var x: Float
var y: Float
var z: Float
}
Swift 不保证这个结构将是三个 Float 紧密打包在一起,但在现在和可预见的 future 它可以工作,并且在大多数平台上大小为 12 字节。
如果您希望能够对这样的结构进行向量运算,那么我建议您寻找一个定义此类类型的库,以节省您一些时间,因为您也会遇到与 3x3 矩阵相同类型的问题.
我遇到了同样的问题,所以我最终推出了自己的: https://github.com/jkolb/Swiftish
关于metal - 如何在 Metal 顶点着色器 MTL 顶点属性描述符中描述 packed_float3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47061717/
在一个环境中,我有 pandas 版本 0.17.0 和 numpy 版本 1.10.1。在另一个环境中,我有 pandas 版本 0.18.1 和 numpy 版本 1.10.4。 我运行这段代码
This question already has answers here: Default stringify for objects, equivalent to Java's toString
我一直在尝试为我的profile命令嵌入设置一个人们可以更改的简历。我认为它应该有效,但我的代码似乎有问题。 相关代码如下: const PREFIX = '!'; var bio = {}; cli
我正在尝试获取网站图标、网站标题和外部 URL 列表的描述,最好使用 jquery。我已经成功地为我的网址同步了谷歌的图标服务,任何人都可以阐明如何实现网站标题和描述吗?这是我到目前为止获得图标的内容
我在尝试运行代码时收到错误。找不到问题出在哪里。我可能遗漏了一些小细节,如果您能纠正它那就太好了。 计算Servlet import java.io.IOException; impo
我的数据库中有两个字段,一个是描述(TEXT),另一个是short_desc(VARCHAR-200)。 当我显示搜索结果时,我显然使用了short_desc,当有人点击该项目时,他们会得到完整的描述
当我尝试通过ajax和Jquery调用 Controller 上的save方法时,我正在使用Spring MVC、Jquery、Hibernate和tomcat。单击“保存”按钮时,我在 tomcat
我试图使用describe()来获取一些描述性统计数据,但获取了应该是数字的nan值。 我尝试使用 axis=0 或 axis=1,而 axis=1 产生了正确的数值,但这不是正确的数值我需要; ax
我有超过 1 亿个字符串要存储在文件系统中。与字符串 (~255Chars utf8) 一起,将有两个日期和一些定义其属性的整数值。 我可以将它们放在一个 CSV 文件中,但它会很大。我可以将几个较小
有没有办法将 JavaScript 合并到 Jenkins 顶部的描述字段中? 每当我添加脚本标签时,当您查看源代码时,它都会被 Jenkins 删除。 如果有人有建议或方向指出我,那就太好了。
您如何获得 SEH 的名称和/或描述?异常无需必须将字符串硬编码到您的应用程序中? 我尝试使用 FormatMessage(),但它有时会截断消息,即使您指定忽略插入也是如此: __asm { //
如果我在 MySQL 中使用 CREATE TRIGGER 语法创建一个触发器,我该如何附加注释来描述它?我的意思是在声明中或之后对我来说是一样的。 我想不通。对于表格,您可以在声明末尾添加 COMM
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
当开发多媒体应用或者游戏应用的时候,需要使用音量控制键来设置程序的音量大小。在Android系统中有多中音频流,通过Activity中的函数 setVolumeControlStream(int s
Slick DSL 允许通过两种方式在表中创建可选字段。 对于这个案例类: case class User(id: Option[Long] = None, fname: String, lname:
如果不属于默认命名空间,我如何描述 pod 信息。使用默认命名空间我没有任何问题。 但我想获得与命名空间对齐的特定 pod 的信息。 但是,当我想描述我可以制作的同一个 pod 时,请参阅 我尝试使用
在我使用过的几乎所有 vim 副本中,程序都会在替换文本后给出更改的描述。 (例如,将显示类似“20 行 92 个替换”之类的内容。) 我现在正在使用默认情况下不这样做的 vim 副本。 是否有一个简
我正在编写规范,需要描述一些 JSON 对象。单独的文本和选项卡往往会使大型 JSON 变得过于困惑。是否有任何在线(最好)工具可以创建类似于 http://www.json.org/ 上的图表的工具
我正在尝试通过 DNS 将我的 Kubernetes 部署连接在一起。 我有一个 Java (Spring Boot) 部署和一个 javascript (node.js) 部署,两者都通过默认的 C
我只是在学习 WebGL 图形编程。 我正在检查包含该语句的某人的代码 // multiply the position by the matrix. gl_Position = vec4((u_ma
我是一名优秀的程序员,十分优秀!