From 30c577beeb4772e5f5ac4697eef5942e814ee544 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 7 Nov 2023 10:19:24 +1100 Subject: [PATCH] [host] all: make the downsample rule matching common --- host/include/downsample_parser.h | 2 + .../platform/Windows/capture/DXGI/src/d3d12.c | 8 -- host/platform/Windows/capture/DXGI/src/dxgi.c | 108 ++---------------- .../Windows/capture/DXGI/src/dxgi_capture.h | 1 - .../Windows/capture/NVFBC/src/nvfbc.c | 19 +-- host/src/downsample_parser.c | 19 +++ 6 files changed, 37 insertions(+), 120 deletions(-) diff --git a/host/include/downsample_parser.h b/host/include/downsample_parser.h index ef428279..c25b02e1 100644 --- a/host/include/downsample_parser.h +++ b/host/include/downsample_parser.h @@ -37,6 +37,8 @@ extern Vector downsampleRules; bool downsampleParser(struct Option * opt, const char * str); +DownsampleRule * downsampleRule_match(int x, int y); + #define DOWNSAMPLE_PARSER(moduleName) \ { \ .module = moduleName, \ diff --git a/host/platform/Windows/capture/DXGI/src/d3d12.c b/host/platform/Windows/capture/DXGI/src/d3d12.c index f953602e..c8b8ae34 100644 --- a/host/platform/Windows/capture/DXGI/src/d3d12.c +++ b/host/platform/Windows/capture/DXGI/src/d3d12.c @@ -91,14 +91,6 @@ static bool d3d12_create(struct DXGIInterface * intf) if (!d3d12) return false; - if (dxgi->downsampleLevel > 0) - { - DEBUG_WARN("The D3D12 backend does not support downsampling yet"); - dxgi->downsampleLevel = 0; - dxgi->outputWidth = dxgi->width; - dxgi->outputHeight = dxgi->height; - } - if (dxgi->debug) { D3D12GetDebugInterface_t D3D12GetDebugInterface = (D3D12GetDebugInterface_t) diff --git a/host/platform/Windows/capture/DXGI/src/dxgi.c b/host/platform/Windows/capture/DXGI/src/dxgi.c index 817b331f..21ce4ff7 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/host/platform/Windows/capture/DXGI/src/dxgi.c @@ -20,6 +20,7 @@ #include "interface/capture.h" #include "interface/platform.h" +#include "downsample_parser.h" #include "common/array.h" #include "common/debug.h" #include "common/windebug.h" @@ -65,18 +66,6 @@ typedef struct } PostProcessInstance; -typedef struct -{ - unsigned int id; - bool greater; - unsigned int x; - unsigned int y; - unsigned int level; -} -DownsampleRule; - -static Vector downsampleRules = {0}; - // locals static struct DXGIInterface * this = NULL; @@ -112,58 +101,6 @@ static const char * dxgi_getName(void) return name; } -static bool downsampleOptParser(struct Option * opt, const char * str) -{ - if (!str) - return false; - - opt->value.x_string = strdup(str); - - if (downsampleRules.data) - vector_destroy(&downsampleRules); - - if (!vector_create(&downsampleRules, sizeof(DownsampleRule), 10)) - { - DEBUG_ERROR("Failed to allocate ram"); - return false; - } - - char * tmp = strdup(str); - char * token = strtok(tmp, ","); - int count = 0; - while(token) - { - DownsampleRule rule = {0}; - if (token[0] == '>') - { - rule.greater = true; - ++token; - } - - if (sscanf(token, "%ux%u:%u", &rule.x, &rule.y, &rule.level) != 3) - return false; - - rule.id = count++; - - DEBUG_INFO( - "Rule %u: %u%% IF X %s %4u %s Y %s %4u", - rule.id, - 100 / (1 << rule.level), - rule.greater ? "> " : "==", - rule.x, - rule.greater ? "OR " : "AND", - rule.greater ? "> " : "==", - rule.y - ); - vector_push(&downsampleRules, &rule); - - token = strtok(NULL, ","); - } - free(tmp); - - return true; -} - static void dxgi_initOptions(void) { struct Option options[] = @@ -182,14 +119,7 @@ static void dxgi_initOptions(void) .type = OPTION_TYPE_STRING, .value.x_string = NULL }, - { - .module = "dxgi", - .name = "downsample", //dxgi:downsample=1920x1200:1, - .description = "Downsample conditions and levels, format: [>](width)x(height):level", - .type = OPTION_TYPE_STRING, - .value.x_string = NULL, - .parser = downsampleOptParser - }, + DOWNSAMPLE_PARSER("dxgi"), { .module = "dxgi", .name = "maxTextures", @@ -761,27 +691,13 @@ static bool dxgi_init(void) goto fail; } - this->downsampleLevel = 0; - this->outputWidth = this->width; - this->outputHeight = this->height; - - DownsampleRule * rule, * match = NULL; - vector_forEachRef(rule, &downsampleRules) + this->outputWidth = this->width; + this->outputHeight = this->height; + DownsampleRule * rule = downsampleRule_match(this->width, this->height); + if (rule) { - if ( - ( rule->greater && (this->width > rule->x || this->height > rule->y)) || - (!rule->greater && (this->width == rule->x && this->height == rule->y))) - { - match = rule; - } - } - - if (match) - { - DEBUG_INFO("Matched downsample rule %d", rule->id); - this->downsampleLevel = match->level; - this->outputWidth >>= match->level; - this->outputHeight >>= match->level; + this->outputWidth = rule->targetX; + this->outputHeight = rule->targetY; } DEBUG_INFO("Request Size : %u x %u", this->outputWidth, this->outputHeight); @@ -940,10 +856,10 @@ static void rectToFrameDamageRect(RECT * src, FrameDamageRect * dst) { *dst = (FrameDamageRect) { - .x = src->left >> this->downsampleLevel, - .y = src->top >> this->downsampleLevel, - .width = (src->right - src->left) >> this->downsampleLevel, - .height = (src->bottom - src->top) >> this->downsampleLevel + .x = src->left , + .y = src->top , + .width = (src->right - src->left), + .height = (src->bottom - src->top) }; } diff --git a/host/platform/Windows/capture/DXGI/src/dxgi_capture.h b/host/platform/Windows/capture/DXGI/src/dxgi_capture.h index 269fd756..9b1332e4 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi_capture.h +++ b/host/platform/Windows/capture/DXGI/src/dxgi_capture.h @@ -105,7 +105,6 @@ struct DXGIInterface unsigned int formatVer; unsigned int width , outputWidth , dataWidth; unsigned int height, outputHeight, dataHeight; - unsigned int downsampleLevel; unsigned int pitch; unsigned int stride; unsigned int padding; diff --git a/host/platform/Windows/capture/NVFBC/src/nvfbc.c b/host/platform/Windows/capture/NVFBC/src/nvfbc.c index 1a86bd9c..c6920caf 100644 --- a/host/platform/Windows/capture/NVFBC/src/nvfbc.c +++ b/host/platform/Windows/capture/NVFBC/src/nvfbc.c @@ -191,23 +191,12 @@ static bool nvfbc_create( static void updateScale(void) { - DownsampleRule * rule, * match = NULL; - vector_forEachRef(rule, &downsampleRules) + DownsampleRule * rule = downsampleRule_match(this->width, this->height); + if (rule) { - if ( - ( rule->greater && (this->width > rule->x || this->height > rule->y)) || - (!rule->greater && (this->width == rule->x && this->height == rule->y))) - { - match = rule; - } - } - - if (match) - { - DEBUG_INFO("Matched downsample rule %d", rule->id); this->scale = true; - this->targetWidth = match->targetX; - this->targetHeight = match->targetY; + this->targetWidth = rule->targetX; + this->targetHeight = rule->targetY; return; } diff --git a/host/src/downsample_parser.c b/host/src/downsample_parser.c index 69aa80cd..d93047cd 100644 --- a/host/src/downsample_parser.c +++ b/host/src/downsample_parser.c @@ -84,3 +84,22 @@ bool downsampleParser(struct Option * opt, const char * str) return true; } + +DownsampleRule * downsampleRule_match(int x, int y) +{ + DownsampleRule * rule, * match = NULL; + vector_forEachRef(rule, &downsampleRules) + { + if ( + ( rule->greater && (x > rule->x || y > rule->y)) || + (!rule->greater && (x == rule->x && y == rule->y))) + { + match = rule; + } + } + + if (match) + DEBUG_INFO("Matched downsample rule %d", rule->id); + + return match; +}