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

MeltyPlayer / MeltyTool / 17574188774

09 Sep 2025 06:45AM UTC coverage: 39.98% (-2.3%) from 42.309%
17574188774

push

github

MeltyPlayer
Okay, hopefully fixed the models now???

5774 of 16361 branches covered (35.29%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

935 existing lines in 47 files now uncovered.

24391 of 59090 relevant lines covered (41.28%)

74782.62 hits per line

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

86.1
/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>
244✔
18
        colorValues_ = new();
244✔
19

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

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

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

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

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

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

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

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

61
    public void UpdateAlphaRegister(
62
        ColorRegister alphaRegister,
63
        IScalarValue alphaValue) {
399✔
64
      var source = alphaRegister switch {
399!
65
          ColorRegister.GX_TEVPREV => GxCa.GX_CA_APREV,
399✔
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
      };
399✔
74

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

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

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

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

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

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

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

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

110
      return texture;
328✔
111
    }
328✔
112

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

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

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

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

133
      return texture;
7✔
134
    }
7✔
135

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

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

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

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

153
      return texture;
239✔
154
    }
239✔
155

156

157
    private GxColorChannel? colorChannel_;
158

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

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

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

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

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

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

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

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

210
      return color;
281✔
211
    }
281✔
212

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

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

221
      return alpha;
284✔
222
    }
284✔
223

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

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

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

235
    private GxKonstColorSel tevStageColorConstantSel_;
236
    private GxKonstAlphaSel tevStageAlphaConstantSel_;
237

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

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

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

254
      index = -1;
148✔
255
      return false;
148✔
256
    }
221✔
257

258
    // https://github.com/magcius/bmdview/blob/master/tev.markdown#gx_settevkcolorsel
259
    public IColorValue GetKonstColor_(GxKonstColorSel sel) {
32✔
260
      if (this.TryGetEnumIndex_(sel,
32✔
261
                                GxKonstColorSel.KCSel_1,
32✔
262
                                GxKonstColorSel.KCSel_1_8,
32✔
263
                                out var fracIndex)) {
49✔
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,
15✔
270
                                GxKonstColorSel.KCSel_K0,
15✔
271
                                GxKonstColorSel.KCSel_K3,
15✔
272
                                out var rgbIndex)) {
22✔
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

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

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

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

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

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

315
    public IScalarValue GetKonstAlpha_(GxKonstAlphaSel sel) {
29✔
316
      if (this.TryGetEnumIndex_(sel,
29✔
317
                                GxKonstAlphaSel.KASel_1,
29✔
318
                                GxKonstAlphaSel.KASel_1_8,
29✔
319
                                out var fracIndex)) {
32✔
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,
26✔
326
                                GxKonstAlphaSel.KASel_K0_R,
26✔
327
                                GxKonstAlphaSel.KASel_K3_R,
26✔
328
                                out var rIndex)) {
27✔
329
        var konstR = this.konstColorImpls_[rIndex];
1✔
330
        return this.equations_.CreateScalarConstant(konstR.R / 255f);
1✔
331
      }
332

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

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

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

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

361
    public IColorValue GetColor(GxCc colorSource) {
1,840✔
362
      if (this.colorValues_.TryGetValue(colorSource, out var colorValue)) {
2,997✔
363
        return colorValue;
1,157✔
364
      }
365

366
      if (colorSource == GxCc.GX_CC_TEXC) {
1,011✔
367
        return this.GetTextureColorChannel_();
328✔
368
      }
369

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

374
      if (colorSource == GxCc.GX_CC_RASC ||
348✔
375
          colorSource == GxCc.GX_CC_RASA) {
629✔
376
        return this.GetVertexColorChannel_(colorSource);
281✔
377
      }
378

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

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

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

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

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

411
      throw new NotImplementedException();
412
    }
1,840✔
413

414
    public IScalarValue GetAlpha(GxCa alphaSource) {
1,840✔
415
      if (this.alphaValues_.TryGetValue(alphaSource, out var alphaValue)) {
3,116✔
416
        return alphaValue;
1,276✔
417
      }
418

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

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

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

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

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

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

451
      throw new NotImplementedException();
452
    }
1,840✔
453

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

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

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

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

UNCOV
470
      return null;
×
471
    }
47✔
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