图形编译器¶
在渲染一个 InstructionGroup 之前,我们会编译该组,以减少渲染时执行的指令数量。
减少上下文指令¶
假设你有这样一个方案:
Color(1, 1, 1)
Rectangle(source='button.png', pos=(0, 0), size=(20, 20))
Color(1, 1, 1)
Rectangle(source='button.png', pos=(10, 10), size=(20, 20))
Color(1, 1, 1)
Rectangle(source='button.png', pos=(10, 20), size=(20, 20))
图形画布实际看到的指令如下:
Color: change 'color' context to 1, 1, 1
BindTexture: change 'texture0' to `button.png texture`
Rectangle: push vertices (x1, y1...) to vbo & draw
Color: change 'color' context to 1, 1, 1
BindTexture: change 'texture0' to `button.png texture`
Rectangle: push vertices (x1, y1...) to vbo & draw
Color: change 'color' context to 1, 1, 1
BindTexture: change 'texture0' to `button.png texture`
Rectangle: push vertices (x1, y1...) to vbo & draw
只有第一个 Color 和 BindTexture 是有用的,并且真正改变了上下文。我们可以将它们简化为:
Color: change 'color' context to 1, 1, 1
BindTexture: change 'texture0' to `button.png texture`
Rectangle: push vertices (x1, y1...) to vbo & draw
Rectangle: push vertices (x1, y1...) to vbo & draw
Rectangle: push vertices (x1, y1...) to vbo & draw
这正是编译器首先所做的,通过给所有未使用的指令标记GI_IGNORE标志。一旦Color内容发生变化,整个InstructionGroup将被重新编译,而先前未使用的Color可能会在下次编译中被使用。
致所有Kivy贡献者/内部开发者:
所有上下文指令都会被检查,以确认它们是否对缓存有任何更改。
我们必须确保当前画布需要一条上下文指令。
我们必须确保不依赖于任何其他画布。
如果我们的某个子节点是另一个指令组,我们必须重置缓存,因为我们不知道它是否会做出一些奇怪的行为。