Untitled conversation
- Userlua
- Assistant
-- SobelOperator.lua
-- Применяет оператор Собеля как шейдер-оверлей в SA-MP с использованием MoonLoader, mimgui и LuaJIT FFI
local ffi = require 'ffi'
local imgui = require 'imgui' -- Библиотека для создания интерфейса
local bit = require 'bit'
local band = bit.band
local bor = bit.bor
local lshift = bit.lshift
local rshift = bit.rshift
-- Определяем необходимые структуры и методы через FFI
ffi.cdef[[
typedef long HRESULT;
typedef unsigned long DWORD;
typedef unsigned int UINT;
typedef unsigned char BYTE;
typedef struct IDirect3DDevice9 IDirect3DDevice9;
typedef struct IDirect3DSurface9 IDirect3DSurface9;
typedef struct IDirect3DTexture9 IDirect3DTexture9;
typedef struct D3DLOCKED_RECT {
int Pitch;
void* pBits;
} D3DLOCKED_RECT;
typedef struct RECT {
long left;
long top;
long right;
long bottom;
} RECT;
// Перечисление форматов пикселей
typedef enum D3DFORMAT {
D3DFMT_UNKNOWN = 0,
D3DFMT_A8R8G8B8 = 21
/* Добавьте другие форматы при необходимости */
} D3DFORMAT;
// Типы BackBuffer
typedef enum D3DBACKBUFFER_TYPE {
D3DBACKBUFFER_TYPE_MONO = 0,
D3DBACKBUFFER_TYPE_LEFT = 1,
D3DBACKBUFFER_TYPE_RIGHT = 2
} D3DBACKBUFFER_TYPE;
// Перечисление пулов
typedef enum D3DPOOL {
D3DPOOL_DEFAULT = 0,
D3DPOOL_MANAGED = 1,
D3DPOOL_SYSTEMMEM = 2,
D3DPOOL_SCRATCH = 3
} D3DPOOL;
// Методы устройства Direct3D
typedef HRESULT (__stdcall *GetBackBuffer_t)(IDirect3DDevice9* self, UINT iSwapChain, UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9** ppBackBuffer);
typedef HRESULT (__stdcall *CreateTexture_t)(IDirect3DDevice9* self, UINT Width, UINT Height, UINT Levels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, void* pSharedHandle);
// Методы поверхности Direct3D
typedef HRESULT (__stdcall *LockRect_t)(IDirect3DSurface9* self, D3DLOCKED_RECT* pLockedRect, const RECT* pRect, DWORD Flags);
typedef HRESULT (__stdcall *UnlockRect_t)(IDirect3DSurface9* self);
// Методы текстуры Direct3D
typedef HRESULT (__stdcall *GetSurfaceLevel_t)(IDirect3DTexture9* self, UINT Level, IDirect3DSurface9** ppSurfaceLevel);
// Определяем структуру методов IDirect3DDevice9
typedef struct IDirect3DDevice9_Methods {
void* QueryInterface;
void* AddRef;
void* Release;
/* ... другие методы ... */
void* reserved1[32]; // Скорректируйте в соответствии с реальным количеством методов
GetBackBuffer_t GetBackBuffer;
/* ... другие методы ... */
void* reserved2[55]; // Скорректируйте в соответствии с реальным количеством методов
CreateTexture_t CreateTexture;
/* ... остальные методы ... */
} IDirect3DDevice9_Methods;
// Структура IDirect3DDevice9
typedef struct IDirect3DDevice9 {
IDirect3DDevice9_Methods* lpVtbl;
// Остальные поля опущены
} IDirect3DDevice9;
// Определяем структуру методов IDirect3DSurface9
typedef struct IDirect3DSurface9_Methods {
void* QueryInterface;
void* AddRef;
void* Release;
/* ... другие методы ... */
void* reserved1[3]; // Скорректируйте в соответствии с реальным количеством методов
LockRect_t LockRect;
UnlockRect_t UnlockRect;
/* ... остальные методы ... */
} IDirect3DSurface9_Methods;
// Структура IDirect3DSurface9
typedef struct IDirect3DSurface9 {
IDirect3DSurface9_Methods* lpVtbl;
// Остальные поля опущены
} IDirect3DSurface9;
// Определяем структуру методов IDirect3DTexture9
typedef struct IDirect3DTexture9_Methods {
void* QueryInterface;
void* AddRef;
void* Release;
/* ... другие методы ... */
void* reserved1[20]; // Скорректируйте в соответствии с реальным количеством методов
GetSurfaceLevel_t GetSurfaceLevel;
/* ... остальные методы ... */
} IDirect3DTexture9_Methods;
// Структура IDirect3DTexture9
typedef struct IDirect3DTexture9 {
IDirect3DTexture9_Methods* lpVtbl;
// Остальные поля опущены
} IDirect3DTexture9;
]]
-- Константы
local D3D_OK = 0
-- Определение модуля dx9
local dx9 = {}
-- Константы для dx9
dx9.D3DBACKBUFFER_TYPE_MONO = 0
-- Перечисление форматов D3DFORMAT
dx9.D3DFMT_A8R8G8B8 = 21
dx9.D3DFMT_UNKNOWN = 0
-- Перечисление пулов D3DPOOL
dx9.D3DPOOL_DEFAULT = 0
dx9.D3DPOOL_MANAGED = 1
dx9.D3DPOOL_SYSTEMMEM = 2
dx9.D3DPOOL_SCRATCH = 3
-- Переменная устройства Direct3D
local device = nil
-- Функция обратного вызова для получения устройства Direct3D
function onD3DCreate(pDevice)
device = ffi.cast('IDirect3DDevice9*', pDevice)
end
-- Функция обратного вызова при потере устройства Direct3D
function onD3DLost()
device = nil
end
-- dx9 функции
function dx9.GetDevice()
return device
end
function dx9.GetBackBuffer(device, iSwapChain, iBackBuffer, Type)
local backBufferPP = ffi.new('IDirect3DSurface9*[1]') -- указатель на указатель
local hr = device.lpVtbl.GetBackBuffer(device, iSwapChain, iBackBuffer, Type, backBufferPP)
if hr ~= D3D_OK then return nil end
return backBufferPP[0]
end
function dx9.LockRect(surface, pRect, flags)
local lockedRect = ffi.new('D3DLOCKED_RECT')
local hr = surface.lpVtbl.LockRect(surface, lockedRect, pRect, flags)
if hr ~= D3D_OK then return nil end
return lockedRect
end
function dx9.UnlockRect(surface)
surface.lpVtbl.UnlockRect(surface)
end
function dx9.Release(object)
if object and object.lpVtbl and object.lpVtbl.Release then
object.lpVtbl.Release(object)
end
end
function dx9.CreateTexture(device, Width, Height, Levels, Usage, Format, Pool)
local ppTexture = ffi.new('IDirect3DTexture9*[1]')
local hr = device.lpVtbl.CreateTexture(device, Width, Height, Levels, Usage, Format, Pool, ppTexture, nil)
if hr ~= D3D_OK then return nil end
return ppTexture[0]
end
function dx9.GetSurfaceLevel(texture, Level)
local surfacePP = ffi.new('IDirect3DSurface9*[1]')
local hr = texture.lpVtbl.GetSurfaceLevel(texture, Level, surfacePP)
if hr ~= D3D_OK then return nil end
return surfacePP[0]
end
-- Определение операторов Собеля
local Gx = ffi.new('int[3][3]', {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
})
local Gy = ffi.new('int[3][3]', {
{ 1, 2, 1},
{ 0, 0, 0},
{-1, -2, -1}
})
-- Функция для преобразования ARGB в оттенки серого
local function rgbToGrayscale(r, g, b)
return math.floor(0.3 * r + 0.59 * g + 0.11 * b)
end
-- Функция для преобразования буфера пикселей в оттенки серого
local function to_grayscale(width, height, pixels_ptr, pitch, grayscale)
local ptr = ffi.cast('unsigned int*', pixels_ptr)
for y = 0, height - 1 do
for x = 0, width - 1 do
local pixel = ptr[y * (pitch / 4) + x]
local b = band(pixel, 0xFF)
local g = band(rshift(pixel, 8), 0xFF)
local r = band(rshift(pixel, 16), 0xFF)
grayscale[y * width + x] = rgbToGrayscale(r, g, b)
end
end
end
-- Функция для применения оператора Собеля
local function apply_sobel(width, height, grayscale, edge)
for y = 1, height - 2 do
for x = 1, width - 2 do
local sumX = 0
local sumY = 0
for ky = 0, 2 do
for kx = 0, 2 do
local pixel = grayscale[(y + ky - 1) * width + (x + kx - 1)]
sumX = sumX + Gx[ky][kx] * pixel
sumY = sumY + Gy[ky][kx] * pixel
end
end
local mag = math.sqrt(sumX * sumX + sumY * sumY)
if mag > 255 then mag = 255 end
edge[y * width + x] = math.floor(mag)
end
end
end
-- Функция для создания текстуры из данных границ
local function create_edge_texture(device, width, height, edge_data)
local texture = dx9.CreateTexture(device, width, height, 1, 0, dx9.D3DFMT_A8R8G8B8, dx9.D3DPOOL_MANAGED)
if not texture then return nil end
-- Получаем уровень поверхности
local surface = dx9.GetSurfaceLevel(texture, 0)
if not surface then
dx9.Release(texture)
return nil
end
-- Блокируем всю поверхность
local locked_rect = dx9.LockRect(surface, nil, 0)
if not locked_rect then
dx9.Release(surface)
dx9.Release(texture)
return nil
end
local pixels = ffi.cast('unsigned int*', locked_rect.pBits)
local pitch = locked_rect.Pitch / 4
for y = 0, height - 1 do
for x = 0, width - 1 do
local intensity = edge_data[y * width + x]
local color = bor(lshift(intensity, 16), lshift(intensity, 8), intensity)
pixels[y * pitch + x] = bor(lshift(255, 24), color) -- A8R8G8B8
end
end
dx9.UnlockRect(surface)
dx9.Release(surface)
return texture
end
-- Переменная для хранения текстуры границ
local edge_texture = nil
-- Функция обработки кадра и применения оператора Собеля
local function process_sobel()
local device = dx9.GetDevice()
if not device then return end
-- Получаем Back Buffer
local back_buffer = dx9.GetBackBuffer(device, 0, 0, dx9.D3DBACKBUFFER_TYPE_MONO)
if not back_buffer then return end
-- Получаем размеры экрана
local screen_width, screen_height = getScreenResolution()
if screen_width == 0 or screen_height == 0 then
dx9.Release(back_buffer)
return
end
-- Блокируем Back Buffer для доступа к пикселям
local locked_rect = dx9.LockRect(back_buffer, nil, 0)
if not locked_rect then
dx9.Release(back_buffer)
return
end
-- Выделяем память для оттенков серого и границ
local grayscale = ffi.new('unsigned char[?]', screen_width * screen_height)
local edge = ffi.new('unsigned char[?]', screen_width * screen_height)
-- Преобразуем изображение в оттенки серого
to_grayscale(screen_width, screen_height, locked_rect.pBits, locked_rect.Pitch, grayscale)
-- Разблокируем Back Buffer
dx9.UnlockRect(back_buffer)
dx9.Release(back_buffer)
-- Применяем оператор Собеля
apply_sobel(screen_width, screen_height, grayscale, edge)
-- Создаем текстуру из полученных данных
local new_texture = create_edge_texture(device, screen_width, screen_height, edge)
-- Освобождаем предыдущую текстуру, если она существует
if edge_texture then
dx9.Release(edge_texture)
end
edge_texture = new_texture
end
-- Переменная для видимости окна ImGui
local window = imgui.ImBool(true) -- Показать окно при запуске
-- Основная функция
function main()
while not isSampAvailable() do
wait(200)
end
imgui.Process = false
while true do
wait(0)
imgui.Process = window.v
end
end
-- Функция отрисовки окна ImGui
function imgui.OnDrawFrame()
-- Обработка оператора Собеля при каждом кадре
process_sobel()
if edge_texture then
local resX, resY = getScreenResolution()
local sizeX, sizeY = 300, 300 -- Размер окна
imgui.SetNextWindowPos(imgui.ImVec2(resX / 2 - sizeX / 2, resY / 2 - sizeY / 2), imgui.Cond.FirstUseEver)
imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
if imgui.Begin('Sobel Operator Shader', window) then
-- Отображаем текстуру границ
imgui.Image(edge_texture, imgui.ImVec2(sizeX - 20, sizeY - 40))
end
imgui.End()
end
end
[{ "resource": "/C:/Users/Admin/Downloads/cleaned originalSA by kichiro/moonloader/SobelOperator.lua", "owner": "glua", "severity": 4, "message": "Variable 'device' shadows existing binding, defined at line 144, column 7", "startLineNumber": 161, "startColumn": 28, "endLineNumber": 161, "endColumn": 34 },{ "resource": "/C:/Users/Admin/Downloads/cleaned originalSA by kichiro/moonloader/SobelOperator.lua", "owner": "glua", "severity": 4, "message": "Variable 'device' shadows existing binding, defined at line 144, column 7", "startLineNumber": 185, "startColumn": 28, "endLineNumber": 185, "endColumn": 34 },{ "resource": "/C:/Users/Admin/Downloads/cleaned originalSA by kichiro/moonloader/SobelOperator.lua", "owner": "glua", "severity": 4, "message": "Variable 'device' shadows existing binding, defined at line 144, column 7", "startLineNumber": 252, "startColumn": 36, "endLineNumber": 252, "endColumn": 42 },{ "resource": "/C:/Users/Admin/Downloads/cleaned originalSA by kichiro/moonloader/SobelOperator.lua", "owner": "glua", "severity": 4, "message": "Variable 'device' shadows existing binding, defined at line 144, column 7", "startLineNumber": 291, "startColumn": 11, "endLineNumber": 291, "endColumn": 17 }]
The warnings you're encountering indicate that the variable device is being redefined (or "shadowed") in several places in your code where it already exists as a global variable. This shadowing can lead to confusion and potential bugs, as it makes it harder to predict which device variable is being referred to in different parts of your code.
Let's break down what's happening and how to fix it.
Global Variable device (Line 144):
-- Line 144
local device = nil
Here, you define device as a global variable (since it's at the top level of your script). This variable is intended to store the Direct3D device.
Shadowing Occurs in Functions:
At the lines specified in the warnings (161, 185, 252, and 291), device is either used as a function parameter or redefined as a local variable within a function, which "shadows" the global device.
Function Parameters (Lines 161, 185, 252):
-- Line 161
function dx9.GetBackBuffer(device, iSwapChain, iBackBuffer, Type)
-- Line 185
function dx9.CreateTexture(device, Width, Height, Levels, Usage, Format, Pool)
-- Line 252
local function create_edge_texture(device, width, height, edge_data)
In these functions, device is used as a parameter name, which creates a new local variable device within the function scope, shadowing the global device.
Local Variable (Line 291):
-- Line 291
local function process_sobel()
local device = dx9.GetDevice()
...
end
Here, device is declared as a local variable inside the function, again shadowing the global device.
Shadowing can make your code harder to read and maintain because:
The goal is to ensure that variable names are unique within their scope and don't unintentionally override variables from an outer scope.
Here are the steps to fix the shadowing in your code:
Change the parameter name device to something else in the functions where it's used. A common convention is to prefix parameter names with p (for "parameter") or d3d. Here's how you can modify your functions:
Line 161:
-- Original
function dx9.GetBackBuffer(device, iSwapChain, iBackBuffer, Type)
-- Modified
function dx9.GetBackBuffer(pDevice, iSwapChain, iBackBuffer, Type)
Line 185:
-- Original
function dx9.CreateTexture(device, Width, Height, Levels, Usage, Format, Pool)
-- Modified
function dx9.CreateTexture(pDevice, Width, Height, Levels, Usage, Format, Pool)
Line 252:
-- Original
local function create_edge_texture(device, width, height, edge_data)
-- Modified
local function create_edge_texture(pDevice, width, height, edge_data)
For local variables named device within functions, you can rename them to avoid shadowing the global device.
Line 291:
-- Original
local function process_sobel()
local device = dx9.GetDevice()
if not device then return end
...
end
-- Modified
local function process_sobel()
local pDevice = dx9.GetDevice()
if not pDevice then return end
...
end
Note: After renaming, make sure to update all references to device within the function to the new name pDevice.
After renaming parameters and local variables, ensure that you update all instances where they are used within their respective functions.
For example, in dx9.GetBackBuffer:
-- After renaming parameter to pDevice
function dx9.GetBackBuffer(pDevice, iSwapChain, iBackBuffer, Type)
local backBufferPP = ffi.new('IDirect3DSurface9*[1]')
local hr = pDevice.lpVtbl.GetBackBuffer(pDevice, iSwapChain, iBackBuffer, Type, backBufferPP)
if hr ~= D3D_OK then return nil end
return backBufferPP[0]
end
Here's how the modified functions would look:
dx9.GetBackBuffer:
function dx9.GetBackBuffer(pDevice, iSwapChain, iBackBuffer, Type)
local backBufferPP = ffi.new('IDirect3DSurface9*[1]')
local hr = pDevice.lpVtbl.GetBackBuffer(pDevice, iSwapChain, iBackBuffer, Type, backBufferPP)
if hr ~= D3D_OK then return nil end
return backBufferPP[0]
end
dx9.CreateTexture:
function dx9.CreateTexture(pDevice, Width, Height, Levels, Usage, Format, Pool)
local ppTexture = ffi.new('IDirect3DTexture9*[1]')
local hr = pDevice.lpVtbl.CreateTexture(pDevice, Width, Height, Levels, Usage, Format, Pool, ppTexture, nil)
if hr ~= D3D_OK then return nil end
return ppTexture[0]
end
create_edge_texture:
local function create_edge_texture(pDevice, width, height, edge_data)
local texture = dx9.CreateTexture(pDevice, width, height, 1, 0, dx9.D3DFMT_A8R8G8B8, dx9.D3DPOOL_MANAGED)
if not texture then return nil end
-- Get the surface level
local surface = dx9.GetSurfaceLevel(texture, 0)
if not surface then
dx9.Release(texture)
return nil
end
-- Lock the entire surface
local locked_rect = dx9.LockRect(surface, nil, 0)
if not locked_rect then
dx9.Release(surface)
dx9.Release(texture)
return nil
end
local pixels = ffi.cast('unsigned int*', locked_rect.pBits)
local pitch = locked_rect.Pitch / 4
for y = 0, height - 1 do
for x = 0, width - 1 do
local intensity = edge_data[y * width + x]
local color = bit.bor(bit.lshift(intensity, 16), bit.lshift(intensity, 8), intensity)
pixels[y * pitch + x] = bit.bor(bit.lshift(255, 24), color) -- A8R8G8B8
end
end
dx9.UnlockRect(surface)
dx9.Release(surface)
return texture
end
p for parameters, local for local variables, and avoid reusing names of global variables.By renaming the local variables and function parameters that are shadowing the global device variable, you eliminate the warnings and make your code more robust and maintainable. This change ensures that the scope of each variable is clear, reducing the potential for bugs related to variable access and modification.
Feel free to apply these changes to your code, and the warnings should be resolved. If you have any further questions or need assistance with other parts of your code, don't hesitate to ask!
Not yet included in a published release.
process_sobel:
local function process_sobel()
local pDevice = dx9.GetDevice()
if not pDevice then return end
-- Get Back Buffer
local back_buffer = dx9.GetBackBuffer(pDevice, 0, 0, dx9.D3DBACKBUFFER_TYPE_MONO)
if not back_buffer then return end
-- Get screen dimensions
local screen_width, screen_height = getScreenResolution()
if screen_width == 0 or screen_height == 0 then
dx9.Release(back_buffer)
return
end
-- Lock Back Buffer to access pixels
local locked_rect = dx9.LockRect(back_buffer, nil, 0)
if not locked_rect then
dx9.Release(back_buffer)
return
end
-- Allocate memory for grayscale and edge data
local grayscale = ffi.new('unsigned char[?]', screen_width * screen_height)
local edge = ffi.new('unsigned char[?]', screen_width * screen_height)
-- Convert image to grayscale
to_grayscale(screen_width, screen_height, locked_rect.pBits, locked_rect.Pitch, grayscale)
-- Unlock Back Buffer
dx9.UnlockRect(back_buffer)
dx9.Release(back_buffer)
-- Apply Sobel operator
apply_sobel(screen_width, screen_height, grayscale, edge)
-- Create texture from the resulting data
local new_texture = create_edge_texture(pDevice, screen_width, screen_height, edge)
-- Release previous texture if it exists
if edge_texture then
dx9.Release(edge_texture)
end
edge_texture = new_texture
end