Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# 1.2.0
- Add contrast, brightness and hue sliders
- Fix a black or frozen screen on drivers that mishandle texture-to-texture copies (Intel Windows)
- Free the mod's render target when it is switched off (Fixes Leak)

# 1.1.1
- Optimize shader
40 changes: 38 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ tasks.processResources {
{ "sampler_name": "DiffuseSampler", "target": "minecraft:main" }
],
"uniforms": [
{ "name": "Saturation", "values": [ 1.0 ] }
{ "name": "Saturation", "values": [ 1.0 ] },
{ "name": "Contrast", "values": [ 1.0 ] },
{ "name": "Brightness", "values": [ 1.0 ] },
{ "name": "Hue", "values": [ 0.0 ] }
],
"output": "swap"
},
Expand All @@ -147,7 +150,10 @@ tasks.processResources {
{ "sampler_name": "DiffuseSampler", "target": "minecraft:main" }
],
"uniforms": [
{ "name": "Saturation", "type": "float", "values": [ 1.0 ] }
{ "name": "Saturation", "type": "float", "values": [ 1.0 ] },
{ "name": "Contrast", "type": "float", "values": [ 1.0 ] },
{ "name": "Brightness", "type": "float", "values": [ 1.0 ] },
{ "name": "Hue", "type": "float", "values": [ 0.0 ] }
],
"output": "swap"
},
Expand Down Expand Up @@ -183,6 +189,21 @@ tasks.processResources {
"name": "Saturation",
"type": "float",
"value": 1.0
},
{
"name": "Contrast",
"type": "float",
"value": 1.0
},
{
"name": "Brightness",
"type": "float",
"value": 1.0
},
{
"name": "Hue",
"type": "float",
"value": 0.0
}
]
},
Expand Down Expand Up @@ -217,6 +238,21 @@ tasks.processResources {
"name": "Saturation",
"type": "float",
"value": 1.0
},
{
"name": "Contrast",
"type": "float",
"value": 1.0
},
{
"name": "Brightness",
"type": "float",
"value": 1.0
},
{
"name": "Hue",
"type": "float",
"value": 0.0
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loom.ignoreDependencyLoomVersionValidation=true
# Mod information
mod.name=ColorSaturation
mod.id=colorsaturation
mod.version=1.1.1
mod.version=1.2.0
mod.group=org.polyfrost

# Loom settings, [VERSIONED] means to be overridden in each version's gradle.properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Mixin_ApplySaturation {
)
private void colorsaturation$applySaturation(DeltaTracker deltaTracker, boolean renderLevel, CallbackInfo ci) {
if (!ColorSaturationConfig.isEnabled || !this.minecraft.isGameLoadFinished() || this.minecraft.level == null) {
SaturationHandler.free();
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@ object ColorSaturationConfig : Config(
@Slider(title = "Saturation Strength", min = -1f, max = 5f, step = 0.05f)
var strength = 1f

@JvmField
@Slider(title = "Contrast", min = 0f, max = 2f, step = 0.05f)
var contrast = 1f

@JvmField
@Slider(title = "Brightness", min = 0f, max = 2f, step = 0.05f)
var brightness = 1f

@JvmField
@Slider(title = "Hue Shift", min = -180f, max = 180f, step = 1f)
var hue = 0f

@JvmStatic
val isIdentityStrength: Boolean
get() = abs(strength - 1f) < 1e-4f
val isIdentity: Boolean
get() = abs(strength - 1f) < 1e-4f &&
abs(contrast - 1f) < 1e-4f &&
abs(brightness - 1f) < 1e-4f &&
abs(hue) < 1e-4f

init {
addCallback("strength") {
SaturationHandler.updateShaderUniforms()
for (key in arrayOf("strength", "contrast", "brightness", "hue")) {
addCallback(key) {
SaturationHandler.updateShaderUniforms()
}
}
save()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package org.polyfrost.colorsaturation.client

//? if <1.21.11 {
//~ if >=1.21.11 'ResourceLocation' -> 'Identifier' {
import net.minecraft.resources.ResourceLocation

fun location(namespace: String, path: String): ResourceLocation =
ResourceLocation.fromNamespaceAndPath(namespace, path)
//?}

//? if >=1.21.11 {
/*import net.minecraft.resources.Identifier

fun location(namespace: String, path: String): Identifier =
Identifier.fromNamespaceAndPath(namespace, path)
*///?}
fun location(namespace: String, path: String) = ResourceLocation.fromNamespaceAndPath(namespace, path)
//~}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ object SaturationHandler {

@JvmStatic
val isActive: Boolean
get() = ColorSaturationConfig.isEnabled && !ColorSaturationConfig.isIdentityStrength
get() = ColorSaturationConfig.isEnabled && !ColorSaturationConfig.isIdentity

@JvmStatic
fun updateShaderUniforms() {
postChain?.setUniform("Saturation", ColorSaturationConfig.strength)
val chain = postChain ?: return
chain.setUniform("Saturation", ColorSaturationConfig.strength)
chain.setUniform("Contrast", ColorSaturationConfig.contrast)
chain.setUniform("Brightness", ColorSaturationConfig.brightness)
chain.setUniform("Hue", ColorSaturationConfig.hue)
}

@JvmStatic
fun free() {
postChain?.close()
postChain = null
prevWidth = -1
prevHeight = -1
}

@JvmStatic
Expand Down Expand Up @@ -92,12 +104,16 @@ import org.polyfrost.colorsaturation.ColorSaturationConstants

@JvmStatic
val isActive: Boolean
get() = ColorSaturationConfig.isEnabled && !ColorSaturationConfig.isIdentityStrength
get() = ColorSaturationConfig.isEnabled && !ColorSaturationConfig.isIdentity

@JvmStatic
fun updateShaderUniforms() {
}

@JvmStatic
fun free() {
}

@JvmStatic
fun render(renderTarget: RenderTarget, resourcePool: CrossFrameResourcePool) {
if (!isActive) {
Expand Down Expand Up @@ -125,10 +141,16 @@ import org.polyfrost.colorsaturation.ColorSaturationConstants
targetBundle,
Consumer { renderPass: com.mojang.blaze3d.systems.RenderPass ->
renderPass.setUniform("Saturation", ColorSaturationConfig.strength)
renderPass.setUniform("Contrast", ColorSaturationConfig.contrast)
renderPass.setUniform("Brightness", ColorSaturationConfig.brightness)
renderPass.setUniform("Hue", ColorSaturationConfig.hue)
}
)
//?} else {
/*shader?.setUniform("Saturation", ColorSaturationConfig.strength)
shader?.setUniform("Contrast", ColorSaturationConfig.contrast)
shader?.setUniform("Brightness", ColorSaturationConfig.brightness)
shader?.setUniform("Hue", ColorSaturationConfig.hue)
shader?.addToFrame(
builder,
renderTarget.viewWidth,
Expand Down Expand Up @@ -171,50 +193,67 @@ import org.polyfrost.colorsaturation.ColorSaturationConstants
import java.util.OptionalInt

object SaturationHandler {
private val pipeline by lazy {
RenderPipeline.builder()
.withLocation(location(ColorSaturationConstants.ID, "saturation_pipeline"))
private val saturationPipeline by lazy {
createPipeline("saturation_pipeline", "post/color_saturation", true)
}

private val blitPipeline by lazy {
createPipeline("blit_pipeline", "post/blit", false)
}

private fun createPipeline(name: String, fragmentShader: String, hasUniforms: Boolean): RenderPipeline {
val builder = RenderPipeline.builder()
.withLocation(location(ColorSaturationConstants.ID, name))
.withVertexShader(location(ColorSaturationConstants.ID, "core/fullscreen_triangle"))
.withFragmentShader(location(ColorSaturationConstants.ID, "post/color_saturation"))
//? if >=26.2 {
/*.withVertexBinding(0, DefaultVertexFormat.POSITION)
.withFragmentShader(location(ColorSaturationConstants.ID, fragmentShader))

//? if >=26.2 {
/*builder.withVertexBinding(0, DefaultVertexFormat.POSITION)
.withPrimitiveTopology(PrimitiveTopology.TRIANGLES)
.withDepthStencilState(Optional.empty())
.withColorTargetState(ColorTargetState.DEFAULT)
.withBindGroupLayout(
BindGroupLayout.builder()
.withSampler("DiffuseSampler")
.withUniform("SaturationConfig", UniformType.UNIFORM_BUFFER)
.build()
)
*///?}
//? if <26.2 {
.withVertexFormat(DefaultVertexFormat.POSITION, VertexFormat.Mode.TRIANGLES)
//?}
//? if >=26.1 && <26.2 {
/*.withDepthStencilState(Optional.empty())

val bindGroupLayout = BindGroupLayout.builder().withSampler("DiffuseSampler")
if (hasUniforms) {
bindGroupLayout.withUniform("SaturationConfig", UniformType.UNIFORM_BUFFER)
}
builder.withBindGroupLayout(bindGroupLayout.build())
*///?}
//? if <26.2 {
builder.withVertexFormat(DefaultVertexFormat.POSITION, VertexFormat.Mode.TRIANGLES)
//?}
//? if >=26.1 && <26.2 {
/*builder.withDepthStencilState(Optional.empty())
.withColorTargetState(ColorTargetState.DEFAULT)
*///?}
//? if <26.1 {
.withDepthWrite(false)
*///?}
//? if <26.1 {
builder.withDepthWrite(false)
.withDepthTestFunction(DepthTestFunction.NO_DEPTH_TEST)
.withColorWrite(true, true)
//?}
//? if <26.2 {
.withUniform("SaturationConfig", UniformType.UNIFORM_BUFFER)
.withSampler("DiffuseSampler")
//?}
.build()
//?}
//? if <26.2 {
if (hasUniforms) {
builder.withUniform("SaturationConfig", UniformType.UNIFORM_BUFFER)
}
builder.withSampler("DiffuseSampler")
//?}

return builder.build()
}

@JvmStatic
val isActive: Boolean
get() = ColorSaturationConfig.isEnabled && !ColorSaturationConfig.isIdentityStrength
get() = ColorSaturationConfig.isEnabled && !ColorSaturationConfig.isIdentity

@JvmStatic
fun updateShaderUniforms() {
}

@JvmStatic
fun free() {
InternalTargetTracker.free()
}

@JvmStatic
fun render(renderTarget: RenderTarget, resourcePool: CrossFrameResourcePool) {
if (!isActive) {
Expand All @@ -223,22 +262,31 @@ object SaturationHandler {
}

InternalTargetTracker.updateSize(renderTarget)
SaturationUniforms.upload(ColorSaturationConfig.strength)
SaturationUniforms.upload(
ColorSaturationConfig.strength,
ColorSaturationConfig.contrast,
ColorSaturationConfig.brightness,
ColorSaturationConfig.hue,
)

val tempTarget = InternalTargetTracker.target ?: return
val commandEncoder = RenderSystem.getDevice().createCommandEncoder()

commandEncoder.copyTextureToTexture(
renderTarget.getColorTexture()!!,
tempTarget.getColorTexture()!!,
0, 0, 0, 0, 0, renderTarget.width, renderTarget.height,
)
drawPass("ColorSaturation/Saturation", tempTarget, renderTarget, saturationPipeline, true)
drawPass("ColorSaturation/Blit", renderTarget, tempTarget, blitPipeline, false)
}

private fun drawPass(
label: String,
output: RenderTarget,
input: RenderTarget,
pipeline: RenderPipeline,
hasUniforms: Boolean,
) {
val commandEncoder = RenderSystem.getDevice().createCommandEncoder()
val vertexBuffer = FullscreenTriangle.vertexBuffer

commandEncoder.createRenderPass(
{ "ColorSaturation/Saturation" },
renderTarget.getColorTextureView()!!,
{ label },
output.getColorTextureView()!!,
//? if >=26.2 {
/*Optional.empty()*/
//?}
Expand All @@ -256,14 +304,16 @@ object SaturationHandler {
//? if >=1.21.11 {
/*renderPass.bindTexture(
"DiffuseSampler",
tempTarget.getColorTextureView()!!,
input.getColorTextureView()!!,
RenderSystem.getSamplerCache().getClampToEdge(FilterMode.NEAREST)
)*/
//?}
//? if <1.21.11 {
renderPass.bindSampler("DiffuseSampler", tempTarget.getColorTextureView()!!)
renderPass.bindSampler("DiffuseSampler", input.getColorTextureView()!!)
//?}
renderPass.setUniform("SaturationConfig", SaturationUniforms.buffer)
if (hasUniforms) {
renderPass.setUniform("SaturationConfig", SaturationUniforms.buffer)
}
//? if >=26.2 {
/*renderPass.draw(FullscreenTriangle.VERTEX_COUNT, 1, 0, 0)*/
//?}
Expand Down
Loading