gpt4 book ai didi

c# - 使用 DirectX 将像素着色器应用于视频

转载 作者:太空狗 更新时间:2023-10-29 21:50:49 32 4
gpt4 key购买 nike

我需要将像素着色器应用于此代码(全屏四边形)。我有 FX 文件。程序是什么? (编辑完成:代码正在运行)

public int CompositeImage(IntPtr pD3DDevice, IntPtr pddsRenderTarget, AMMediaType pmtRenderTarget, long rtStart, long rtEnd, int dwClrBkGnd, VMR9VideoStreamInfo[] pVideoStreamInfo, int cStreams)
{
try
{
if (udevice != pD3DDevice)
{
InitCompositionDevice(pD3DDevice);
}

// will be creating managed object from those so increment ref count
Marshal.AddRef(pddsRenderTarget);
Marshal.AddRef(pVideoStreamInfo[0].pddsVideoSurface);

device.Clear(ClearFlags.Target, Color.Red, 1f, 0);
device.BeginScene();

// here the video frame will be stored
Texture capturedVideoTexture = null;

// this is the output surface
Surface renderTarget = new Surface(pddsRenderTarget);

// get the surface for the input video
Surface videoSurface = new Surface(pVideoStreamInfo[0].pddsVideoSurface);

// will use this rect for calculations
Rectangle videoSurfaceRect = new Rectangle(0, 0, videoSurface.Description.Width, videoSurface.Description.Height);

// create single layer texture from input video
capturedVideoTexture = new Texture(device, videoSurfaceRect.Width, videoSurfaceRect.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);

// get its surface
Surface textureSurface = capturedVideoTexture.GetSurfaceLevel(0);

// will use this rect for calculations
Rectangle textureSurfaceRect = new Rectangle(0, 0, textureSurface.Description.Width, textureSurface.Description.Height);

// copy the whole video surface into the texture surface
device.StretchRectangle(videoSurface, videoSurfaceRect, textureSurface, textureSurfaceRect, TextureFilter.Linear);

// identity matreices for world projection and view
device.Transform.World = Matrix.Identity;
device.Transform.Projection = Matrix.Identity;
device.Transform.View = Matrix.Identity;

// setup viewport
Viewport view = new Viewport();
view.X = 0;
view.Y = 0;
view.Width = 1920;
view.Height = 1080;
view.MinZ = 0;
view.MaxZ = 1;
device.Viewport = view;

// writing will go to output surface
device.SetRenderTarget(0, renderTarget);

// nothing fancy of a vertex shader
device.VertexFormat = CustomVertex.PositionTextured.Format;

// use the texture while rendering
device.SetTexture(0, capturedVideoTexture);

//Bind our Vertex Buffer
device.SetStreamSource(0, vb, 0);

// setup and apply shader
ps.Begin(FX.None);
ps.BeginPass(0);
ps.SetValue("ScreenTexture", capturedVideoTexture);

//Render from our Vertex Buffer
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);

ps.EndPass();
ps.End();

device.EndScene();

videoSurface.Dispose();
textureSurface.Dispose();
capturedVideoTexture.Dispose();
renderTarget.Dispose();

videoSurface = null;
renderTarget = null;
capturedVideoTexture = null;

}

catch (Exception e)
{
Debug.WriteLine(e.ToString());
}

return 0;

}

这是最终实现的效果:VMR9 视频上的棕褐色:

http://img825.imageshack.us/img825/9207/sepiag.png

现在剩下的就是编写 3D 内容的浮雕着色器 :)

最佳答案

从视频帧创建您的纹理,然后 LockRect 它并复制数据。您现在在 D3D9 纹理中拥有数据。

加载像素着色器很容易。使用 D3DXCreateEffectFromFile .

渲染带有纹理的四边形可以通过查看 Internet 上的任何教程轻松学习。试试这个:

http://www.drunkenhyena.com/cgi-bin/view_cpp_article.pl?chapter=2;article=30

如果设置正确,效果将自动应用于四边形。

编辑:从您的代码来看,您似乎没有设置世界、 View 或投影矩阵。您也不设置视口(viewport)。

首先,您应该将所有矩阵设置为恒等式。这意味着您传入的顶点将在管道中移动而不会发生任何变化。然后您需要记住“投影”空间从 -1 到 1(从左到右从下到上)。

因此您需要按如下方式定义四边形:

ver[0] = new CustomVertex.PositionTextured(-1, 1, 0, 0, 0);
ver[1] = new CustomVertex.PositionTextured(1, 1, 0, 1, 0);
ver[2] = new CustomVertex.PositionTextured(-1, -1, 0, 0, 1);
ver[3] = new CustomVertex.PositionTextured(1, 1, 0, 1, 0);
ver[4] = new CustomVertex.PositionTextured(1, -1, 0, 1, 1);
ver[5] = new CustomVertex.PositionTextured(-1, -1, 0, 0, 1);

然后您需要按如下方式设置视口(viewport)(这是一个 C++ 视口(viewport)设置,因为我并没有真正使用 C#):

D3DVIEWPORT9 vp;
vp.X = 0;
vp.Y = 0;
vp.Width = 1920;
vp.Height = 1080;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f;
pDevice->SetViewport( &vp );

此时我希望在屏幕上看到整个纹理。

顺便说一句,我也不完全确定你在 SetTextureStageState 中做什么......你只是说“选择 Arg1”。您是否将相当于 D3DTA_TEXTURE 的值传递给 COLOR ARG 1?

关于c# - 使用 DirectX 将像素着色器应用于视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380588/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com