• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MeltyPlayer / MeltyTool / 28601236297

02 Jul 2026 03:19PM UTC coverage: 42.428% (-0.1%) from 42.542%
28601236297

push

github

MeltyPlayer
Oops, fixed a really stupid bug in CachedFileHierarchy.

7644 of 19951 branches covered (38.31%)

Branch coverage included in aggregate %.

0 of 1 new or added line in 1 file covered. (0.0%)

81 existing lines in 12 files now uncovered.

31816 of 73053 relevant lines covered (43.55%)

62044.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/FinModelUtility/Libraries/Gx/Gx/src/impl/GxFixedFunctionMaterial_ValueManager.cs
1
using System.Drawing;
2

3
using fin.language.equations.fixedFunction;
4
using fin.model;
5
using fin.util.asserts;
6

7
namespace gx;
8

9
public partial class GxFixedFunctionMaterial {
10
  private class ValueManager {
11
    private readonly IColorValue colorUndefined_;
12
    private readonly IScalarValue alphaUndefined_;
13

14
    private readonly IFixedFunctionEquations<FixedFunctionSource> equations_;
15
    private readonly IFixedFunctionRegisters registers_;
16

17
    private readonly Dictionary<GxCc, IColorValue>
102✔
18
        colorValues_ = new();
102✔
19

20
    private readonly Dictionary<GxCa, IScalarValue>
102✔
21
        alphaValues_ = new();
102✔
22

23
    public ValueManager(
102✔
24
        IFixedFunctionEquations<FixedFunctionSource> equations,
102✔
25
        IFixedFunctionRegisters registers) {
204✔
26
      this.equations_ = equations;
102✔
27
      this.registers_ = registers;
102✔
28

29
      var colorZero = equations.CreateColorConstant(0);
102✔
30
      var colorOne = equations.CreateColorConstant(1);
102✔
31

32
      this.colorValues_[GxCc.GX_CC_ZERO] = colorZero;
102✔
33
      this.colorValues_[GxCc.GX_CC_ONE] = colorOne;
102✔
34

35
      this.alphaValues_[GxCa.GX_CA_ZERO] =
102✔
36
          equations.CreateScalarConstant(0);
102✔
37

38
      this.colorUndefined_ =
102✔
39
          equations.CreateOrGetColorInput(FixedFunctionSource.UNDEFINED);
102✔
40
      this.alphaUndefined_ =
102✔
41
          equations.CreateOrGetScalarInput(FixedFunctionSource.UNDEFINED);
102✔
42
    }
102✔
43

44
    public void UpdateColorRegister(
45
        ColorRegister colorRegister,
46
        IColorValue colorValue) {
186✔
47
      var source = colorRegister switch {
186!
48
          ColorRegister.GX_TEVPREV => GxCc.GX_CC_CPREV,
173✔
49
          ColorRegister.GX_TEVREG0 => GxCc.GX_CC_C0,
8✔
UNCOV
50
          ColorRegister.GX_TEVREG1 => GxCc.GX_CC_C1,
×
51
          ColorRegister.GX_TEVREG2 => GxCc.GX_CC_C2,
5✔
52
          _ => throw new ArgumentOutOfRangeException(
×
53
              nameof(colorRegister),
×
54
              colorRegister,
×
55
              null)
×
56
      };
186✔
57

58
      this.colorValues_[source] = colorValue;
186✔
59
    }
186✔
60

61
    public void UpdateAlphaRegister(
62
        ColorRegister alphaRegister,
63
        IScalarValue alphaValue) {
186✔
64
      var source = alphaRegister switch {
186!
65
          ColorRegister.GX_TEVPREV => GxCa.GX_CA_APREV,
186✔
66
          ColorRegister.GX_TEVREG0 => GxCa.GX_CA_A0,
×
67
          ColorRegister.GX_TEVREG1 => GxCa.GX_CA_A1,
×
68
          ColorRegister.GX_TEVREG2 => GxCa.GX_CA_A2,
×
69
          _ => throw new ArgumentOutOfRangeException(
×
70
              nameof(alphaRegister),
×
71
              alphaRegister,
×
72
              null)
×
73
      };
186✔
74

75
      this.alphaValues_[source] = alphaValue;
186✔
76
    }
186✔
77

78
    private readonly IColorValue?[] textureColors_ = new IColorValue?[8];
102✔
79
    private readonly IColorValue?[] textureAlphas_ = new IColorValue?[8];
102✔
80
    private int? textureIndex_ = null;
102✔
81

82
    public void UpdateTextureIndex(int? index) {
186✔
83
      this.textureIndex_ = index;
186✔
84

85
      if (index != null) {
351✔
86
        Asserts.True(index >= 0 && index < 8);
165!
87
      }
165✔
88
    }
186✔
89

90
    private IColorValue GetTextureColorChannel_() {
153✔
91
      var indexOrNull = this.textureIndex_;
153✔
92

93
      IColorValue texture;
94
      if (indexOrNull == null && !STRICT) {
153!
95
        texture = this.colorUndefined_;
×
96
      } else {
153✔
97
        Asserts.Nonnull(indexOrNull);
153✔
98

99
        var index = indexOrNull.Value;
153✔
100
        Asserts.True(index >= 0 && index < 8);
153!
101

102
        texture = this.textureColors_[index];
153✔
103
        if (texture == null) {
304✔
104
          this.textureColors_[index] =
151✔
105
              texture = this.equations_.CreateOrGetColorInput(
151✔
106
                  FixedFunctionSource.TEXTURE_COLOR_0 + index);
151✔
107
        }
151✔
108
      }
153✔
109

110
      return texture;
153✔
111
    }
153✔
112

113
    private IColorValue GetTextureAlphaChannel_() {
6✔
114
      var indexOrNull = this.textureIndex_;
6✔
115

116
      IColorValue texture;
117
      if (indexOrNull == null && !STRICT) {
6!
118
        texture = this.colorUndefined_;
×
119
      } else {
6✔
120
        Asserts.Nonnull(indexOrNull);
6✔
121

122
        var index = indexOrNull.Value;
6✔
123
        Asserts.True(index >= 0 && index < 8);
6!
124

125
        texture = this.textureAlphas_[index];
6✔
126
        if (texture == null) {
12✔
127
          this.textureAlphas_[index] =
6✔
128
              texture = this.equations_.CreateOrGetColorInput(
6✔
129
                  FixedFunctionSource.TEXTURE_ALPHA_0 + index);
6✔
130
        }
6✔
131
      }
6✔
132

133
      return texture;
6✔
134
    }
6✔
135

136
    private IScalarValue GetTextureAlphaChannelAsAlpha_() {
95✔
137
      var indexOrNull = this.textureIndex_;
95✔
138

139
      IScalarValue texture;
140
      if (indexOrNull == null && !STRICT) {
95!
141
        texture = this.alphaUndefined_;
×
142
      } else {
95✔
143
        Asserts.Nonnull(indexOrNull);
95✔
144

145
        var index = indexOrNull.Value;
95✔
146
        Asserts.True(index >= 0 && index < 8);
95!
147

148
        texture =
95✔
149
            this.equations_.CreateOrGetScalarInput(
95✔
150
                FixedFunctionSource.TEXTURE_ALPHA_0 + index);
95✔
151
      }
95✔
152

153
      return texture;
95✔
154
    }
95✔
155

156

157
    private GxColorChannel? colorChannel_;
158

159
    public void UpdateRascChannel(GxColorChannel? colorChannel) {
186✔
160
      this.colorChannel_ = colorChannel;
186✔
161
    }
186✔
162

163
    private readonly Dictionary<GxColorChannel, IColorValue>
102✔
164
        colorChannelColorColors_
102✔
165
            = new([
102✔
166
                new KeyValuePair<GxColorChannel, IColorValue>(
102✔
167
                    GxColorChannel.GX_COLOR_NULL,
102✔
168
                    ColorConstant.ZERO)
102✔
169
            ]);
102✔
170

171
    private readonly Dictionary<GxColorChannel, IColorValue>
102✔
172
        colorChannelColorAlphas_ = new([
102✔
173
            new KeyValuePair<GxColorChannel, IColorValue>(
102✔
174
                GxColorChannel.GX_COLOR_NULL,
102✔
175
                ColorConstant.ZERO)
102✔
176
        ]);
102✔
177

178
    private readonly Dictionary<GxColorChannel, IScalarValue>
102✔
179
        colorChannelAlphas_ = new([
102✔
180
            new KeyValuePair<GxColorChannel, IScalarValue>(
102✔
181
                GxColorChannel.GX_COLOR_NULL,
102✔
182
                ScalarConstant.ZERO)
102✔
183
        ]);
102✔
184

185
    public void UpdateColorChannelColor(
186
        GxColorChannel colorChannel,
187
        IColorValue colorValue) {
408✔
188
      this.colorChannelColorColors_[colorChannel] = colorValue;
408✔
189
    }
408✔
190

191
    public void UpdateColorChannelAlpha(
192
        GxColorChannel colorChannel,
193
        IScalarValue alphaValue) {
408✔
194
      this.colorChannelAlphas_[colorChannel] = alphaValue;
408✔
195
      this.colorChannelColorAlphas_[colorChannel] =
408✔
196
          this.equations_.CreateColor(alphaValue);
408✔
197
    }
408✔
198

199
    private IColorValue GetVertexColorChannel_(GxCc colorSource) {
122✔
200
      var channelOrNull = this.colorChannel_;
122✔
201
      Asserts.Nonnull(channelOrNull);
122✔
202

203
      var channel = channelOrNull.Value;
122✔
204
      var color = colorSource switch {
122!
205
          GxCc.GX_CC_RASC => this.colorChannelColorColors_[channel],
115✔
206
          GxCc.GX_CC_RASA => this.colorChannelColorAlphas_[channel],
7✔
207
          _               => throw new NotImplementedException()
×
208
      };
122✔
209

210
      return color;
122✔
211
    }
122✔
212

213
    // TODO: Switch from vertex alpha to ambient/diffuse lights when applicable
214
    private IScalarValue GetVertexAlphaChannel_() {
103✔
215
      var channelOrNull = this.colorChannel_;
103✔
216
      Asserts.Nonnull(channelOrNull);
103✔
217

218
      var channel = channelOrNull.Value;
103✔
219
      var alpha = this.colorChannelAlphas_[channel];
103✔
220

221
      return alpha;
103✔
222
    }
103✔
223

224
    private IList<IColorRegister> colorRegisterColors_;
225
    private IList<Color> konstColorImpls_;
226

227
    public void SetColorRegisters(IList<IColorRegister> colorRegisterColors) {
102✔
228
      this.colorRegisterColors_ = colorRegisterColors;
102✔
229
    }
102✔
230

231
    public void SetKonstColors(IList<Color> konstColors) {
102✔
232
      this.konstColorImpls_ = konstColors;
102✔
233
    }
102✔
234

235
    private GxKonstColorSel tevStageColorConstantSel_;
236
    private GxKonstAlphaSel tevStageAlphaConstantSel_;
237

238
    public void UpdateKonst(GxKonstColorSel tevStageColorConstantSel,
239
                            GxKonstAlphaSel tevStageAlphaConstantSel) {
186✔
240
      this.tevStageColorConstantSel_ = tevStageColorConstantSel;
186✔
241
      this.tevStageAlphaConstantSel_ = tevStageAlphaConstantSel;
186✔
242
    }
186✔
243

244
    public bool TryGetEnumIndex_<T>(T value, T min, T max, out int index)
245
        where T : IComparable, IConvertible {
121✔
246
      var minCompare = value.CompareTo(min);
121✔
247
      var maxCompare = value.CompareTo(max);
121✔
248

249
      if (minCompare >= 0 && maxCompare <= 0) {
174!
250
        index = value.ToInt32(null) - min.ToInt32(null);
53✔
251
        return true;
53✔
252
      }
253

254
      index = -1;
68✔
255
      return false;
68✔
256
    }
121✔
257

258
    // https://github.com/magcius/bmdview/blob/master/tev.markdown#gx_settevkcolorsel
259
    public IColorValue GetKonstColor_(GxKonstColorSel sel) {
24✔
260
      if (this.TryGetEnumIndex_(sel,
24✔
261
                                GxKonstColorSel.KCSel_1,
24✔
262
                                GxKonstColorSel.KCSel_1_8,
24✔
263
                                out var fracIndex)) {
41✔
264
        var numerator = 8 - fracIndex;
17✔
265
        var intensity = numerator / 8f;
17✔
266
        return this.equations_.CreateColorConstant(intensity);
17✔
267
      }
268

269
      if (this.TryGetEnumIndex_(sel,
7!
270
                                GxKonstColorSel.KCSel_K0,
7✔
271
                                GxKonstColorSel.KCSel_K3,
7✔
272
                                out var rgbIndex)) {
14✔
273
        var konstRgb = this.konstColorImpls_[rgbIndex];
7✔
274
        return this.equations_.CreateColorConstant(
7✔
275
            konstRgb.R / 255f,
7✔
276
            konstRgb.G / 255f,
7✔
277
            konstRgb.B / 255f);
7✔
278
      }
279

UNCOV
280
      if (this.TryGetEnumIndex_(sel,
×
UNCOV
281
                                GxKonstColorSel.KCSel_K0_R,
×
UNCOV
282
                                GxKonstColorSel.KCSel_K3_R,
×
UNCOV
283
                                out var rIndex)) {
×
284
        var konstR = this.konstColorImpls_[rIndex];
×
285
        return this.equations_.CreateColorConstant(konstR.R / 255f);
×
286
      }
287

UNCOV
288
      if (this.TryGetEnumIndex_(sel,
×
UNCOV
289
                                GxKonstColorSel.KCSel_K0_G,
×
UNCOV
290
                                GxKonstColorSel.KCSel_K3_G,
×
UNCOV
291
                                out var gIndex)) {
×
292
        var konstG = this.konstColorImpls_[gIndex];
×
293
        return this.equations_.CreateColorConstant(konstG.G / 255f);
×
294
      }
295

UNCOV
296
      if (this.TryGetEnumIndex_(sel,
×
UNCOV
297
                                GxKonstColorSel.KCSel_K0_B,
×
UNCOV
298
                                GxKonstColorSel.KCSel_K3_B,
×
UNCOV
299
                                out var bIndex)) {
×
300
        var konstB = this.konstColorImpls_[bIndex];
×
301
        return this.equations_.CreateColorConstant(konstB.B / 255f);
×
302
      }
303

UNCOV
304
      if (this.TryGetEnumIndex_(sel,
×
UNCOV
305
                                GxKonstColorSel.KCSel_K0_A,
×
UNCOV
306
                                GxKonstColorSel.KCSel_K3_A,
×
UNCOV
307
                                out var aIndex)) {
×
UNCOV
308
        var konstA = this.konstColorImpls_[aIndex];
×
UNCOV
309
        return this.equations_.CreateColorConstant(konstA.A / 255f);
×
310
      }
311

312
      throw new NotImplementedException();
×
313
    }
24✔
314

315
    public IScalarValue GetKonstAlpha_(GxKonstAlphaSel sel) {
19✔
316
      if (this.TryGetEnumIndex_(sel,
19✔
317
                                GxKonstAlphaSel.KASel_1,
19✔
318
                                GxKonstAlphaSel.KASel_1_8,
19✔
319
                                out var fracIndex)) {
22✔
320
        var numerator = 8 - fracIndex;
3✔
321
        var intensity = numerator / 8f;
3✔
322
        return this.equations_.CreateScalarConstant(intensity);
3✔
323
      }
324

325
      if (this.TryGetEnumIndex_(sel,
16✔
326
                                GxKonstAlphaSel.KASel_K0_R,
16✔
327
                                GxKonstAlphaSel.KASel_K3_R,
16✔
328
                                out var rIndex)) {
17✔
329
        var konstR = this.konstColorImpls_[rIndex];
1✔
330
        return this.equations_.CreateScalarConstant(konstR.R / 255f);
1✔
331
      }
332

333
      if (this.TryGetEnumIndex_(sel,
15!
334
                                GxKonstAlphaSel.KASel_K0_G,
15✔
335
                                GxKonstAlphaSel.KASel_K3_G,
15✔
336
                                out var gIndex)) {
15✔
337
        var konstG = this.konstColorImpls_[gIndex];
×
338
        return this.equations_.CreateScalarConstant(konstG.G / 255f);
×
339
      }
340

341
      if (this.TryGetEnumIndex_(sel,
15!
342
                                GxKonstAlphaSel.KASel_K0_B,
15✔
343
                                GxKonstAlphaSel.KASel_K3_B,
15✔
344
                                out var bIndex)) {
15✔
345
        var konstB = this.konstColorImpls_[bIndex];
×
346
        return this.equations_.CreateScalarConstant(konstB.B / 255f);
×
347
      }
348

349
      if (this.TryGetEnumIndex_(sel,
15!
350
                                GxKonstAlphaSel.KASel_K0_A,
15✔
351
                                GxKonstAlphaSel.KASel_K3_A,
15✔
352
                                out var aIndex)) {
30✔
353
        var konstA = this.konstColorImpls_[aIndex];
15✔
354
        return this.equations_.CreateScalarConstant(
15✔
355
            konstA.A / 255f);
15✔
356
      }
357

358
      throw new NotImplementedException();
×
359
    }
19✔
360

361
    public IColorValue GetColor(GxCc colorSource) {
846✔
362
      if (this.colorValues_.TryGetValue(colorSource, out var colorValue)) {
1,370✔
363
        return colorValue;
524✔
364
      }
365

366
      if (colorSource == GxCc.GX_CC_TEXC) {
475✔
367
        return this.GetTextureColorChannel_();
153✔
368
      }
369

370
      if (colorSource == GxCc.GX_CC_TEXA) {
175✔
371
        return this.GetTextureAlphaChannel_();
6✔
372
      }
373

374
      if (colorSource == GxCc.GX_CC_RASC ||
163✔
375
          colorSource == GxCc.GX_CC_RASA) {
285✔
376
        return this.GetVertexColorChannel_(colorSource);
122✔
377
      }
378

379
      if (colorSource == GxCc.GX_CC_KONST) {
65✔
380
        return this.GetKonstColor_(this.tevStageColorConstantSel_);
24✔
381
      }
382

383
      if (colorSource >= GxCc.GX_CC_C0 &&
17!
384
          colorSource <= GxCc.GX_CC_A2) {
34✔
385
        var (colorRegister, isColor) =
17✔
386
            this.GetColorRegisterColorForSource_(colorSource);
17✔
387
        Asserts.Nonnull(colorRegister);
17✔
388
        var color = colorRegister.Color;
17✔
389
        var index = colorRegister.Index;
17✔
390

391
        if (isColor) {
32✔
392
          return this.registers_.GetOrCreateColorRegister(
15✔
393
              $"GxColorRegister{index}",
15✔
394
              this.equations_.CreateColorConstant(
15✔
395
                  color.R / 255f,
15✔
396
                  color.G / 255f,
15✔
397
                  color.B / 255f));
15✔
398
        }
399

400
        return new ColorWrapper(
2✔
401
            this.registers_.GetOrCreateScalarRegister(
2✔
402
                $"GxAlphaRegister{index}",
2✔
403
                this.equations_.CreateScalarConstant(
2✔
404
                    color.A / 255f)));
2✔
405
      }
406

407
      if (!STRICT) {
×
408
        return this.colorUndefined_;
×
409
      }
410

411
      throw new NotImplementedException();
412
    }
846✔
413

414
    public IScalarValue GetAlpha(GxCa alphaSource) {
846✔
415
      if (this.alphaValues_.TryGetValue(alphaSource, out var alphaValue)) {
1,465✔
416
        return alphaValue;
619✔
417
      }
418

419
      if (alphaSource == GxCa.GX_CA_TEXA) {
322✔
420
        return this.GetTextureAlphaChannelAsAlpha_();
95✔
421
      }
422

423
      if (alphaSource == GxCa.GX_CA_RASA) {
235✔
424
        return this.GetVertexAlphaChannel_();
103✔
425
      }
426

427
      if (alphaSource == GxCa.GX_CA_KONST) {
48✔
428
        return this.GetKonstAlpha_(this.tevStageAlphaConstantSel_);
19✔
429
      }
430

431
      if (this.TryGetEnumIndex_(
10!
432
              alphaSource,
10✔
433
              GxCa.GX_CA_A0,
10✔
434
              GxCa.GX_CA_A2,
10✔
435
              out var caIndex)) {
20✔
436
        var colorRegister = this.GetColorRegisterColorForIndex_(caIndex);
10✔
437
        Asserts.Nonnull(colorRegister);
10✔
438
        var color = colorRegister.Color;
10✔
439
        var index = colorRegister.Index;
10✔
440

441
        return this.registers_.GetOrCreateScalarRegister(
10✔
442
            $"GxAlphaRegister{index}",
10✔
443
            this.equations_.CreateScalarConstant(
10✔
444
                color.A / 255f));
10✔
445
      }
446

447
      if (!STRICT) {
×
448
        return this.alphaUndefined_;
×
449
      }
450

451
      throw new NotImplementedException();
452
    }
846✔
453

454
    private (IColorRegister? colorRegister, bool isAlpha)
455
        GetColorRegisterColorForSource_(
456
            GxCc source) {
17✔
457
      var ccIndex = (int) source - (int) GxCc.GX_CC_C0;
17✔
458

459
      var isColor = ccIndex % 2 == 0;
17✔
460
      var index = isColor ? ccIndex / 2 : (ccIndex - 1) / 2;
17✔
461

462
      return (this.GetColorRegisterColorForIndex_(index), isColor);
17✔
463
    }
17✔
464

465
    private IColorRegister? GetColorRegisterColorForIndex_(int index) {
27✔
466
      if (this.colorRegisterColors_.Count > index) {
54!
467
        return this.colorRegisterColors_[index];
27✔
468
      }
469

470
      return null;
×
471
    }
27✔
472
  }
473
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc