0
我想繪製從Win2D-Example-Gallery中取得的mandelbrot-set並稍微調整一下。Win2D:CanvasVirtualControl的正確用法
起初,我有我的所有代碼生成的CanvasAnimatedControl
的CreateResources
- 方法裏面的天壤之別,但由於性能問題,我又使用着色器(HLSL或PixelShaderEffect
)和CanvasVirtualControl
做到這一點:
public PixelShaderEffect _effectMandel;
CanvasVirtualImageSource _sdrc;
public async Task CreateResources(CanvasVirtualControl sender)
{
_sdrc = new CanvasVirtualImageSource(sender, new Size(_width, _height));
var arr = await FileHelper.ReadAllBytes("Shaders/Mandelbrot.bin");
if (arr != null)
{
_effectMandel = new PixelShaderEffect(arr);
using (CanvasDrawingSession drawingSession = sender.CreateDrawingSession(new Rect(0,0,_width,_height)))
{
drawingSession.DrawImage(_effectMandel);
}
}
}
當我運行應用程序,我在使用部分獲得System.Runtime.InteropServices.COMException
權利和「App.gics」文件打開了告訴我:
我使用的着色器代碼是這樣的:
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// This shader has no input textures.
// It generates a mandelbrot fractal.
#define D2D_INPUT_COUNT 0
#define D2D_REQUIRES_SCENE_POSITION
#include "d2d1effecthelpers.hlsli"
float scale;
float2 translate;
static const float4 tapOffsetsX = float4(-0.25, 0.25, -0.25, 0.25);
static const float4 tapOffsetsY = float4(-0.25, -0.25, 0.25, 0.25);
static const int iterations = 100;
D2D_PS_ENTRY(main)
{
float2 pos = D2DGetScenePosition().xy;
// Improve visual quality by supersampling inside the pixel shader, evaluating four separate
// versions of the fractal in parallel, each at a slightly different position offset.
// The x, y, z, and w components of these float4s contain the four simultaneous computations.
float4 c_r = (pos.x + tapOffsetsX) * scale + translate.x;
float4 c_i = (pos.y + tapOffsetsY) * scale + translate.y;
float4 value_r = 0;
float4 value_i = 0;
// Evalulate the Mandelbrot fractal.
for (int i = 0; i < iterations; i++)
{
float4 new_r = value_r * value_r - value_i * value_i + c_r;
float4 new_i = value_r * value_i * 2 + c_i;
value_r = new_r;
value_i = new_i;
}
// Adjust our four parallel results to range 0:1.
float4 distanceSquared = value_r * value_r + value_i * value_i;
float4 vectorResult = isfinite(distanceSquared) ? saturate(1 - distanceSquared) : 0;
// Resolve the supersampling to produce a single scalar result.
float result = dot(vectorResult, 0.25);
if (result < 1.0/256)
return 0;
else
return float4(result, result, result, 1);
}
如果你知道爲什麼發生這種情況,請回答。謝謝!
可能是因爲您加載的文件不是編譯的FX着色器?退房http://stackoverflow.com/questions/1938514/pixel-shader-effect-examples –
@BerinLoritsch我更新了我的問題,並提供了來自Microsoft的着色器代碼。我現在按照此處提供的示例https://microsoft.github.io/Win2D/html/M_Microsoft_Graphics_Canvas_Effects_PixelShaderEffect__ctor.htm,但它也不起作用。 – Manticore
自從我編寫着色器代碼已經有一段時間了,我遇到的最大問題是確保二進制文件符合我的預期。我不認爲編譯的着色器代碼是32/64位敏感的。另外,請確保清除所有目標文件('.g.i.cs'文件是生成的源文件)。您可能還必須重新啓動Visual Studio。 –