Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the --smoothstep flag #391

Merged
merged 2 commits into from
May 12, 2024
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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Shader Minifier

[![Build status](https://ci.appveyor.com/api/projects/status/chwlpnssgd5kdl4x/branch/master?svg=true)](https://ci.appveyor.com/project/laurentlb/shader-minifier/branch/master)
[![Build status](https://ci.appveyor.com/api/projects/status/9sekrxuiway58su3?svg=true)](https://ci.appveyor.com/project/laurentlb/shader-minifier-r5gmt)

Shader Minifier is a tool that minifies and obfuscates shader code (GLSL and
HLSL) without affecting its behaviour. It is also suitable for reducing the size
Expand Down Expand Up @@ -100,7 +100,7 @@ USAGE: Shader Minifier [--help] [-o <string>] [-v] [--hlsl]
[--preserve-all-globals] [--no-inlining]
[--aggressive-inlining] [--no-renaming]
[--no-renaming-list <string>] [--no-sequence]
[--smoothstep] [--no-remove-unused]
[--no-remove-unused]
[--move-declarations] [<filename>...]

FILENAMES:
Expand Down Expand Up @@ -134,7 +134,6 @@ OPTIONS:
--no-renaming-list <string>
Comma-separated list of functions to preserve
--no-sequence Do not use the comma operator trick
--smoothstep Use IQ's smoothstep trick
--no-remove-unused Do not remove unused code
--move-declarations Move declarations to group them
--preprocess Evaluate some of the file preprocessor directives
Expand Down Expand Up @@ -318,7 +317,6 @@ them all at the same time by listing them all on the command-line.
- [Unused local variables](TRANSFORMATIONS.md#Unused-local-variables)
- [Dead code removal](TRANSFORMATIONS.md#Dead-code-removal)
- [Unused function removal](TRANSFORMATIONS.md#Unused-function-removal)
- [Smoothstep transformation](TRANSFORMATIONS.md#Smoothstep-transformation)
- [Renaming](TRANSFORMATIONS.md#Renaming)


Expand Down
11 changes: 0 additions & 11 deletions TRANSFORMATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,6 @@ default, this is the case for `main` and `mainImage`.
**Note**: Use `--no-remove-unused` to disable this transformation.


## Smoothstep transformation

`smoothstep(a,b,x)` calls can be replaced with
`smoothstep(0.0,1.0,(x-a)/(b-a))`. When `a` and `b` are constant, the expression
will be simplified. In some cases, this might make the code more compressible,
and this technique was [used in
Elevated](https://www.pouet.net/topic.php?which=6751&page=1#c295695).

However, in many cases this trick doesn't give good results. As a result, this
is not enabled by default; use `--smoothstep` if you want to try it.

## Renaming

There are two renaming strategies:
Expand Down
4 changes: 0 additions & 4 deletions src/options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type CliArguments =
| [<CustomCommandLine("--no-renaming")>] NoRenaming
| [<CustomCommandLine("--no-renaming-list")>] NoRenamingList of string
| [<CustomCommandLine("--no-sequence")>] NoSequence
| [<CustomCommandLine("--smoothstep")>] Smoothstep
| [<CustomCommandLine("--no-remove-unused")>] NoRemoveUnused
| [<CustomCommandLine("--move-declarations")>] MoveDeclarations
| [<CustomCommandLine("--preprocess")>] Preprocess
Expand All @@ -58,7 +57,6 @@ type CliArguments =
| NoRenaming -> "Do not rename anything"
| NoRenamingList _ -> "Comma-separated list of functions to preserve"
| NoSequence -> "Do not use the comma operator trick"
| Smoothstep -> "Use IQ's smoothstep trick"
| NoRemoveUnused -> "Do not remove unused code"
| MoveDeclarations -> "Move declarations to group them"
| Preprocess -> "Evaluate some of the file preprocessor directives"
Expand All @@ -70,7 +68,6 @@ type Options() =
member val outputFormat = CVariables with get, set
member val verbose = false with get, set
member val debug = false with get, set
member val smoothstepTrick = false with get, set
member val canonicalFieldNames = "xyzw" with get, set
member val preserveExternals = false with get, set
member val preserveAllGlobals = false with get, set
Expand Down Expand Up @@ -119,7 +116,6 @@ let private initPrivate argv needFiles =
options.outputFormat <- args.GetResult(FormatArg, defaultValue = CVariables)
options.verbose <- args.Contains(Verbose)
options.debug <- args.Contains(Debug)
options.smoothstepTrick <- args.Contains(Smoothstep)
options.canonicalFieldNames <- (sprintf "%A" (args.GetResult(FieldNames, defaultValue = XYZW))).ToLower()
options.preserveExternals <- args.Contains(PreserveExternals) || args.Contains(PreserveAllGlobals)
options.preserveAllGlobals <- args.Contains(PreserveAllGlobals)
Expand Down
12 changes: 2 additions & 10 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ module private RewriterImpl =

// Expression that is equivalent to an assignment.
let (|Assignment|_|) = function
| (FunCall (Op "=", [Var v; e])) -> Some (v, e)
| (FunCall (Op op, [Var name; e])) when Builtin.assignOps.Contains op ->
| FunCall (Op "=", [Var v; e]) -> Some (v, e)
| FunCall (Op op, [Var name; e]) when Builtin.assignOps.Contains op ->
let baseOp = op.TrimEnd('=')
if not (Builtin.augmentableOperators.Contains baseOp)
then None
Expand Down Expand Up @@ -336,14 +336,6 @@ module private RewriterImpl =
| FunCall(Var constr, args) when constr.Name = "vec2" || constr.Name = "vec3" || constr.Name = "vec4" ->
simplifyVec constr args

// iq's smoothstep trick: http://www.pouet.net/topic.php?which=6751&page=1#c295695
| FunCall(Var var, [Float (0.M,_); Float (1.M,_); _]) as e when var.Name = "smoothstep" -> e
| FunCall(Var var, [a; b; x]) when var.Name = "smoothstep" && options.smoothstepTrick ->
let sub1 = FunCall(Op "-", [x; a])
let sub2 = FunCall(Op "-", [b; a])
let div = FunCall(Op "/", [sub1; sub2]) |> mapExpr env
FunCall(Var (Ident "smoothstep"), [Float (0.M,""); Float (1.M,""); div])

| Dot(e, field) when options.canonicalFieldNames <> "" -> Dot(e, renameField field)

| Var s as e ->
Expand Down