This commit is contained in:
mofeng-git
2025-12-28 18:19:16 +08:00
commit d143d158e4
771 changed files with 220548 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
[package]
name = "capture"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
[build-dependencies]
cc = "1.0"
bindgen = "0.59"

View File

@@ -0,0 +1,51 @@
use cc::Build;
use std::{
env,
path::{Path, PathBuf},
};
fn main() {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let externals_dir = manifest_dir
.parent()
.unwrap()
.parent()
.unwrap()
.join("externals");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed={}", externals_dir.display());
let ffi_header = "src/dxgi_ffi.h";
bindgen::builder()
.header(ffi_header)
.rustified_enum("*")
.generate()
.unwrap()
.write_to_file(Path::new(&env::var_os("OUT_DIR").unwrap()).join("capture_ffi.rs"))
.unwrap();
let mut builder = Build::new();
// system
#[cfg(windows)]
["d3d11", "dxgi"].map(|lib| println!("cargo:rustc-link-lib={}", lib));
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-lib=stdc++");
#[cfg(windows)]
{
// dxgi
let dxgi_path = externals_dir.join("nvEncDXGIOutputDuplicationSample");
builder.include(&dxgi_path);
for f in vec!["DDAImpl.cpp"] {
builder.file(format!("{}/{}", dxgi_path.display(), f));
}
builder.file("src/dxgi.cpp");
}
// crate
builder
.cpp(false)
.static_crt(true)
.warnings(false)
.compile("capture");
}

View File

@@ -0,0 +1,42 @@
#include <DDA.h>
#include <Windows.h>
#include <string>
extern "C" void *dxgi_new_capturer(int64_t luid) {
DemoApplication *d = new DemoApplication(luid);
HRESULT hr = d->Init();
if (FAILED(hr)) {
delete d;
d = NULL;
return NULL;
}
return d;
}
extern "C" void *dxgi_device(void *capturer) {
DemoApplication *d = (DemoApplication *)capturer;
return d->Device();
}
extern "C" int dxgi_width(const void *capturer) {
DemoApplication *d = (DemoApplication *)capturer;
return d->width();
}
extern "C" int dxgi_height(const void *capturer) {
DemoApplication *d = (DemoApplication *)capturer;
return d->height();
}
extern "C" void *dxgi_capture(void *capturer, int wait_ms) {
DemoApplication *d = (DemoApplication *)capturer;
void *texture = d->Capture(wait_ms);
return texture;
}
extern "C" void destroy_dxgi_capturer(void *capturer) {
DemoApplication *d = (DemoApplication *)capturer;
if (d)
delete d;
}

View File

@@ -0,0 +1,42 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::os::raw::c_void;
include!(concat!(env!("OUT_DIR"), "/capture_ffi.rs"));
pub struct Capturer {
inner: *mut c_void,
}
impl Capturer {
pub fn new(luid: i64) -> Result<Self, ()> {
let inner = unsafe { dxgi_new_capturer(luid) };
if inner.is_null() {
Err(())
} else {
Ok(Self { inner })
}
}
pub unsafe fn device(&mut self) -> *mut c_void {
dxgi_device(self.inner)
}
pub unsafe fn width(&self) -> i32 {
dxgi_width(self.inner)
}
pub unsafe fn height(&self) -> i32 {
dxgi_height(self.inner)
}
pub unsafe fn capture(&mut self, wait_ms: i32) -> *mut c_void {
dxgi_capture(self.inner, wait_ms)
}
pub unsafe fn drop(&mut self) {
destroy_dxgi_capturer(self.inner);
}
}

View File

@@ -0,0 +1,13 @@
#ifndef FFI_H
#define FFI_H
#include <stdint.h>
void *dxgi_new_capturer(int64_t luid);
void *dxgi_device(void *capturer);
int dxgi_width(const void *capturer);
int dxgi_height(const void *capturer);
void *dxgi_capture(void *capturer, int wait_ms);
void destroy_dxgi_capturer(void *capturer);
#endif // FFI_H

View File

@@ -0,0 +1,2 @@
#[cfg(windows)]
pub mod dxgi;

View File

@@ -0,0 +1,13 @@
[package]
name = "render"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
[build-dependencies]
cc = "1.0"
bindgen = "0.59"

View File

@@ -0,0 +1,50 @@
use cc::Build;
use std::{
env,
path::{Path, PathBuf},
};
fn main() {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let externals_dir = manifest_dir
.parent()
.unwrap()
.parent()
.unwrap()
.join("externals");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed={}", externals_dir.display());
let ffi_header = "src/render_ffi.h";
bindgen::builder()
.header(ffi_header)
.rustified_enum("*")
.generate()
.unwrap()
.write_to_file(Path::new(&env::var_os("OUT_DIR").unwrap()).join("render_ffi.rs"))
.unwrap();
let mut builder = Build::new();
// system
#[cfg(windows)]
["d3d11", "dxgi", "User32"].map(|lib| println!("cargo:rustc-link-lib={}", lib));
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-lib=stdc++");
#[cfg(windows)]
{
let sdl_dir = externals_dir.join("SDL");
builder.include(sdl_dir.join("include"));
let sdl_lib_path = sdl_dir.join("lib").join("x64");
builder.file(manifest_dir.join("src").join("dxgi_sdl.cpp"));
println!("cargo:rustc-link-search=native={}", sdl_lib_path.display());
println!("cargo:rustc-link-lib=SDL2");
}
// crate
builder
.cpp(false)
.static_crt(true)
.warnings(false)
.compile("render");
}

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,581 @@
#include <atomic>
#include <chrono>
#include <cstdio>
#include <list>
#include <mutex>
#include <thread>
#include <vector>
#include <DirectXMath.h>
#include <SDL.h>
#include <SDL_syswm.h>
#include <d3d11.h>
#include <d3d11_1.h>
#include <d3d11_2.h>
#include <d3d11_3.h>
#include <d3d11_4.h>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <iostream>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
#define SAFE_RELEASE(p) \
{ \
if ((p)) { \
(p)->Release(); \
(p) = nullptr; \
} \
}
#define LUID(desc) \
(((int64_t)desc.AdapterLuid.HighPart << 32) | desc.AdapterLuid.LowPart)
#define HRB(f) MS_CHECK(f, return false;)
#define HRI(f) MS_CHECK(f, return -1;)
#define HRP(f) MS_CHECK(f, return nullptr;)
#define MS_CHECK(f, ...) \
do { \
HRESULT __ms_hr__ = (f); \
if (FAILED(__ms_hr__)) { \
std::clog \
<< #f " ERROR@" << __LINE__ << __FUNCTION__ << ": (" << std::hex \
<< __ms_hr__ << std::dec << ") " \
<< std::error_code(__ms_hr__, std::system_category()).message() \
<< std::endl \
<< std::flush; \
__VA_ARGS__ \
} \
} while (false)
#define MS_THROW(f, ...) MS_CHECK(f, throw std::runtime_error(#f);)
#define LUID(desc) \
(((int64_t)desc.AdapterLuid.HighPart << 32) | desc.AdapterLuid.LowPart)
#ifndef CSO_DIR
#define CSO_DIR "dev/render/res"
#endif
struct AdatperOutputs {
IDXGIAdapter1 *adapter;
DXGI_ADAPTER_DESC1 desc;
AdatperOutputs() : adapter(nullptr){};
AdatperOutputs(AdatperOutputs &&src) noexcept {
adapter = src.adapter;
src.adapter = nullptr;
desc = src.desc;
}
AdatperOutputs(const AdatperOutputs &src) {
adapter = src.adapter;
adapter->AddRef();
desc = src.desc;
}
~AdatperOutputs() {
if (adapter)
adapter->Release();
}
};
bool get_first_adapter_output(IDXGIFactory2 *factory2,
IDXGIAdapter1 **adapter_out,
IDXGIOutput1 **output_out, int64_t luid) {
UINT num_adapters = 0;
AdatperOutputs curent_adapter;
IDXGIAdapter1 *selected_adapter = nullptr;
IDXGIOutput1 *selected_output = nullptr;
HRESULT hr = S_OK;
bool found = false;
while (factory2->EnumAdapters1(num_adapters, &curent_adapter.adapter) !=
DXGI_ERROR_NOT_FOUND) {
++num_adapters;
DXGI_ADAPTER_DESC1 desc = DXGI_ADAPTER_DESC1();
curent_adapter.adapter->GetDesc1(&desc);
if (LUID(desc) != luid) {
continue;
}
selected_adapter = curent_adapter.adapter;
selected_adapter->AddRef();
IDXGIOutput *output;
if (curent_adapter.adapter->EnumOutputs(0, &output) !=
DXGI_ERROR_NOT_FOUND) {
IDXGIOutput1 *temp;
hr = output->QueryInterface(IID_PPV_ARGS(&temp));
if (SUCCEEDED(hr)) {
selected_output = temp;
}
}
found = true;
break;
}
*adapter_out = selected_adapter;
*output_out = selected_output;
return found;
}
class dx_device_context {
public:
dx_device_context(int64_t luid) {
// This is what matters (the 1)
// Only a guid of fatory2 will not work
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory2));
if (FAILED(hr))
exit(hr);
if (!get_first_adapter_output(factory2, &adapter1, &output1, luid)) {
std::cout << "no render adapter found" << std::endl;
exit(-1);
}
D3D_FEATURE_LEVEL levels[]{D3D_FEATURE_LEVEL_11_0};
hr = D3D11CreateDevice(
adapter1, D3D_DRIVER_TYPE_UNKNOWN, NULL,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT | D3D11_CREATE_DEVICE_BGRA_SUPPORT,
levels, 1, D3D11_SDK_VERSION, &device, NULL, &context);
if (FAILED(hr))
exit(hr);
hr = device->QueryInterface(IID_PPV_ARGS(&video_device));
if (FAILED(hr))
exit(hr);
hr = context->QueryInterface(IID_PPV_ARGS(&video_context));
if (FAILED(hr))
exit(hr);
hr = context->QueryInterface(IID_PPV_ARGS(&hmt));
if (FAILED(hr))
exit(hr);
// This is required for MFXVideoCORE_SetHandle
hr = hmt->SetMultithreadProtected(TRUE);
if (FAILED(hr))
exit(hr);
}
~dx_device_context() {
if (hmt)
hmt->Release();
if (video_context)
video_context->Release();
if (video_device)
video_device->Release();
if (context)
context->Release();
if (device)
device->Release();
if (output1)
output1->Release();
if (adapter1)
adapter1->Release();
if (factory2)
factory2->Release();
}
IDXGIFactory2 *factory2 = nullptr;
IDXGIAdapter1 *adapter1 = nullptr;
IDXGIOutput1 *output1 = nullptr;
ID3D11Device *device = nullptr;
ID3D11DeviceContext *context = nullptr;
ID3D11VideoDevice *video_device = nullptr;
ID3D11VideoContext *video_context = nullptr;
ID3D10Multithread *hmt = nullptr;
HMODULE debug_mod = nullptr;
};
class simplerenderer {
public:
simplerenderer(HWND in_window, dx_device_context &dev_ctx)
: ctx(dev_ctx), window(in_window) {
ctx.factory2->MakeWindowAssociation(in_window, 0);
sampler_view = nullptr;
D3D11_SAMPLER_DESC desc = CD3D11_SAMPLER_DESC(CD3D11_DEFAULT());
HRESULT hr = ctx.device->CreateSamplerState(&desc, &sampler_interp);
if (FAILED(hr))
exit(hr);
init_fbo0(window);
init_vbo();
init_shaders();
}
~simplerenderer() {
// render_thread.join();
if (sampler_interp)
sampler_interp->Release();
if (sampler_view)
sampler_view->Release();
if (vbo)
vbo->Release();
if (vao)
vao->Release();
if (frag)
frag->Release();
if (vert)
vert->Release();
if (fbo0)
fbo0->Release();
if (fbo0rbo)
fbo0rbo->Release();
if (swapchain)
swapchain->Release();
}
private:
void init_fbo0(HWND window) {
DXGI_SWAP_CHAIN_DESC1 swp_desc{
1280, 720, DXGI_FORMAT_B8G8R8A8_UNORM, FALSE, DXGI_SAMPLE_DESC{1, 0},
DXGI_USAGE_RENDER_TARGET_OUTPUT, 3, DXGI_SCALING_STRETCH,
// DXGI_SWAP_EFFECT_DISCARD,
DXGI_SWAP_EFFECT_FLIP_DISCARD, DXGI_ALPHA_MODE_UNSPECIFIED, 0};
HRESULT hr = ctx.factory2->CreateSwapChainForHwnd(
ctx.device, window, &swp_desc, NULL, NULL, &swapchain);
if (FAILED(hr))
exit(hr);
fbo0 = nullptr;
fbo0rbo = nullptr;
RECT rect;
GetClientRect(window, &rect);
hr = swapchain->GetBuffer(0, IID_PPV_ARGS(&fbo0rbo));
if (FAILED(hr))
exit(hr);
hr = ctx.device->CreateRenderTargetView(fbo0rbo, nullptr, &fbo0);
if (FAILED(hr))
exit(hr);
D3D11_VIEWPORT VP{};
VP.Width = static_cast<FLOAT>(rect.right - rect.left);
VP.Height = static_cast<FLOAT>(rect.bottom - rect.top);
VP.MinDepth = 0.0f;
VP.MaxDepth = 1.0f;
VP.TopLeftX = 0;
VP.TopLeftY = 0;
ctx.context->RSSetViewports(1, &VP);
}
void init_vbo() {
struct vertex {
DirectX::XMFLOAT3 Pos;
DirectX::XMFLOAT2 TexCoord;
};
vertex points[4]{{DirectX::XMFLOAT3(-1, 1, 0), DirectX::XMFLOAT2(0, 0)},
{DirectX::XMFLOAT3(1, 1, 0), DirectX::XMFLOAT2(1, 0)},
{DirectX::XMFLOAT3(-1, -1, 0), DirectX::XMFLOAT2(0, 1)},
{DirectX::XMFLOAT3(1, -1, 0), DirectX::XMFLOAT2(1, 1)}};
D3D11_BUFFER_DESC vbo_desc{};
vbo_desc.ByteWidth = sizeof(vertex) * 4;
vbo_desc.Usage = D3D11_USAGE_IMMUTABLE;
vbo_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA initial_data{};
initial_data.pSysMem = points;
HRESULT hr = ctx.device->CreateBuffer(&vbo_desc, &initial_data, &vbo);
if (FAILED(hr))
exit(hr);
vbo_stride = sizeof(vertex);
vbo_offset = 0;
}
void init_shaders() {
uint8_t *shader_bytecode = nullptr;
size_t bytecode_len = 0;
// read file
FILE *shader_file = fopen(CSO_DIR "/frag.cso", "rb");
fseek(shader_file, 0, SEEK_END);
bytecode_len = ftell(shader_file);
fseek(shader_file, 0, SEEK_SET);
shader_bytecode = (uint8_t *)malloc(bytecode_len);
fread(shader_bytecode, 1, bytecode_len, shader_file);
HRESULT hr = ctx.device->CreatePixelShader(shader_bytecode, bytecode_len,
nullptr, &frag);
if (FAILED(hr))
exit(hr);
// free(shader_bytecode);
shader_file = freopen(CSO_DIR "/vert.cso", "rb", shader_file);
fseek(shader_file, 0, SEEK_END);
bytecode_len = ftell(shader_file);
fseek(shader_file, 0, SEEK_SET);
shader_bytecode = (uint8_t *)malloc(bytecode_len);
fread(shader_bytecode, 1, bytecode_len, shader_file);
// fclose(shader_file);
hr = ctx.device->CreateVertexShader(shader_bytecode, bytecode_len, nullptr,
&vert);
if (FAILED(hr))
exit(hr);
D3D11_INPUT_ELEMENT_DESC input_desc[]{
// name, vertex attrib index, format, unpack alignment, instance
// releated
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12,
D3D11_INPUT_PER_VERTEX_DATA, 0},
};
hr = ctx.device->CreateInputLayout(input_desc, 2, shader_bytecode,
bytecode_len, &vao);
if (FAILED(hr))
exit(hr);
// free(shader_bytecode);
ctx.context->VSSetShader(vert, nullptr, 0);
ctx.context->IASetInputLayout(vao);
ctx.context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
ctx.context->IASetVertexBuffers(0, 1, &vbo, &vbo_stride, &vbo_offset);
ctx.context->PSSetShader(frag, nullptr, 0);
ctx.context->PSSetSamplers(0, 1, &sampler_interp);
}
void bind_texture(ID3D11Texture2D *texture) {
D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc);
D3D11_SHADER_RESOURCE_VIEW_DESC shader_resource_desc{};
shader_resource_desc.Format = desc.Format;
;
shader_resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shader_resource_desc.Texture2D = {0, 1};
if (sampler_view)
sampler_view->Release();
HRESULT hr = ctx.device->CreateShaderResourceView(
texture, &shader_resource_desc, &sampler_view);
if (FAILED(hr))
exit(hr);
ctx.context->PSSetShaderResources(0, 1, &sampler_view);
}
void resize_swapchain(uint32_t width, uint32_t height) {
if (fbo0)
fbo0->Release();
if (fbo0rbo)
fbo0rbo->Release();
HRESULT hr =
swapchain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
hr = swapchain->GetBuffer(0, IID_PPV_ARGS(&fbo0rbo));
if (FAILED(hr))
exit(hr);
hr = ctx.device->CreateRenderTargetView(fbo0rbo, nullptr, &fbo0);
if (FAILED(hr))
exit(hr);
D3D11_VIEWPORT VP{};
VP.Width = static_cast<FLOAT>(width);
VP.Height = static_cast<FLOAT>(height);
VP.MinDepth = 0.0f;
VP.MaxDepth = 1.0f;
VP.TopLeftX = 0;
VP.TopLeftY = 0;
ctx.context->RSSetViewports(1, &VP);
}
std::chrono::high_resolution_clock::time_point last_fps_time =
std::chrono::high_resolution_clock::now();
public:
void render_frame(ID3D11Texture2D *texture) {
if (!occluded) {
bind_texture(texture);
if (need_resize.load(std::memory_order_acquire)) {
atomic_packed_32x2 temp;
temp.packed.store(client_size.packed.load(std::memory_order_relaxed),
std::memory_order_relaxed);
resize_swapchain(temp.separate.width, temp.separate.height);
}
ctx.context->OMSetRenderTargets(1, &fbo0, nullptr);
ctx.context->Draw(4, 0);
HRESULT hr = swapchain->Present(0, 0);
if (FAILED(hr))
exit(hr);
if (hr == DXGI_STATUS_OCCLUDED) {
occluded = true;
}
frame_count++;
std::chrono::high_resolution_clock::time_point current =
std::chrono::high_resolution_clock::now();
if (current - last_fps_time >= std::chrono::seconds(1)) {
int fps = frame_count - last_frame_count;
last_frame_count = frame_count;
last_fps_time = current;
std::cout << fps << " Hz" << std::endl;
}
} else {
HRESULT hr = swapchain->Present(0, DXGI_PRESENT_TEST);
if (FAILED(hr))
exit(hr);
if (!DXGI_STATUS_OCCLUDED) {
occluded = false;
}
}
}
void set_size(uint32_t width, uint32_t height) {
atomic_packed_32x2 temp;
temp.separate.width = width;
temp.separate.height = height;
client_size.packed.store(temp.packed.load(std::memory_order_relaxed),
std::memory_order_relaxed);
}
HWND window;
dx_device_context &ctx;
IDXGISwapChain1 *swapchain;
ID3D11Texture2D *fbo0rbo;
ID3D11RenderTargetView *fbo0;
ID3D11VertexShader *vert;
ID3D11PixelShader *frag;
ID3D11InputLayout *vao;
ID3D11Buffer *vbo;
ID3D11ShaderResourceView *sampler_view;
ID3D11SamplerState *sampler_interp;
UINT vbo_stride;
UINT vbo_offset;
std::thread render_thread;
std::atomic_bool running;
std::atomic_bool need_resize;
bool occluded = false;
int frame_count = 0;
int last_frame_count = 0;
struct atomic_packed_32x2 {
union {
struct detail {
uint32_t width;
uint32_t height;
} separate;
std::atomic_uint64_t packed;
};
atomic_packed_32x2();
} client_size;
};
simplerenderer::atomic_packed_32x2::atomic_packed_32x2(void) {}
class Render {
public:
Render(int64_t luid, bool inputSharedHandle);
int Init();
int RenderTexture(ID3D11Texture2D *);
std::unique_ptr<std::thread> message_thread;
std::unique_ptr<simplerenderer> renderer;
bool running = false;
std::unique_ptr<dx_device_context> ctx;
// dx_device_context ctx;
int64_t luid;
bool inputSharedHandle;
};
Render::Render(int64_t luid, bool inputSharedHandle) {
// ctx.reset(new dx_device_context());
this->luid = luid;
this->inputSharedHandle = inputSharedHandle;
ctx = std::make_unique<dx_device_context>(luid);
};
static void run(Render *self) {
SetProcessDPIAware();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow(
"test window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280,
720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_SysWMinfo info{};
SDL_GetWindowWMInfo(window, &info);
{
self->renderer.reset(new simplerenderer(info.info.win.window, *self->ctx));
MONITORINFOEX monitor_info{};
monitor_info.cbSize = sizeof(monitor_info);
DXGI_OUTPUT_DESC screen_desc;
if (self->ctx->output1) {
HRESULT hr = self->ctx->output1->GetDesc(&screen_desc);
GetMonitorInfo(screen_desc.Monitor, &monitor_info);
}
self->running = true;
bool maximized = false;
while (self->running) {
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
// capturer.Stop();
self->running = false;
break;
case SDL_WINDOWEVENT_MAXIMIZED:
if (self->ctx->output1) {
int border_l, border_r, border_t, border_b;
SDL_GetWindowBordersSize(window, &border_t, &border_l, &border_b,
&border_r);
int max_w = monitor_info.rcWork.right - monitor_info.rcWork.left;
int max_h =
monitor_info.rcWork.bottom - monitor_info.rcWork.top - border_t;
SDL_SetWindowSize(window, max_w, max_h);
SDL_SetWindowPosition(window, monitor_info.rcWork.left, border_t);
maximized = true;
}
break;
case SDL_WINDOWEVENT_RESTORED:
maximized = false;
break;
case SDL_WINDOWEVENT_RESIZED:
if (self->ctx->output1) {
int max_w, max_h;
SDL_GetWindowMaximumSize(window, &max_w, &max_h);
double aspect = double(screen_desc.DesktopCoordinates.right -
screen_desc.DesktopCoordinates.left) /
(screen_desc.DesktopCoordinates.bottom -
screen_desc.DesktopCoordinates.top);
int temp = event.window.data1 * event.window.data2;
int width = sqrt(temp * aspect) + 0.5;
int height = sqrt(temp / aspect) + 0.5;
int pos_x, pos_y;
SDL_GetWindowPosition(window, &pos_x, &pos_y);
int ori_w, ori_h;
SDL_GetWindowSize(window, &ori_w, &ori_h);
SDL_SetWindowPosition(window, pos_x + ((ori_w - width) / 2),
pos_y + ((ori_h - height) / 2));
SDL_SetWindowSize(window, width, height);
}
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
self->renderer->set_size(event.window.data1, event.window.data2);
break;
default:
break;
}
break;
default:
break;
}
if (event.type == SDL_WINDOWEVENT) {
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
exit(0);
}
int Render::Init() {
message_thread.reset(new std::thread(run, this));
return 0;
}
int Render::RenderTexture(ID3D11Texture2D *texture) {
renderer->render_frame(texture);
return 0;
}
extern "C" void *CreateDXGIRender(int64_t luid, bool inputSharedHandle) {
Render *p = new Render(luid, inputSharedHandle);
p->Init();
return p;
}
extern "C" int DXGIRenderTexture(void *render, HANDLE handle) {
Render *self = (Render *)render;
if (!self->running)
return 0;
ComPtr<ID3D11Texture2D> texture = nullptr;
if (self->inputSharedHandle) {
ComPtr<IDXGIResource> resource = nullptr;
ComPtr<ID3D11Texture2D> tex_ = nullptr;
MS_THROW(self->ctx->device->OpenSharedResource(
handle, __uuidof(ID3D11Texture2D),
(void **)resource.ReleaseAndGetAddressOf()));
MS_THROW(resource.As(&tex_));
texture = tex_.Get();
} else {
texture = (ID3D11Texture2D *)handle;
}
self->RenderTexture(texture.Get());
return 0;
}
extern "C" void DestroyDXGIRender(void *render) {
Render *self = (Render *)render;
self->running = false;
if (self->message_thread)
self->message_thread->join();
}
extern "C" void *DXGIDevice(void *render) {
Render *self = (Render *)render;
return self->ctx->device;
}

View File

@@ -0,0 +1,39 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::os::raw::c_void;
include!(concat!(env!("OUT_DIR"), "/render_ffi.rs"));
pub struct Render {
inner: *mut c_void,
}
impl Render {
pub fn new(luid: i64, input_shared_handle: bool) -> Result<Self, ()> {
let inner = unsafe { CreateDXGIRender(luid, input_shared_handle) };
if inner.is_null() {
Err(())
} else {
Ok(Self { inner })
}
}
pub unsafe fn render(&mut self, tex: *mut c_void) -> Result<(), i32> {
let result = DXGIRenderTexture(self.inner, tex);
if result == 0 {
Ok(())
} else {
Err(result)
}
}
pub unsafe fn device(&mut self) -> *mut c_void {
DXGIDevice(self.inner)
}
pub unsafe fn drop(&mut self) {
DestroyDXGIRender(self.inner);
}
}

View File

@@ -0,0 +1,11 @@
#ifndef RENDER_FFI_H
#define RENDER_FFI_H
#include <stdbool.h>
void *CreateDXGIRender(long long luid, bool inputSharedHandle);
int DXGIRenderTexture(void *render, void *tex);
void DestroyDXGIRender(void *render);
void *DXGIDevice(void *render);
#endif // RENDER_FFI_H

View File

@@ -0,0 +1,13 @@
[package]
name = "tool"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
[build-dependencies]
cc = "1.0"
bindgen = "0.59"

View File

@@ -0,0 +1,39 @@
use cc::Build;
use std::{
env,
path::{Path, PathBuf},
};
fn main() {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
println!("cargo:rerun-if-changed=src");
let ffi_header = "src/tool_ffi.h";
bindgen::builder()
.header(ffi_header)
.rustified_enum("*")
.generate()
.unwrap()
.write_to_file(Path::new(&env::var_os("OUT_DIR").unwrap()).join("tool_ffi.rs"))
.unwrap();
let mut builder = Build::new();
builder.include(
manifest_dir
.parent()
.unwrap()
.parent()
.unwrap()
.join("cpp")
.join("common"),
);
builder.file("src/tool.cpp");
// crate
builder
.cpp(false)
.static_crt(true)
.warnings(false)
.compile("tool");
}

View File

@@ -0,0 +1,44 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::os::raw::c_void;
include!(concat!(env!("OUT_DIR"), "/tool_ffi.rs"));
pub struct Tool {
inner: *mut c_void,
}
impl Tool {
pub fn new(luid: i64) -> Result<Self, ()> {
let inner = unsafe { tool_new(luid) };
if inner.is_null() {
Err(())
} else {
Ok(Self { inner })
}
}
pub fn device(&mut self) -> *mut c_void {
unsafe { tool_device(self.inner) }
}
pub fn get_texture(&mut self, width: i32, height: i32) -> *mut c_void {
unsafe { tool_get_texture(self.inner, width, height) }
}
pub fn get_texture_size(&mut self, texture: *mut c_void) -> (i32, i32) {
let mut width = 0;
let mut height = 0;
unsafe { tool_get_texture_size(self.inner, texture, &mut width, &mut height) }
(width, height)
}
}
impl Drop for Tool {
fn drop(&mut self) {
unsafe { tool_destroy(self.inner) }
self.inner = std::ptr::null_mut();
}
}

View File

@@ -0,0 +1,67 @@
#include <memory.h>
#include "common.h"
#include "system.h"
namespace {
class Tool {
public:
std::unique_ptr<NativeDevice> native_;
bool initialized_ = false;
public:
Tool(int64_t luid) {
native_ = std::make_unique<NativeDevice>();
initialized_ = native_->Init(luid, nullptr, 1);
}
ID3D11Texture2D *GetTexture(int width, int height) {
native_->EnsureTexture(width, height);
return native_->GetCurrentTexture();
}
void getSize(ID3D11Texture2D *texture, int *width, int *height) {
D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc);
*width = desc.Width;
*height = desc.Height;
}
};
} // namespace
extern "C" {
void *tool_new(int64_t luid) {
Tool *t = new Tool(luid);
if (t && !t->initialized_) {
delete t;
return nullptr;
}
return t;
}
void *tool_device(void *tool) {
Tool *t = (Tool *)tool;
return t->native_->device_.Get();
}
void *tool_get_texture(void *tool, int width, int height) {
Tool *t = (Tool *)tool;
return t->GetTexture(width, height);
}
void tool_get_texture_size(void *tool, void *texture, int *width, int *height) {
Tool *t = (Tool *)tool;
t->getSize((ID3D11Texture2D *)texture, width, height);
}
void tool_destroy(void *tool) {
Tool *t = (Tool *)tool;
if (t) {
delete t;
t = nullptr;
}
}
} // extern "C"

View File

@@ -0,0 +1,12 @@
#ifndef TOOL_FFI_H
#define TOOL_FFI_H
#include <stdint.h>
void *tool_new(int64_t luid);
void *tool_device(void *tool);
void *tool_get_texture(void *tool, int width, int height);
void tool_get_texture_size(void *tool, void *texture, int *width, int *height);
void tool_destroy(void *tool);
#endif // TOOL_FFI_H

View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AMFTest", "AMFTest.vcxproj", "{59599E6A-52F7-44DD-9EC5-487342FF33F8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Debug|x64.ActiveCfg = Debug|x64
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Debug|x64.Build.0 = Debug|x64
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Debug|x86.ActiveCfg = Debug|Win32
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Debug|x86.Build.0 = Debug|Win32
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Release|x64.ActiveCfg = Release|x64
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Release|x64.Build.0 = Release|x64
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Release|x86.ActiveCfg = Release|Win32
{59599E6A-52F7-44DD-9EC5-487342FF33F8}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E1168B32-184A-4268-AC43-21E66A82F076}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,154 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{59599e6a-52f7-44dd-9ec5-487342ff33f8}</ProjectGuid>
<RootNamespace>AMFTest</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;CSO_DIR="../../render/res";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>..\..\externals\AMF_v1.4.29\amf;..\..\externals\AMF_v1.4.29\amf\public\common;..\..\common\src;..\..\common\src\platform\win;..\..\externals\nvEncDXGIOutputDuplicationSample;..\..\codec\src;..\..\externals\SDL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>..\..\externals\SDL\lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>SDL2.lib;dxgi.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\amf\src\common.cpp" />
<ClCompile Include="..\..\amf\src\decode.cpp" />
<ClCompile Include="..\..\amf\src\encode.cpp" />
<ClCompile Include="..\..\capture\src\dxgi.cpp" />
<ClCompile Include="..\..\codec\src\data.c" />
<ClCompile Include="..\..\codec\src\utils.c" />
<ClCompile Include="..\..\common\src\log.cpp" />
<ClCompile Include="..\..\common\src\platform\win\win.cpp" />
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\AMFFactory.cpp" />
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\AMFSTL.cpp" />
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\Thread.cpp" />
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\TraceAdapter.cpp" />
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\Windows\ThreadWindows.cpp" />
<ClCompile Include="..\..\externals\nvEncDXGIOutputDuplicationSample\DDAImpl.cpp" />
<ClCompile Include="..\..\externals\nvEncDXGIOutputDuplicationSample\Preproc.cpp" />
<ClCompile Include="..\..\render\src\dxgi_sdl.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="source">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="source\externals">
<UniqueIdentifier>{a09eabb2-ceac-4062-b19e-244b29176c2b}</UniqueIdentifier>
</Filter>
<Filter Include="source\externals\AMF_v1.4.29">
<UniqueIdentifier>{8adb5a7a-3fba-4330-9a43-dc4251f2ff2f}</UniqueIdentifier>
</Filter>
<Filter Include="source\externals\nvEncDXGIOutputDuplicationSample">
<UniqueIdentifier>{2bc8fd1a-5082-40fb-a6b5-ac8c3b84d450}</UniqueIdentifier>
</Filter>
<Filter Include="source\render">
<UniqueIdentifier>{6edc9023-4c96-4474-b544-20059b2e32ac}</UniqueIdentifier>
</Filter>
<Filter Include="source\codec">
<UniqueIdentifier>{ccee2176-d5f2-46b3-b138-538ac9bd7d13}</UniqueIdentifier>
</Filter>
<Filter Include="source\common">
<UniqueIdentifier>{8a1b8e43-9bdb-4e28-98d9-854c8e8bf107}</UniqueIdentifier>
</Filter>
<Filter Include="source\capture">
<UniqueIdentifier>{a65898d7-8992-40d0-9141-7b4f10415e1f}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>source</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\AMFFactory.cpp">
<Filter>source\externals\AMF_v1.4.29</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\AMFSTL.cpp">
<Filter>source\externals\AMF_v1.4.29</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\Thread.cpp">
<Filter>source\externals\AMF_v1.4.29</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\Windows\ThreadWindows.cpp">
<Filter>source\externals\AMF_v1.4.29</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\AMF_v1.4.29\amf\public\common\TraceAdapter.cpp">
<Filter>source\externals\AMF_v1.4.29</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\nvEncDXGIOutputDuplicationSample\DDAImpl.cpp">
<Filter>source\externals\nvEncDXGIOutputDuplicationSample</Filter>
</ClCompile>
<ClCompile Include="..\..\externals\nvEncDXGIOutputDuplicationSample\Preproc.cpp">
<Filter>source\externals\nvEncDXGIOutputDuplicationSample</Filter>
</ClCompile>
<ClCompile Include="..\..\common\src\platform\win\win.cpp">
<Filter>source\common</Filter>
</ClCompile>
<ClCompile Include="..\..\capture\src\dxgi.cpp">
<Filter>source\capture</Filter>
</ClCompile>
<ClCompile Include="..\..\render\src\dxgi_sdl.cpp">
<Filter>source\render</Filter>
</ClCompile>
<ClCompile Include="..\..\codec\src\data.c">
<Filter>source\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\codec\src\utils.c">
<Filter>source\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\amf\src\common.cpp">
<Filter>source\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\amf\src\decode.cpp">
<Filter>source\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\amf\src\encode.cpp">
<Filter>source\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\common\src\log.cpp">
<Filter>source\common</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@@ -0,0 +1,109 @@
#include <Windows.h>
#include <callback.h>
#include <common.h>
#include <iostream>
#include <stdint.h>
#include <system.h>
extern "C" {
void *dxgi_new_capturer(int64_t luid);
void *dxgi_device(void *self);
int dxgi_width(const void *self);
int dxgi_height(const void *self);
void *dxgi_capture(void *self, int wait_ms);
void destroy_dxgi_capturer(void *self);
void *amf_new_encoder(void *hdl, int64_t luid, API api, DataFormat dataFormat,
int32_t width, int32_t height, int32_t kbs,
int32_t framerate, int32_t gop);
int amf_encode(void *e, void *tex, EncodeCallback callback, void *obj);
int amf_destroy_encoder(void *e);
void *amf_new_decoder(void *device, int64_t luid, int32_t api,
int32_t dataFormat, bool outputSharedHandle);
int amf_decode(void *decoder, uint8_t *data, int32_t length,
DecodeCallback callback, void *obj);
int amf_destroy_decoder(void *decoder);
void *CreateDXGIRender(long long luid, bool inputSharedHandle);
int DXGIRenderTexture(void *render, HANDLE shared_handle);
void DestroyDXGIRender(void *render);
void *DXGIDevice(void *render);
}
static const uint8_t *encode_data;
static int32_t encode_len;
static void *decode_shared_handle;
extern "C" static void encode_callback(const uint8_t *data, int32_t len,
int32_t key, const void *obj) {
encode_data = data;
encode_len = len;
std::cerr << "encode len" << len << std::endl;
}
extern "C" static void decode_callback(void *shared_handle, const void *obj) {
decode_shared_handle = shared_handle;
}
extern "C" void log_gpucodec(int level, const char *message) {
std::cout << message << std::endl;
}
int main() {
Adapters adapters;
adapters.Init(ADAPTER_VENDOR_AMD);
if (adapters.adapters_.size() == 0) {
std::cout << "no amd adapter" << std::endl;
return -1;
}
int64_t luid = LUID(adapters.adapters_[0].get()->desc1_);
DataFormat dataFormat = H264;
void *dup = dxgi_new_capturer(luid);
if (!dup) {
std::cerr << "create duplicator failed" << std::endl;
return -1;
}
int width = dxgi_width(dup);
int height = dxgi_height(dup);
std::cout << "width: " << width << " height: " << height << std::endl;
void *device = dxgi_device(dup);
void *encoder = amf_new_encoder(device, luid, API_DX11, dataFormat, width,
height, 4000, 30, 0xFFFF);
if (!encoder) {
std::cerr << "create encoder failed" << std::endl;
return -1;
}
void *render = CreateDXGIRender(luid, false);
if (!render) {
std::cerr << "create render failed" << std::endl;
return -1;
}
void *decoder =
amf_new_decoder(DXGIDevice(render), luid, API_DX11, dataFormat, false);
if (!decoder) {
std::cerr << "create decoder failed" << std::endl;
return -1;
}
while (true) {
void *texture = dxgi_capture(dup, 100);
if (!texture) {
std::cerr << "texture is NULL" << std::endl;
continue;
}
if (0 != amf_encode(encoder, texture, encode_callback, NULL)) {
std::cerr << "encode failed" << std::endl;
continue;
}
if (0 != amf_decode(decoder, (uint8_t *)encode_data, encode_len,
decode_callback, NULL)) {
std::cerr << "decode failed" << std::endl;
continue;
}
if (0 != DXGIRenderTexture(render, decode_shared_handle)) {
std::cerr << "render failed" << std::endl;
continue;
}
}
// no release temporarily
}

View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MFXTest", "MFXTest.vcxproj", "{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Debug|x64.ActiveCfg = Debug|x64
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Debug|x64.Build.0 = Debug|x64
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Debug|x86.ActiveCfg = Debug|Win32
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Debug|x86.Build.0 = Debug|Win32
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Release|x64.ActiveCfg = Release|x64
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Release|x64.Build.0 = Release|x64
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Release|x86.ActiveCfg = Release|Win32
{1FBDACB6-9142-40DA-9B50-5F591F1DD0AD}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B559C12-4403-4530-9A4E-7F4DF6235164}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,175 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{1fbdacb6-9142-40da-9b50-5f591f1dd0ad}</ProjectGuid>
<RootNamespace>MFXTest</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;CSO_DIR="../../render/res";MFX_D3D11_SUPPORT;NOMINMAX=1;MFX_DEPRECATED_OFF;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>D:\rustdesk\gpucodec\externals\libvpl_v2023.4.0\tools\legacy\media_sdk_compatibility_headers;..\..\..\externals\libvpl_v2023.4.0\libvpl;..\..\..\externals\libvpl_v2023.4.0\api;..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src;..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\include;..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\include\vm;..\..\..\externals\nvEncDXGIOutputDuplicationSample;..\..\..\native\common;..\..\..\native\gpucodec;..\..\..\externals\SDL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>..\..\..\externals\SDL\lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>SDL2.lib;dxgi.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_config_interface\mfx_config_interface.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_config_interface\mfx_config_interface_string_api.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_config.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_loader.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_log.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_lowlatency.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_msdk.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_critical_section.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dispatcher.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dispatcher_log.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dispatcher_main.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_driver_store_loader.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dxva2_device.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_function_table.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_library_iterator.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_load_dll.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_win_reg_key.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\avc_bitstream.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\avc_nal_spl.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\avc_spl.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\base_allocator.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\d3d11_allocator.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\sample_utils.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\atomic.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\shared_object.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\thread_windows.cpp" />
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\time.cpp" />
<ClCompile Include="..\..\..\externals\nvEncDXGIOutputDuplicationSample\DDAImpl.cpp" />
<ClCompile Include="..\..\..\native\common\log.cpp" />
<ClCompile Include="..\..\..\native\common\platform\win\win.cpp" />
<ClCompile Include="..\..\..\native\gpucodec\data.c" />
<ClCompile Include="..\..\..\native\gpucodec\gpucodec_utils.c" />
<ClCompile Include="..\..\..\native\vpl\vpl_decode.cpp" />
<ClCompile Include="..\..\..\native\vpl\vpl_encode.cpp" />
<ClCompile Include="..\..\capture\src\dxgi.cpp" />
<ClCompile Include="..\..\render\src\dxgi_sdl.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,172 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\capture">
<UniqueIdentifier>{bd78eb0e-0894-4f11-a2f5-2cc02731bc52}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\codec">
<UniqueIdentifier>{dd2d68d6-6559-413b-948b-f718b34cde0a}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external">
<UniqueIdentifier>{fd52358a-9b99-43b4-b717-f3a77580a52b}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\nvEncDXGIOutputDuplicationSample">
<UniqueIdentifier>{bdbd71f9-8ed4-4bdd-a8c3-612f5858504d}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\render">
<UniqueIdentifier>{2089080f-479d-4d89-8cf5-7ac8e52b11d1}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\common">
<UniqueIdentifier>{3514d21d-4a74-44b9-b21b-bea909cda0b2}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl">
<UniqueIdentifier>{ad9c7b01-cd0a-40a2-afe6-f81133ac0e15}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl\libvpl">
<UniqueIdentifier>{ad00df31-d9af-432b-b452-b3ab9c2b381c}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl\libvpl\src">
<UniqueIdentifier>{b5498eaa-1ff8-4c2b-b5c3-7c71a0a2efcc}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl\libvpl\src\windows">
<UniqueIdentifier>{bbca452b-a73f-4d37-a393-627c0db2617c}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl\libvpl\src\mfx_config_interface">
<UniqueIdentifier>{cb497035-08af-4b38-9abd-e4616861d366}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl\sample_common">
<UniqueIdentifier>{85b55a79-fab7-428c-bc6c-fdf0a58b4704}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\external\vpl\sample_common\vm">
<UniqueIdentifier>{a3824aee-0800-467f-964b-61a76000e160}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\capture\src\dxgi.cpp">
<Filter>Source Files\capture</Filter>
</ClCompile>
<ClCompile Include="..\..\render\src\dxgi_sdl.cpp">
<Filter>Source Files\render</Filter>
</ClCompile>
<ClCompile Include="..\..\..\native\common\log.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\native\common\platform\win\win.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\native\gpucodec\data.c">
<Filter>Source Files\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\..\native\gpucodec\gpucodec_utils.c">
<Filter>Source Files\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_critical_section.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dispatcher.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dispatcher_log.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dispatcher_main.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_driver_store_loader.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_dxva2_device.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_function_table.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_library_iterator.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_load_dll.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\windows\mfx_win_reg_key.cpp">
<Filter>Source Files\external\vpl\libvpl\src\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_config_interface\mfx_config_interface.cpp">
<Filter>Source Files\external\vpl\libvpl\src\mfx_config_interface</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_config_interface\mfx_config_interface_string_api.cpp">
<Filter>Source Files\external\vpl\libvpl\src\mfx_config_interface</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl.cpp">
<Filter>Source Files\external\vpl\libvpl\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_config.cpp">
<Filter>Source Files\external\vpl\libvpl\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_loader.cpp">
<Filter>Source Files\external\vpl\libvpl\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_log.cpp">
<Filter>Source Files\external\vpl\libvpl\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_lowlatency.cpp">
<Filter>Source Files\external\vpl\libvpl\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\libvpl\src\mfx_dispatcher_vpl_msdk.cpp">
<Filter>Source Files\external\vpl\libvpl\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\avc_bitstream.cpp">
<Filter>Source Files\external\vpl\sample_common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\avc_nal_spl.cpp">
<Filter>Source Files\external\vpl\sample_common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\avc_spl.cpp">
<Filter>Source Files\external\vpl\sample_common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\base_allocator.cpp">
<Filter>Source Files\external\vpl\sample_common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\sample_utils.cpp">
<Filter>Source Files\external\vpl\sample_common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\atomic.cpp">
<Filter>Source Files\external\vpl\sample_common\vm</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\shared_object.cpp">
<Filter>Source Files\external\vpl\sample_common\vm</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\thread_windows.cpp">
<Filter>Source Files\external\vpl\sample_common\vm</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\vm\time.cpp">
<Filter>Source Files\external\vpl\sample_common\vm</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\nvEncDXGIOutputDuplicationSample\DDAImpl.cpp">
<Filter>Source Files\external\nvEncDXGIOutputDuplicationSample</Filter>
</ClCompile>
<ClCompile Include="..\..\..\native\vpl\vpl_decode.cpp">
<Filter>Source Files\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\..\native\vpl\vpl_encode.cpp">
<Filter>Source Files\codec</Filter>
</ClCompile>
<ClCompile Include="..\..\..\externals\libvpl_v2023.4.0\tools\legacy\sample_common\src\d3d11_allocator.cpp">
<Filter>Source Files\external\vpl\sample_common</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@@ -0,0 +1,111 @@
#include <Windows.h>
#include <callback.h>
#include <common.h>
#include <iostream>
#include <stdint.h>
#include <system.h>
extern "C" {
void *dxgi_new_capturer(int64_t luid);
void *dxgi_device(void *self);
int dxgi_width(const void *self);
int dxgi_height(const void *self);
void *dxgi_capture(void *self, int wait_ms);
void destroy_dxgi_capturer(void *self);
void *vpl_new_encoder(void *hdl, int64_t luid, API api, DataFormat dataFormat,
int32_t width, int32_t height, int32_t kbs,
int32_t framerate, int32_t gop);
int vpl_encode(void *e, void *tex, EncodeCallback callback, void *obj);
int vpl_destroy_encoder(void *e);
void *vpl_new_decoder(void *device, int64_t luid, int32_t api,
int32_t dataFormat, bool outputSharedHandle);
int vpl_decode(void *decoder, uint8_t *data, int32_t length,
DecodeCallback callback, void *obj);
int vpl_destroy_decoder(void *decoder);
void *CreateDXGIRender(long long luid, bool inputSharedHandle);
int DXGIRenderTexture(void *render, HANDLE shared_handle);
void DestroyDXGIRender(void *render);
void *DXGIDevice(void *render);
}
static const uint8_t *encode_data;
static int32_t encode_len;
static void *decode_shared_handle;
extern "C" static void encode_callback(const uint8_t *data, int32_t len,
int32_t key, const void *obj) {
encode_data = data;
encode_len = len;
}
extern "C" static void decode_callback(void *shared_handle, const void *obj) {
decode_shared_handle = shared_handle;
}
extern "C" void log_gpucodec(int level, const char *message) {
std::cout << message << std::endl;
}
int main() {
Adapters adapters;
adapters.Init(ADAPTER_VENDOR_INTEL);
if (adapters.adapters_.size() == 0) {
std::cout << "no intel adapter" << std::endl;
return -1;
}
int64_t luid = LUID(adapters.adapters_[0].get()->desc1_);
DataFormat dataFormat = H264;
void *dup = dxgi_new_capturer(luid);
if (!dup) {
std::cerr << "create duplicator failed" << std::endl;
return -1;
}
int width = dxgi_width(dup);
int height = dxgi_height(dup);
std::cout << "width: " << width << " height: " << height << std::endl;
void *device = dxgi_device(dup);
void *encoder = vpl_new_encoder(device, luid, API_DX11, dataFormat, width,
height, 4000, 30, 0xFFFF);
if (!encoder) {
std::cerr << "create encoder failed" << std::endl;
return -1;
}
void *render = CreateDXGIRender(luid, false);
if (!render) {
std::cerr << "create render failed" << std::endl;
return -1;
}
void *decoder =
vpl_new_decoder(DXGIDevice(render), luid, API_DX11, dataFormat, false);
if (!decoder) {
std::cerr << "create decoder failed" << std::endl;
return -1;
}
while (true) {
void *texture = dxgi_capture(dup, 100);
if (!texture) {
std::cerr << "texture is NULL" << std::endl;
continue;
}
if (0 != vpl_encode(encoder, texture, encode_callback, NULL)) {
std::cerr << "encode failed" << std::endl;
continue;
}
if (0 != vpl_decode(decoder, (uint8_t *)encode_data, encode_len,
decode_callback, NULL)) {
std::cerr << "decode failed" << std::endl;
continue;
}
if (0 != DXGIRenderTexture(render, decode_shared_handle)) {
std::cerr << "render failed" << std::endl;
continue;
}
}
// no release temporarily
}

View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ShaderCompileTool", "ShaderCompileTool.vcxproj", "{AB626EFE-F38C-4587-B79C-29FC898FBC96}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Debug|x64.ActiveCfg = Debug|x64
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Debug|x64.Build.0 = Debug|x64
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Debug|x86.ActiveCfg = Debug|Win32
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Debug|x86.Build.0 = Debug|Win32
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Release|x64.ActiveCfg = Release|x64
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Release|x64.Build.0 = Release|x64
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Release|x86.ActiveCfg = Release|Win32
{AB626EFE-F38C-4587-B79C-29FC898FBC96}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A483A95-1588-48EF-A2C9-2B4731AFCAB2}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,150 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{ab626efe-f38c-4587-b79c-29fc898fbc96}</ProjectGuid>
<RootNamespace>ShaderCompileTool</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<FxCompile Include="nv_vertex_shader.hlsl">
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
</ObjectFileOutput>
<EntryPointName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">VS</EntryPointName>
<DisableOptimizations Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</DisableOptimizations>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
<HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).h</HeaderFileOutput>
</FxCompile>
<FxCompile Include="nv_pixel_shader_601.hlsl">
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
</ObjectFileOutput>
<EntryPointName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">PS</EntryPointName>
<DisableOptimizations Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</DisableOptimizations>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).h</HeaderFileOutput>
</FxCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<FxCompile Include="nv_pixel_shader_601.hlsl" />
<FxCompile Include="nv_vertex_shader.hlsl" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@@ -0,0 +1,20 @@
Texture2D g_txFrame0 : register(t0);
Texture2D g_txFrame1 : register(t1);
SamplerState g_Sam : register(s0);
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
float4 PS(PS_INPUT input) : SV_TARGET{
float y = g_txFrame0.Sample(g_Sam, input.Tex).r;
y = 1.164383561643836 * (y - 0.0625);
float2 uv = g_txFrame1.Sample(g_Sam, input.Tex).rg - float2(0.5f, 0.5f);
float u = uv.x;
float v = uv.y;
float r = saturate(y + 1.596026785714286 * v);
float g = saturate(y - 0.812967647237771 * v - 0.391762290094914 * u);
float b = saturate(y + 2.017232142857142 * u);
return float4(r, g, b, 1.0f);
}

View File

@@ -0,0 +1,15 @@
struct VS_INPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD;
};
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD;
};
VS_OUTPUT VS(VS_INPUT input)
{
return input;
}