# JUCE 8 Direct2D + Melatonin Blur issue

**URL:** <https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758>\
**Category:** General JUCE discussion\
**Created:** [June 8, 2024, 5:06pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758 "2024-06-08T17:06:08Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 8, 2024, 5:06pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/1 "2024-06-08T17:06:08Z")

</div>

I haven’t look into the details yet, but it looks like [my blur module](https://github.com/sudara/melatonin_blur)’s shadows aren’t happy on JUCE 8 Direct2D. It looks like maybe when compositing from single color to rgba, it’s painting the color at full opacity vs. using the opacity values in the single color image…

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/5/2/521af0fde8126abd89491300832a32d0ba584aee.png)

I’ll take a look at see if i can get a simpler reproduction…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 8, 2024, 5:11pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/2 "2024-06-08T17:11:46Z")

</div>

I’m also seeing these two errors when trying to [configure JUCE 8 on GitHub Actions](https://github.com/sudara/melatonin_blur/actions/runs/9430148734/job/25977349747) (trying to get the blur tests to run there)

```auto
 D:\a\melatonin_blur\melatonin_blur\Builds\_deps\juce-src\modules\juce_gui_basics\native\juce_Windowing_windows.cpp(2493,31):
  error C2065: 'DWMWCP_DONOTROUND': undeclared identifier
  [D:\a\melatonin_blur\melatonin_blur\Builds\_deps\juce-build\tools\extras\Build\juceaide\juceaide.vcxproj]

  D:\a\melatonin_blur\melatonin_blur\Builds\_deps\juce-src\modules\juce_gui_basics\native\juce_Windowing_windows.cpp(2494,42):
  error C2065: 'DWMWA_WINDOW_CORNER_PREFERENCE': undeclared identifier
  [D:\a\melatonin_blur\melatonin_blur\Builds\_deps\juce-build\tools\extras\Build\juceaide\juceaide.vcxproj]

```

I’ve tried to use the latest Windows 11 sdk available, which is 10.0.22621.0 but it is either not taking or not helping…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 8, 2024, 5:49pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/3 "2024-06-08T17:49:13Z")

</div>

OK, here’s something strange. All of my tests are failing locally, it seems like part of the reason is that `Image#getPixelAt` is always returning transparent black. For example, this fails:

```auto
    juce::Image result (juce::Image::ARGB, 9, 9, true);
    juce::Graphics g (result);
    g.fillAll (juce::Colours::white);
    jassert (result.getPixelAt (2, 2).toDisplayString (true) == "FFFFFFFF");

```

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [June 8, 2024, 6:54pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/4 "2024-06-08T18:54:48Z")

</div>

The behavior of the Graphics object for the D2D and OpenGL renderers is that the image is not completely written/flushed intil the Graphics object goes out of scope. The example you provided should work if you add a scope around the Graphics object. If that’s still broken, please let us know.

```auto
juce::Image result (juce::Image::ARGB, 9, 9, true);
{
    juce::Graphics g (result);
    g.fillAll (juce::Colours::white);
}
jassert (result.getPixelAt (2, 2).toDisplayString (true) == "FFFFFFFF");

```

This has come up at least once in the past, so we should probably update the docs to make this a bit clearer.

> [@Part of output not visible when 2D drawing with OpenGL context](https://forum.juce.com/t/part-of-output-not-visible-when-2d-drawing-with-opengl-context/56547/3):
>
> Hi reuk, I updated the example here: [GitHub - drlight-code/juce-opengl-minimal](https://github.com/drlight-code/juce-opengl-minimal) In the stripped down example, I can currently only observe the problem when rendering offscreen. In my application, where more things are being drawn, I could however also observe missing primitive parts using the “onscreen” method. You will find three boolean variables to test the different variants, drawOnScreen, drawRect and useLayer. I currently observe the following: useLayer == false && drawOnScreen == false: …

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 8, 2024, 7:49pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/5 "2024-06-08T19:49:57Z")

</div>

Thanks Reuben! Glad to hear I’m not losing it 😃

It doesn’t look like there’s another way to sync/flush, so I’ll go restructure my tests and see what I run into next!

Documenting sounds useful. Maybe it could be part of the general JUCE 8 release notes / docs too. I tried to poke around to see if i could disable Direct2D (`JUCE_DIRECT2D=0`) and use the old renderer on JUCE 8, but it seems like that isn’t possible?

---

<div class="post-metadata">

**Author:** ![fuo](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@fuo](https://forum.juce.com/u/fuo)\
**Post date:** [June 8, 2024, 8:11pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/6 "2024-06-08T20:11:23Z")

</div>

Hi @sudara

You an set the renderer at runtime:

```auto
    void setDirect2D(bool enable) {
        juce::Timer::callAfterDelay(0, [this, enable] {
            jassert(getPeer());
            if (!getPeer()) return;
            getPeer()->setCurrentRenderingEngine(enable ? 1 : 0);
        });
    }

```

The `callAfterDelay` is there so you can use it directly in a component constructor, when getPeer() is still false.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 8, 2024, 8:43pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/7 "2024-06-08T20:43:03Z")

</div>

> [@fuo](#):
>
> You an set the renderer at runtime:

🤯 amazing!

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 8, 2024, 8:56pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/8 "2024-06-08T20:56:33Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/0/1/01fcb48685274549eab3c8496110761d83e85897.png)

Back in biz!

> [@reuk](#):
>
> the image is not completely written/flushed intil the Graphics object goes out of scope

It looks like this is also true for writing pixel data with `juce::Image::BitmapData.`

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 9, 2024, 12:35pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/9 "2024-06-09T12:35:35Z")

</div>

I encountered one last baby edge case in updating blur to JUCE 8. This is in font rendering – unrelated to D2D (occurs on all platforms).

The following code:

```auto
            juce::Image result (juce::Image::ARGB, 9, 9, true);
            juce::Graphics g (result);
            g.fillAll (juce::Colours::white);
            g.setColour (juce::Colours::black);
            g.drawText ("O", 0, 0, 9, 9, juce::Justification::centred, false);

```

JUCE 7 produces this:

 ![Preview - 2024-06-09.31@2x](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/e/5/e5c3242d72fda591708855c4b6f2c101adf17dcf.jpeg)

On JUCE 8, the image remains completely white.

I’m pretty sure this is esoterica — sizes of the default font differ slightly between versions and the text size was almost at the limit of not being painted on JUCE 7. For example if I ` g.setFont(g.getCurrentFont().withHeight(16));` on JUCE 7, I also get a blank image.

Maybe there should be an assertion here vs. just the empty image? But worst case someone, someday will get value from this…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 9, 2024, 1:34pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/10 "2024-06-09T13:34:34Z")

</div>

> [@sudara](#):
>
> `DWMWCP_DONOTROUND`

For anyone else in the future running into this issue, the warning proceeding this error gives a clue:

```auto
CUSTOMBUILD : warning : Environment variable CMAKE_GENERATOR_PLATFORM will
  be ignored, because CMAKE_GENERATOR is not set.

```

Basically, I forgot to specify `-G Ninja` and without a generator it bailed out…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 9, 2024, 8:33pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/11 "2024-06-09T20:33:35Z")

</div>

Good to go!

Even though this project really exercises juce::Graphics and friends, upgrading to JUCE 8 was relatively painless, much more so than other frameworks I’ve used in the past.

 ![Arc - 2024-06-09.11@2x](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/7/6/76368e60e5374fdeab076483178948fa24135b6b.jpeg)

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/matt/32/18340_2.png) [@matt](https://forum.juce.com/u/matt)\
**Post date:** [June 10, 2024, 2:42am UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/12 "2024-06-10T02:42:53Z")

</div>

The JUCE\_DIRECT2D flag has been removed.

As @fuo pointed out, you can switch the renderer for a window by calling setCurrentRenderingEngine. I usually set the renderer from the parentHierarchyChanged callback; here’s an example.

> <https://github.com/mattgonzalez/JUCEDirect2DTest/blob/pips/PIPs/Select%20Renderer.h>

Note that this will only set the renderer for the current window. If you are rendering to an Image, you will still be using Direct2D by default unless you explicitly specify a software image.

Matt

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 10, 2024, 11:36am UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/13 "2024-06-10T11:36:55Z")

</div>

This is great, thanks!

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [June 11, 2024, 12:57am UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/14 "2024-06-11T00:57:24Z")

</div>

@sudara You can also force the software renderer when creating your target-image like this:

`auto shadowImage = juce::Image ( juce::Image::SingleChannel, area.getWidth (), area.getHeight (), true, juce::SoftwareImageType () );`

Now you don’t need to add all the scopes, and I’m sure that (depending on the size and complexity, of course) this might be faster than:

1. Create a native image on the GPU
2. Rendering into it via D2D calls
3. Flushing the draw-queue
4. Copying the image back to RAM

---

<div class="post-metadata">

**Author:** ![fuo](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@fuo](https://forum.juce.com/u/fuo)\
**Post date:** [June 11, 2024, 7:05am UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/15 "2024-06-11T07:05:54Z")

</div>

@matt, it would be useful to have a `convertedToType` Image method to convert from one type to another.

This would for example let us use the native renderer to capture a component (as the differences in font rendering are pretty visible between the Direct2D and software renderers, especially for small fonts), and then convert it to software for further manipulations (eg animated fading, etc).

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/matt/32/18340_2.png) [@matt](https://forum.juce.com/u/matt)\
**Post date:** [June 11, 2024, 7:14am UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/16 "2024-06-11T07:14:47Z")

</div>

Check out ImageType::convert:

[https://docs.juce.com/master/classImageType.html#ae9b3581750ee9df9c1f3c45de5915d35](https://docs.juce.com/master/classImageType.html#ae9b3581750ee9df9c1f3c45de5915d35)

Although you can do animated fading right now without converting the image type.

Matt

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 11, 2024, 4:25pm UTC](https://forum.juce.com/t/juce-8-direct2d-melatonin-blur-issue/61758/17 "2024-06-11T16:25:44Z")

</div>

> [@Toddler-Boy](#):
>
> @sudara You can also force the software renderer when creating your target-image like this:

Thanks! Matt and I chatted yesterday about exactly this option, as in this case it’s literally CPU blurring (we were discussing maybe geeking out over GPU backed mela blurs too, though!). I might leave the scope in the tests since speed isn’t essential there, but i’ll prob have to switch to software backed for the single channel shadow images. I’m curious on the benchmark difference…
