News: 11 March 2016 - Forum Rules

Author Topic: SNES PPU Question  (Read 3225 times)

Squall_FF8

  • Full Member
  • ***
  • Posts: 206
    • View Profile
SNES PPU Question
« on: May 30, 2019, 08:52:00 am »
This might be a little long, but please bear with me to understand the settings.
I'm trying to render a FF5 scene - Map with an ID 63 - North Mountain Fifth Station. Here is part of the map that I try to reconstruct:

The scene is in Mode 1 with 3 layers (BGs):
- Layer 1 (BG1) - the green summit
- Layer 2 (BG2) - the background
- Layer 3 (BG3) - the white clouds
*Layer 3 is in front of all (PPU register $2105)

In order to compose that scene, the games uses 3 PPU registers:
$212C Main Screen: L1 & L2
$212D Sub Screen: L1 & L3
$2131 Colo Math: affects L1 & L2. The math itself is half of addition
* for now we don't care about the other layers - Sprites

According to that video: https://www.youtube.com/watch?v=zcoU6-9_fDM
If Main render to a pixel from L1 we can have the following output:
- L1 (math) L1 => (L1 + L1) : 2 = L1
- L1 (math) L3 => (L1 + L3) : 2
If Main render to a pixel from L2:
- L2 math L1 => (L2 + L1) : 2
- L2 math L3 => (L2 + L3) : 2

Let me paraphrase above 4 cases:
- L1 -> result is L1 unmodified (opaque) - OK
- (L1 + L3):2 -> result is 50% transparent L3 over L1 (a 50% transparent cloud over a summit) - OK
- (L2 + L3):2 -> result is 50% L3 over L2 (a 50% transparent cloud over the background) - OK
- (L2 + L1):2 -> result is 50% L1 over L2 (the summit 50% transparent over the background) - PROBLEM!!!

The last combination never occur on the picture. Actually what we see on the picture are cases where L2 is unmodified (opaque).
My question is WHY? What went wrong? Do I miss something?
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: SNES PPU Question
« Reply #1 on: May 30, 2019, 11:12:53 am »
It's been a while, but IIRC this is how it works:

If a MAIN screen pixel is marked to participate in color math, the matching SUB screen pixel will get added into it.

Ignoring layer 3 for now, your problem is where layer 1 and 2 overlap.  When that happens: main screen will show layer 2 (higher priority), subscreen will show layer 1 (layer 2 isn't rendered to subscreen).

Since layer 2 (on the main screen) participates in color math, this means you'll add layer 1's subscreen pixel to layer 2's main screen pixel, resulting in the blend.

To solve this, you can just put layer 2 on the subscreen as well. This way main and sub screens will be identical (half color math will have no effect) EXCEPT for when layer 3 is visible on the subscreen, in which case it will be appropriately blended.

Squall_FF8

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: SNES PPU Question
« Reply #2 on: May 30, 2019, 12:49:19 pm »
Thank you Disch!

As usual it is a pleasure to hear from you!

Your words confirm my understanding about how PPU Color Math works. I'm writting an Windows application that tries to render FF5 maps as accurate as possible (no windowing so far). Unfortunatly that case is against what the game shows and what the logic dictate.

BTW thank you for the advice for how to achive desired effect. Unfortunatle I have to read that values from the ROM not implying my own.
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: SNES PPU Question
« Reply #3 on: May 30, 2019, 02:40:48 pm »
Wait... so the game has those settings and is displaying properly?


EDIT:  Aha!  I think I mixed up BG1 and BG2 before.  I was thinking BG1 was the background.  If BG2 is the background... that changes things.

When BG1 and BG2 overlap:

mainscreen = BG1  (BG1 has higher priority)
subscreen = BG1  (ditto, also BG2 not rendered to subscreen)

So color math will have no effect, since it'll add BG1+BG1



Now the weirdness happens when BG1 and BG2 don't overlap, and only BG2 is visible:

mainscreen = BG2
subscreen = backdrop color (palette entry 0)

So the color math will and up blending the background layer with the backdrop color.  Which would probably have some kind of "dulling" effect on the background.
« Last Edit: May 30, 2019, 02:51:26 pm by Disch »

Squall_FF8

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: SNES PPU Question
« Reply #4 on: May 30, 2019, 04:10:14 pm »
When BG1 and BG2 overlap:

mainscreen = BG1  (BG1 has higher priority)
That is it! So the problematic situation although possible - will never occur!

Thank you very much Disch!
Welcome to the FF5 Den: https://discord.gg/AUqDF85

Bregalad

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: SNES PPU Question
« Reply #5 on: May 31, 2019, 07:45:57 am »
So the color math will and up blending the background layer with the backdrop color.  Which would probably have some kind of "dulling" effect on the background.
It's been a while since I've been dealing with this, but wasn't there an exeption with the backdrop color where it was not halved when participating in color math ? In that case it's fine, the BG1 adds with backdrop color (which is pitch black so this has no effect), and then it doesn't halve because it's the backdrop color.

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: SNES PPU Question
« Reply #6 on: May 31, 2019, 08:00:44 pm »
but wasn't there an exeption with the backdrop color where it was not halved when participating in color math ?

Ah!  Maybe!  That sounds right.  I forgot about that completely.  Nice catch.

Squall_FF8

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: SNES PPU Question
« Reply #7 on: June 03, 2019, 08:29:13 am »
Thank you Bregalad for the input!

Yes it seems that is the case and it work for the backdrop exactly as you described. Here is the extra description on $2131 I found recently:
Quote
   d6   "1/2 of color data" designation
         when the color constant addition/subtraction or the
         screen addition/subtraction is performed, designate
         whether the rgb result in the addition/subtraction
         area should be "1/2" or not. however, in the back
         color constant area on the sub screen, it does not
         become "1/2"
Welcome to the FF5 Den: https://discord.gg/AUqDF85