Skip to content

Commit 7dafd4b

Browse files
authored
Merge pull request #554 from EarthyScience/jp/shader-uniforms
Shader Uniforms Config
2 parents 9382338 + 80684b0 commit 7dafd4b

4 files changed

Lines changed: 156 additions & 26 deletions

File tree

src/components/ui/ShaderEditor.tsx

Lines changed: 126 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import {
88
SelectTrigger,
99
SelectValue,
1010
} from '@/components/ui/select';
11+
import './css/ShaderEditor.css'
1112
import { Button } from './button';
1213
import { useAnalysisStore, useGlobalStore } from '@/GlobalStates';
1314
import { IoCloseCircleSharp } from "react-icons/io5";
1415
import { useShallow } from 'zustand/shallow';
1516
import {Hider, Input, Switcher} from '../ui';
1617
import { templates } from './ShaderTemplates';
18+
import { HiMiniWrench } from "react-icons/hi2";
19+
import { Popover, PopoverContent, PopoverTrigger } from '../ui';
20+
import { HandleKernelNums } from '@/utils/HelperFuncs';
1721

1822
const selectedPlates = {
1923
None: " ",
@@ -22,6 +26,95 @@ const selectedPlates = {
2226
Convolution2D: "ConvolutionBoilerPlate2D"
2327
}
2428

29+
const ConfigureUniforms = ({variables} : {variables:string[]})=>{
30+
const {reduceOnAxis, axis, setKernelDepth, setKernelSize, setVariable2, setReduceOnAxis, setAxis} = useAnalysisStore(useShallow(state => ({
31+
reduceOnAxis: state.reduceOnAxis,
32+
axis: state.axis,
33+
variable2: state.variable2,
34+
setKernelDepth: state.setKernelDepth,
35+
setKernelSize: state.setKernelSize,
36+
setVariable2: state.setVariable2,
37+
setReduceOnAxis: state.setReduceOnAxis,
38+
setAxis: state.setAxis,
39+
})))
40+
const {dimNames, variable} = useGlobalStore(useShallow(state => ({dimNames: state.dimNames, variable:state.variable})))
41+
const [thisKernelSize, setThisKernelSize] = useState(String(useAnalysisStore.getState().kernelSize))
42+
const [thisKernelDepth, setThisKernelDepth] = useState(String(useAnalysisStore.getState().kernelDepth))
43+
44+
return(
45+
<Popover>
46+
<PopoverTrigger>
47+
<div className='configure-uniforms'>
48+
<HiMiniWrench size={26}/>
49+
</div>
50+
</PopoverTrigger>
51+
<PopoverContent>
52+
<div className='grid grid-cols-2 gap-2'>
53+
<Switcher leftText='Remain' rightText='Reduce' state={!reduceOnAxis} onClick={()=>setReduceOnAxis(!reduceOnAxis)} className='col-span-2'/>
54+
<div className='col-span-2 flex items-center justify-center'>
55+
<p>Use <code>workgroup_size({reduceOnAxis ? "16, 16, 1" : "4, 4, 4" })</code></p>
56+
</div>
57+
<b>Reduction Axis</b>
58+
<Select onValueChange={e=> setAxis(parseInt(e))}>
59+
<SelectTrigger>
60+
<SelectValue placeholder={dimNames[axis]} />
61+
</SelectTrigger>
62+
<SelectContent>
63+
{dimNames.map((val, idx) =>(
64+
<SelectItem key={idx} value={String(idx)}>
65+
{val}
66+
</SelectItem>
67+
))}
68+
</SelectContent>
69+
</Select>
70+
<div>
71+
kernelSize
72+
<Input type='number' value={thisKernelSize}
73+
onChange={e=> setThisKernelSize(e.target.value)}
74+
onBlur={e=>{
75+
const newVal = HandleKernelNums(e.target.value)
76+
setThisKernelSize(String(newVal))
77+
setKernelSize(newVal)
78+
}}
79+
/>
80+
</div>
81+
<div>
82+
kernelDepth
83+
<Input type='number' value={thisKernelDepth}
84+
onChange={e=> setThisKernelDepth(e.target.value)}
85+
onBlur={e=>{
86+
const newVal = HandleKernelNums(e.target.value)
87+
setThisKernelDepth(String(newVal))
88+
setKernelDepth(newVal)
89+
}}
90+
/>
91+
</div>
92+
<div className='col-span-2 flex flex-col items-center'>
93+
<b>Second Variable</b>
94+
<Select onValueChange={e=> setVariable2(e)}>
95+
<SelectTrigger>
96+
<SelectValue placeholder={"Select variable"} />
97+
</SelectTrigger>
98+
<SelectContent>
99+
<SelectItem key={"empty"} value={"Default"}>
100+
No Variable
101+
</SelectItem>
102+
{variables.map((val, idx) =>(
103+
val != variable &&
104+
<SelectItem key={idx} value={val}>
105+
{val}
106+
</SelectItem>
107+
))}
108+
</SelectContent>
109+
</Select>
110+
</div>
111+
112+
</div>
113+
</PopoverContent>
114+
</Popover>
115+
)
116+
}
117+
25118
export const ShaderEditor = ({visible} : {visible: boolean}) => {
26119
const [shader, setShader] = useState<string | undefined>()
27120
const [showUniforms, setShowUniforms] = useState(false)
@@ -39,10 +132,11 @@ export const ShaderEditor = ({visible} : {visible: boolean}) => {
39132
setKernelSize: state.setKernelSize,
40133
setReduceOnAxis: state.setReduceOnAxis
41134
})))
42-
const {dimNames, dataShape, variable} = useGlobalStore(useShallow(state=>({
135+
const {dimNames, dataShape, variable, variables,} = useGlobalStore(useShallow(state=>({
43136
dimNames: state.dimNames,
44137
dataShape: state.dataShape,
45-
variable: state.variable
138+
variable: state.variable,
139+
variables: state.variables
46140
})))
47141
const [outputShape, setOutPutShape] = useState(dataShape)
48142

@@ -53,12 +147,13 @@ export const ShaderEditor = ({visible} : {visible: boolean}) => {
53147

54148
useEffect(()=>{
55149
if (reduceOnAxis){
56-
const newShape = dataShape.filter((_val,idx) => idx != newDim)
150+
const newShape = dataShape.filter((_val,idx) => idx != axis)
57151
setOutPutShape(newShape)
58152
} else{
59153
setOutPutShape(dataShape)
60154
}
61-
},[reduceOnAxis, newDim, dataShape])
155+
},[reduceOnAxis, axis, dataShape])
156+
62157
return (
63158
<div
64159
className='fixed flex flex-col items-center w-[100%] h-[100%] z-10 left-1/2 -translate-x-1/2'
@@ -89,7 +184,7 @@ export const ShaderEditor = ({visible} : {visible: boolean}) => {
89184
<b>kernelSize</b>
90185
<p>This is the spatial radius of the kernel for convolution operations. It is currently set to <b>{kernelSize}</b></p>
91186
<b>inputData</b>
92-
<p>This is the input array</p>
187+
<p>This is the input array when using one variable</p>
93188
<b>dimLength</b>
94189
<p>This is the length of the dimension being reduced. It is currently <b>{dataShape[axis]}</b></p>
95190
<b>kernelDepth</b>
@@ -102,27 +197,40 @@ export const ShaderEditor = ({visible} : {visible: boolean}) => {
102197
<p>When using two inputs, this is the second array. It is currently set to <b>{variable2}</b></p>
103198
</div>
104199
</Hider>
105-
<div className='mt-8 mb-2 flex gap-4 items-center w-[60%]'>
106-
<div className='flex flex-col items-center self-end'>
200+
<div className='mt-8 mb-2 flex gap-4 items-center w-[60%] h-[50px]'>
201+
<div className='flex flex-col items-center self-end '>
107202
<b>Get started with presets</b>
108203
<Select onValueChange={setBoilerPlate}>
109-
<SelectTrigger style={{ width: '175px', marginLeft: '18px' }}>
110-
<SelectValue placeholder={ "Select Template"} />
111-
</SelectTrigger>
112-
<SelectContent>
113-
{["None", "Reduction", "Convolution3D", "Convolution2D"].map((val,idx)=>(
114-
<SelectItem key={idx} value={selectedPlates[val as keyof typeof selectedPlates]}>
115-
{val}
116-
</SelectItem>
117-
))}
118-
</SelectContent>
119-
</Select>
204+
<SelectTrigger style={{ width: '175px', marginLeft: '18px' }}>
205+
<SelectValue placeholder={ "Select Template"} />
206+
</SelectTrigger>
207+
<SelectContent>
208+
{["None", "Reduction", "Convolution3D", "Convolution2D"].map((val,idx)=>(
209+
<SelectItem key={idx} value={selectedPlates[val as keyof typeof selectedPlates]}>
210+
{val}
211+
</SelectItem>
212+
))}
213+
</SelectContent>
214+
</Select>
120215
</div>
121216
<Button
122217
onClick={()=>setShowUniforms(x=> !x)}
123218
>
124219
{(showUniforms ? 'Hide' : 'Show') + ' Uniforms'}
125220
</Button>
221+
<ConfigureUniforms variables={variables}/>
222+
<div>
223+
Reduction:
224+
<span style={{ color: reduceOnAxis ? "#44ef91" : "#ef4444" }}>
225+
{reduceOnAxis ? " True" : " False"}
226+
</span>
227+
</div>
228+
<div>
229+
Using Two Variables:
230+
<span style={{ color: variable2 != "Default" ? "#44ef91" : "#ef4444" }}>
231+
{variable2 != "Default" ? " True" : " False"}
232+
</span>
233+
</div>
126234
</div>
127235
<div className='w-[60%] h-[70%] relative'>
128236
<IoCloseCircleSharp

src/components/ui/ShaderTemplates.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
133133
}
134134
//Continue Here
135135
136-
let outputIndex = outY * xSize + outX;
137-
138136
// Cast final value to f16 and write to output
139137
let calculatedValue = 0.0;
140-
outputData[outputIndex] = f16(calculatedValue);
138+
outputData[globalIdx] = f16(calculatedValue);
141139
}
142140
`
143141

@@ -196,11 +194,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>,) {
196194
}
197195
//Continue Here
198196
199-
let outputIndex = outY * xSize + outX;
200-
201197
// Cast final value to f16 and write to output
202198
let calculatedValue = 0.0;
203-
outputData[outputIndex] = f16(calculatedValue);
199+
outputData[globalIdx] = f16(calculatedValue);
204200
}
205201
`
206202

src/components/ui/Widgets/Switcher.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react'
22

3-
export const Switcher = ({leftText, rightText, state, onClick} : {leftText: string, rightText: string, state: boolean, onClick: () => void}) => {
3+
export const Switcher = ({leftText, rightText, state, onClick, className} : {leftText: string, rightText: string, state: boolean, onClick: () => void, className?: string}) => {
44

55
return (
66
<div
7-
className='relative w-full text-center h-10 bg-primary rounded-full cursor-pointer mb-2 flex items-center justify-between px-4'
7+
className={`${className} relative w-full text-center h-10 bg-primary rounded-full cursor-pointer mb-2 flex items-center justify-between px-4`}
88
onClick={onClick}
99
>
1010
<span className={`z-10 font-semibold transition-colors ${state ? 'text-primary' : 'text-secondary'}`}>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.configure-uniforms {
2+
background:var(--modal-shadow);
3+
border: 1px solid #d0d0d0;
4+
padding: 10px 20px;
5+
border-radius: 6px;
6+
cursor: pointer;
7+
display: inline-block;
8+
transition:
9+
background 0.2s ease,
10+
box-shadow 0.2s ease,
11+
transform 0.15s ease;
12+
13+
box-shadow: 0 1px 2px rgba(0,0,0,0.08);
14+
}
15+
16+
.configure-uniforms:hover {
17+
background: #e6e6e6;
18+
box-shadow: 0 2px 4px rgba(0,0,0,0.12);
19+
transform: translateY(-1px);
20+
}
21+
22+
.configure-uniforms:active {
23+
background: #dcdcdc;
24+
box-shadow: 0 1px 2px rgba(0,0,0,0.10);
25+
transform: translateY(0);
26+
}

0 commit comments

Comments
 (0)