Skip to content

Commit 4e85ef8

Browse files
authored
Merge pull request #275 from baaahs/refactor-patch-storage
WIP—Refactor patch storage
2 parents 5481dc6 + 01c60cd commit 4e85ef8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1134
-389
lines changed

src/commonMain/kotlin/baaahs/OpenShow.kt

+32-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
11
package baaahs
22

3-
import baaahs.glshaders.OpenPatch
4-
import baaahs.glsl.GlslContext
53
import baaahs.show.*
64
import baaahs.show.live.LiveShaderInstance
75

86
open class OpenPatchy(
9-
patchy: Patchy, val dataSources: Map<String, DataSource>
7+
patchy: Patchy,
8+
allShaderInstances: Map<String, LiveShaderInstance>,
9+
allDataSources: Map<String, DataSource>
1010
) {
11+
val title = patchy.title
12+
val patches = patchy.patches.map { OpenPatch(it, allShaderInstances) }
13+
1114
val controlLayout: Map<String, List<Control>> = patchy.controlLayout.mapValues { (_, controlRefs) ->
12-
controlRefs.map { it.dereference(dataSources) }
15+
controlRefs.map { it.dereference(allDataSources) }
1316
}
1417
}
1518

19+
class OpenPatch(
20+
val shaderInstances: List<LiveShaderInstance>,
21+
val surfaces: Surfaces
22+
) {
23+
constructor(patch: Patch, allShaderInstances: Map<String, LiveShaderInstance>): this(
24+
patch.shaderInstanceIds.map {
25+
allShaderInstances.getBang(it, "shader instance")
26+
},
27+
patch.surfaces
28+
)
29+
}
30+
1631
class OpenShow(
17-
private val show: Show, private val showResources: ShowResources
18-
) : RefCounted by RefCounter(), OpenPatchy(show, show.dataSources) {
32+
private val show: Show,
33+
private val showPlayer: ShowPlayer,
34+
private val allShaderInstances: Map<String, LiveShaderInstance>
35+
) : RefCounted by RefCounter(), OpenPatchy(show, allShaderInstances, show.dataSources) {
1936
val id = randomId("show")
2037
val layouts get() = show.layouts
21-
val shaders = show.shaders.mapValues { (_, shader) ->
22-
showResources.openShader(shader, addToCache = true)
23-
}
24-
val shaderInstances = show.shaderInstances.mapValues { (_, shaderInstance) ->
25-
LiveShaderInstance.from(shaderInstance, shaders)
26-
}
38+
39+
val allDataSources = show.dataSources
2740

2841
val dataFeeds = show.dataSources.entries.associate { (id, dataSource) ->
29-
val dataFeed = showResources.openDataFeed(id, dataSource)
42+
val dataFeed = showPlayer.openDataFeed(id, dataSource)
3043
id to dataFeed
3144
}
3245
val scenes = show.scenes.map { OpenScene(it) }
@@ -35,26 +48,19 @@ class OpenShow(
3548
ShowEditor(show, showState).apply(block)
3649

3750
override fun onFullRelease() {
38-
shaders.values.forEach { it.release() }
51+
allShaderInstances.values.forEach { it.release() }
3952
dataFeeds.values.forEach { it.release() }
4053
}
4154

42-
inner class OpenScene(scene: Scene) : OpenPatchy(scene, show.dataSources) {
55+
inner class OpenScene(scene: Scene) : OpenPatchy(scene, allShaderInstances, show.dataSources) {
4356
val id = randomId("scene")
44-
val title = scene.title
4557
val patchSets = scene.patchSets.map { OpenPatchSet(it) }
4658

47-
inner class OpenPatchSet(patchSet: PatchSet) : OpenPatchy(patchSet, show.dataSources) {
59+
inner class OpenPatchSet(patchSet: PatchSet) : OpenPatchy(patchSet, allShaderInstances, show.dataSources) {
4860
val id = randomId("patchset")
49-
val title = patchSet.title
50-
val patches = patchSet.patches.map { OpenPatch(it, shaderInstances, show.dataSources) }
51-
52-
fun createRenderPlan(glslContext: GlslContext): RenderPlan {
53-
val activeDataSources = mutableSetOf<String>()
54-
val programs = patches.map { patch ->
55-
patch to patch.createProgram(glslContext, dataFeeds)
56-
}
57-
return RenderPlan(programs)
61+
62+
fun activePatches(): List<OpenPatchy> {
63+
return listOf(this@OpenShow, this@OpenScene, this)
5864
}
5965
}
6066
}

src/commonMain/kotlin/baaahs/ShowResources.kt src/commonMain/kotlin/baaahs/ShowPlayer.kt

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import baaahs.glshaders.GlslAnalyzer
44
import baaahs.glshaders.GlslProgram
55
import baaahs.glshaders.OpenShader
66
import baaahs.glshaders.Plugins
7+
import baaahs.glsl.AnalysisException
78
import baaahs.glsl.GlslContext
89
import baaahs.model.ModelInfo
910
import baaahs.show.DataSource
1011
import baaahs.show.Shader
1112
import baaahs.show.Show
13+
import baaahs.show.live.ShowOpener
1214

13-
interface ShowResources {
15+
interface ShowPlayer {
1416
val plugins: Plugins
1517
val glslContext: GlslContext
1618
val modelInfo: ModelInfo
@@ -20,17 +22,24 @@ interface ShowResources {
2022
fun <T : Gadget> useGadget(id: String): T
2123

2224
fun openShader(shader: Shader, addToCache: Boolean = false): OpenShader
25+
fun openShaderOrNull(shader: Shader, addToCache: Boolean = false): OpenShader? {
26+
return try {
27+
openShader(shader, addToCache)
28+
} catch (e: AnalysisException) {
29+
null
30+
}
31+
}
2332
fun openDataFeed(id: String, dataSource: DataSource): GlslProgram.DataFeed
2433
fun useDataFeed(dataSource: DataSource): GlslProgram.DataFeed
25-
fun openShow(show: Show): OpenShow = OpenShow(show, this)
34+
fun openShow(show: Show): OpenShow = ShowOpener(show, this).openShow()
2635

2736
fun releaseUnused()
2837
}
2938

30-
abstract class BaseShowResources(
39+
abstract class BaseShowPlayer(
3140
final override val plugins: Plugins,
3241
final override val modelInfo: ModelInfo
33-
) : ShowResources {
42+
) : ShowPlayer {
3443
private val glslAnalyzer = GlslAnalyzer()
3544

3645
private val dataFeeds = mutableMapOf<DataSource, GlslProgram.DataFeed>()

src/commonMain/kotlin/baaahs/ShowRunner.kt

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package baaahs
22

33
import baaahs.OpenShow.OpenScene.OpenPatchSet
4+
import baaahs.glshaders.AutoWirer
5+
import baaahs.glshaders.Plugins
46
import baaahs.glsl.GlslRenderer
57
import baaahs.show.Show
68

@@ -19,7 +21,7 @@ class ShowRunner(
1921
private var nextPatchSet: OpenPatchSet? = showState.findPatchSet(openShow)
2022

2123
private var currentPatchSet: OpenPatchSet? = null
22-
private var currentRenderPlan: RenderPlan? = null
24+
internal var currentRenderPlan: RenderPlan? = null
2325

2426
private var requestedGadgets: LinkedHashMap<String, Gadget> = linkedMapOf()
2527

@@ -74,7 +76,7 @@ class ShowRunner(
7476
return renderPlan != null
7577
}
7678

77-
fun housekeeping() {
79+
fun housekeeping(): Boolean {
7880
var remapSurfaces = surfaceManager.requiresRemap()
7981

8082
// Maybe switch to a new show.
@@ -93,11 +95,21 @@ class ShowRunner(
9395
surfaceManager.remap(it)
9496
}
9597
}
98+
99+
return remapSurfaces
96100
}
97101

98102
private fun prepare(newPatchSet: OpenPatchSet): RenderPlan {
103+
val autoWirer = AutoWirer(Plugins.findAll())
104+
105+
val activePatches = newPatchSet.activePatches()
106+
val entryPoints = autoWirer.merge(*activePatches.toTypedArray())
99107
val glslContext = glslRenderer.gl
100-
return newPatchSet.createRenderPlan(glslContext)
108+
val activeDataSources = mutableSetOf<String>()
109+
val programs = entryPoints.map { (_, liveShaderInstance) ->
110+
liveShaderInstance to liveShaderInstance.createProgram(glslContext, openShow.dataFeeds)
111+
}
112+
return RenderPlan(programs)
101113
}
102114

103115
fun shutDown() {

src/commonMain/kotlin/baaahs/StageManager.kt

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package baaahs
22

33
import baaahs.glshaders.GlslProgram
4-
import baaahs.glshaders.OpenPatch
4+
import baaahs.glshaders.LinkedPatch
55
import baaahs.glshaders.Plugins
66
import baaahs.glsl.GlslContext
77
import baaahs.glsl.GlslRenderer
@@ -11,12 +11,15 @@ import baaahs.io.PubSubRemoteFsServerBackend
1111
import baaahs.io.RemoteFsSerializer
1212
import baaahs.mapper.Storage
1313
import baaahs.model.ModelInfo
14-
import baaahs.show.*
14+
import baaahs.show.Show
15+
import baaahs.show.Surfaces
16+
import baaahs.show.buildEmptyShow
1517
import com.soywiz.klock.DateTime
1618
import kotlinx.coroutines.CoroutineScope
1719
import kotlinx.coroutines.Dispatchers
1820
import kotlinx.coroutines.launch
1921
import kotlinx.serialization.Serializable
22+
import kotlinx.serialization.modules.SerializersModule
2023

2124
class StageManager(
2225
plugins: Plugins,
@@ -28,7 +31,7 @@ class StageManager(
2831
private val movingHeadManager: MovingHeadManager,
2932
private val clock: Clock,
3033
modelInfo: ModelInfo
31-
) : BaseShowResources(plugins, modelInfo) {
34+
) : BaseShowPlayer(plugins, modelInfo) {
3235
val facade = Facade()
3336
override val glslContext: GlslContext
3437
get() = glslRenderer.gl
@@ -116,17 +119,21 @@ class StageManager(
116119
showRunner?.let { showRunner ->
117120
// Unless otherwise instructed, = generate and send the next frame right away,
118121
// then perform any housekeeping tasks immediately afterward, to avoid frame lag.
119-
if (dontProcrastinate) showRunner.housekeeping()
122+
if (dontProcrastinate) housekeeping()
120123

121124
if (showRunner.renderNextFrame()) {
122125
surfaceManager.sendFrame()
123126
dmxUniverse.sendFrame()
124127
}
125128

126-
if (!dontProcrastinate) showRunner.housekeeping()
129+
if (!dontProcrastinate) housekeeping()
127130
}
128131
}
129132

133+
private fun housekeeping() {
134+
if (showRunner!!.housekeeping()) facade.notifyChanged()
135+
}
136+
130137
fun shutDown() {
131138
showRunner?.release()
132139
showStateChannel.unsubscribe()
@@ -139,34 +146,20 @@ class StageManager(
139146
var showIsUnsaved: Boolean = false
140147

141148
init {
142-
val commands = Topics.Commands(remoteFsSerializer)
143-
pubSub.listenOnCommandChannel(commands.newShow) { command, reply -> handleNewShow() }
149+
val commands = Topics.Commands(SerializersModule {
150+
include(remoteFsSerializer.serialModule)
151+
include(plugins.serialModule)
152+
})
153+
pubSub.listenOnCommandChannel(commands.newShow) { command, reply -> handleNewShow(command) }
144154
pubSub.listenOnCommandChannel(commands.switchToShow) { command, reply -> handleSwitchToShow(command.file) }
145155
pubSub.listenOnCommandChannel(commands.saveShow) { command, reply -> handleSaveShow() }
146156
pubSub.listenOnCommandChannel(commands.saveAsShow) { command, reply ->
147157
handleSaveAsShow(storage.resolve(command.file.fullPath))
148158
}
149159
}
150160

151-
private fun handleNewShow() {
152-
val newShow = ShowEditor("Untitled").apply {
153-
addScene("Scene 1") {
154-
addPatchSet("All Dark") {
155-
}
156-
}
157-
addControl("Scenes", SpecialControl("baaahs.Core:Scenes"))
158-
addControl("Patches", SpecialControl("baaahs.Core:Patches"))
159-
160-
editLayouts {
161-
copyFrom(
162-
Layouts(
163-
listOf("Scenes", "Patches", "More Controls", "Preview", "Controls"),
164-
mapOf("default" to SampleData.defaultLayout)
165-
)
166-
)
167-
}
168-
}
169-
switchTo(newShow.getShow(), newShow.getShowState())
161+
private fun handleNewShow(command: NewShowCommand) {
162+
switchTo(command.template ?: buildEmptyShow())
170163
}
171164

172165
private fun handleSwitchToShow(file: Fs.File?) {
@@ -209,6 +202,12 @@ class StageManager(
209202
inner class Facade : baaahs.ui.Facade() {
210203
val currentShow: Show?
211204
get() = this@StageManager.showRunner?.show
205+
206+
val currentGlsl: Map<Surfaces, String>?
207+
get() = this@StageManager.showRunner?.currentRenderPlan
208+
?.programs?.map { (patch, program) ->
209+
patch.surfaces to program.fragShader.source
210+
}?.associate { it }
212211
}
213212
}
214213

@@ -238,7 +237,7 @@ class RefCounter : RefCounted {
238237
}
239238
}
240239

241-
class RenderPlan(val programs: List<Pair<OpenPatch, GlslProgram>>) {
240+
class RenderPlan(val programs: List<Pair<LinkedPatch, GlslProgram>>) {
242241
fun render(glslRenderer: GlslRenderer) {
243242
glslRenderer.draw()
244243
}
@@ -250,7 +249,9 @@ data class ClientData(
250249
)
251250

252251
@Serializable
253-
class NewShowCommand {
252+
class NewShowCommand(
253+
val template: Show? = null
254+
) {
254255
@Serializable
255256
class Response
256257
}

src/commonMain/kotlin/baaahs/SurfaceManager.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package baaahs
22

33
import baaahs.glshaders.GlslProgram
4-
import baaahs.glshaders.OpenPatch
4+
import baaahs.glshaders.LinkedPatch
55
import baaahs.glsl.GlslRenderer
66
import baaahs.glsl.RenderSurface
77

@@ -35,7 +35,7 @@ class SurfaceManager(
3535

3636
fun remap(renderPlan: RenderPlan) {
3737
renderSurfaces.forEach { (surface, renderSurface) ->
38-
renderPlan.programs.forEach { (patch: OpenPatch, program: GlslProgram) ->
38+
renderPlan.programs.forEach { (patch: LinkedPatch, program: GlslProgram) ->
3939
if (patch.matches(surface)) {
4040
renderSurface.useProgram(program)
4141
}

src/commonMain/kotlin/baaahs/Topics.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import kotlinx.serialization.builtins.MapSerializer
66
import kotlinx.serialization.builtins.list
77
import kotlinx.serialization.builtins.nullable
88
import kotlinx.serialization.builtins.serializer
9+
import kotlinx.serialization.modules.SerialModule
910

1011
object Topics {
1112
fun createClientData(fsSerializer: RemoteFsSerializer) =
@@ -25,18 +26,18 @@ object Topics {
2526
MapSerializer(String.serializer(), MovingHead.MovingHeadPosition.serializer())
2627
)
2728

28-
class Commands(fsSerializer: RemoteFsSerializer) {
29+
class Commands(serialModule: SerialModule) {
2930
val newShow =
3031
PubSub.CommandPort("pinky/newShow",
31-
NewShowCommand.serializer(), NewShowCommand.Response.serializer(), fsSerializer.serialModule)
32+
NewShowCommand.serializer(), NewShowCommand.Response.serializer(), serialModule)
3233
val switchToShow =
3334
PubSub.CommandPort("pinky/switchToShow",
34-
SwitchToShowCommand.serializer(), SwitchToShowCommand.Response.serializer(), fsSerializer.serialModule)
35+
SwitchToShowCommand.serializer(), SwitchToShowCommand.Response.serializer(), serialModule)
3536
val saveShow =
3637
PubSub.CommandPort("pinky/saveShow",
37-
SaveShowCommand.serializer(), SaveShowCommand.Response.serializer(), fsSerializer.serialModule)
38+
SaveShowCommand.serializer(), SaveShowCommand.Response.serializer(), serialModule)
3839
val saveAsShow =
3940
PubSub.CommandPort("pinky/saveAsShow",
40-
SaveAsShowCommand.serializer(), SaveAsShowCommand.Response.serializer(), fsSerializer.serialModule)
41+
SaveAsShowCommand.serializer(), SaveAsShowCommand.Response.serializer(), serialModule)
4142
}
4243
}

src/commonMain/kotlin/baaahs/client/ClientShowResources.kt src/commonMain/kotlin/baaahs/client/ClientShowPlayer.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import baaahs.glsl.GlslContext
66
import baaahs.model.ModelInfo
77
import kotlinx.serialization.json.JsonElement
88

9-
class ClientShowResources(
9+
class ClientShowPlayer(
1010
plugins: Plugins,
1111
override val glslContext: GlslContext,
1212
private val pubSub: PubSub.Client,
1313
modelInfo: ModelInfo
14-
) : BaseShowResources(plugins, modelInfo) {
14+
) : BaseShowPlayer(plugins, modelInfo) {
1515
private val gadgets: MutableMap<String, ClientGadget> = mutableMapOf()
1616

1717
override fun <T : Gadget> createdGadget(id: String, gadget: T) {

0 commit comments

Comments
 (0)